Home > database >  I need help please
I need help please

Time:11-27

in the image is the output i see details: it gives me zero primes in the array after running it. type of program: its a program that takes 2d array [n x m] then calculate how many prime numbers are there in the 2d array.

int isprime(int n)
{
    int k;
    if (n <= 1)
        return 0;
    for (k = 2; k <= (n / 2); k  ){
        if (n % k == 0)
         return 0;
    }
    return 1;
}
}
int primecount(int x, int y, int a[x][y]){
    int r, c, count = 0;
    for(r = 0; r < x; r  ) {
        for(c = 0; c < y; c  ) {
            if(isprime(a[r][c]))
            {
                    count  ;
            }
        }
    }
    return count;
}
int main()
{
    int n, m, i, j, z;
    printf("Enter the Number of Rows: ");
    scanf("%d", &n);
    printf("\n");
    printf("Enter the Number of Columns: ");
    scanf("%d", &m);
    printf("\n");
    int a[n][m];
    printf("Enter the elements of the array: \n");
    for(i = 0; i < n; i  )
    {
        for(j = 0; j < m; j  )
            scanf("%d", &a[i][j]);
    }
    z = primecount(n, m, a[n][m]);
    printf("\n");
    printf("The Number of Prime Numbers in the array is: %d", z);
    printf("\n");
    return 0;
}

CodePudding user response:

For starters instead of this call

z = primecount(n, m, a[n][m]);

you need to write

z = primecount(n, m, a);

In this call of the function isprime as in the call shown above

if(isprime(r, c, a[r][c]))

the expression a[r][c] is a scalar object of the type int. However the function isprime expects a two-dimensional array instead of a scalar object of the type int.

int isprime(int p, int q, int a[p][q])
                          ^^^^^^^^^^^

Just declare the function like

int isprime( int x );

and correspondingly change its definition.

The function will be called like

if( isprime( a[r][c] ) )

Pay attention to that the logic of the function isprime is incorrect. It returns logical true for values equal to 1 and 0 though such values are not prime numbers.

Also you need to deal with an array with elements of the type unsigned int. Otherwise the user can enter negative values.

Here is your updated program.

#include <stdio.h>

int isprime(int n)
{
    int k;
    if (n <= 1)
        return 0;
    for (k = 2; k <= (n / 2); k  )
    {
        if (n % k == 0)
         return 0;
    }
    return 1;
}

int primecount(int x, int y, int a[x][y]) //function to count prime numbers
{
    int r, c, count = 0;
    for(r = 0; r < x; r  )
    {
        for(c = 0; c < y; c  )
        {
            if(isprime(a[r][c]))
            {
                    count  ;
            }
        }
    }
    return count;
}

int main()
{
    int n, m, i, j, z;
    printf("Enter the Number of Rows: ");
    scanf("%d", &n);
    printf("\n");
    printf("Enter the Number of Columns: ");
    scanf("%d", &m);
    printf("\n");

    int a[n][m];
    printf("Enter the elements of the array: \n");
    for(i = 0; i < n; i  )
    {
        for(j = 0; j < m; j  )
            scanf("%d", &a[i][j]);
    }
    z = primecount(n, m, a);
    printf("\n");
    printf("The Number of Prime Numbers in the array is: %d", z);
    printf("\n");

    return 0;
}

Its output might look like

Enter the Number of Rows: 2
Enter the Number of Columns: 2
Enter the elements of the array: 1 2 3 4

The Number of Prime Numbers in the array is: 2
  • Related