【问题标题】:I need help please我需要帮助
【发布时间】:2022-01-04 16:45:55
【问题描述】:

这个程序在运行后给了我数组中的零素数。这是一个程序,它采用 2d 数组 [n x m] 然后计算 2d 数组中有多少个素数。

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;
}

【问题讨论】:

  • 您应该编辑您的标题,使其反映您问题的内容。用“please”代替“plz”。
  • @CostantinoGrana:很好的建议,但技术写作可能也不应该使用这个词——避免使用“请”、“谢谢”和“帮助”。最好的问题只是以第一人称的方式向作者提出:我该怎么做?我该如何解决?等
  • 请编辑您的标题。

标签: c if-statement compiler-errors primes function-definition


【解决方案1】:

对于初学者而不是这个电话

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

你需要写

z = primecount(n, m, a);

在函数isprime的调用中如上所示

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

表达式a[r][c]int 类型的标量对象。但是函数isprime 需要一个二维数组而不是int 类型的标量对象。

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

只需像这样声明函数

int isprime( int x );

并相应地改变它的定义。

函数会被调用

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

注意isprime函数的逻辑不正确。它为等于10 的值返回逻辑真,尽管这些值不是素数。

您还需要处理包含unsigned int 类型元素的数组。否则用户可以输入负值。

这是您的更新程序。

#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;
}

它的输出可能看起来像

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

【讨论】:

  • 你能解释一下我应该在 isprime 函数的逻辑中做什么,因为我不明白这部分,谢谢你的帮助。
  • @KareemTamer 您需要从质数集中排除数字 0 和 1。目前你的函数说这些数字是素数。即 for 循环的条件立即评估为 false 并将控制权传递给语句 return 1;
  • 我已经编辑过了,但这次每次都给我零
  • @KareemTamer 在已有答案的情况下更改问题中的代码并不是一个愚蠢的想法,因为这会使读者对问题和答案感到困惑。您忘记更改函数调用。
  • 对于给您带来的不便,我深表歉意,我只是新来的,我没有想到这一点。我改变了它,但仍然给我零。
猜你喜欢
  • 1970-01-01
  • 2022-01-10
  • 2021-04-25
  • 2016-07-14
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多