[英]I need help plz
詳細信息:運行它時它給我一個錯誤它的程序需要二維數組 [nxm] 來計算有多少素數。它給出一個錯誤,memory 無法運行它。
int isprime(int p, int q, int a[p][q]) //function to check prime
{
int k;
for(k = 2; (k <= (a[p][q] / 2)); k++)
{
if(a[p][q] % k != 0)
continue;
else
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(r, c, 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;
}
在此調用 function isprime
if(isprime(r, c, a[r][c]))
表達式a[r][c]
是int
類型的標量 object 。 然而 function isprime
需要一個二維數組,而不是int
類型的標量 object 。
int isprime(int p, int q, int a[p][q])
^^^^^^^^^^^
只需像這樣聲明 function
int isprime( int x );
並相應地改變其定義。
function 將被稱為
if( isprime( a[r][c] ) )
注意function isprime
的邏輯不正確。 它為等於1
和0
的值返回邏輯真,盡管這些值不是素數。
您還需要處理包含unsigned int
類型元素的數組。 否則用戶可以輸入負值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.