c - 在c编程中获取二维数组内3x4矩阵的总分、平均分、最大分和最小分

标签 c arrays

我在获取总分、平均分、最高分和最低分时遇到问题。我在我的程序中找不到问题。我想要一些关于我的代码的帮助。非常感谢所有帮助。我使用 Dev-C++ 作为我的程序的软件。 无论如何,我唯一的问题是总分、最高分和最低分的代码。

这是我的程序:

int main(void)

  {

    char n[3]={'A','B','C'};
    int s[3][4]={90,50,100,10,60,100,20,50,80,70,100,75};
    double average=0;
    int x, y, total=0, max=0, min=0;

    for(x=0;x<3;x++)
    {
        printf("%c\t",n[x]);
        for(y=0;y<4;y++)
        {
            printf("%d\t",s[x][y]);
        }
        printf("\n");
    }
    for(x=0;x<3;x++)
    {
        for(y=0;y<4;y++)
        {
           total+=s[x][y];
           average=(double)total/n[x];
        }
        printf("\nThe total score of %c is %d with the average score of
%.2lf",n[x],total,average);
    }
    max=s[0][0];
    for(x=0;x<3;x++)
    {
        for(y=0;y<4;y++)
        {
            if(s[x][y]>max)
            max=s[x][y];
        }
        printf("\nThe maximum score of %c is %d",n[x],max);
    }
    min=s[0][0];
    for(x=0;x<3;x++)
    {
        for(y=0;y<4;y++)
        {
            if(s[x][y]<min)
            min=s[x][y];
        }
        printf("\nThe minimum score of %c is %d",n[x],min);
    }
    return 0;
  }

结果是:

A     90     50     100     10
B     60     100    20      50
C     80     70     100     75

The total score of A is 250 with the average score of 3.85
The total score of B is 480 with the average score of 7.27
The total score of C is 805 with the average score of 12.01
The maximum score of A is 100
The maximum score of B is 100
The maximum score of C is 100
The minimum score of A is 10
The minimum score of B is 10
The minimum score of C is 10

最佳答案

您不会重置每行的最小值和最大值,而只会重置每个矩阵,因此它将反射(reflect)这一点。

将初始化移到第一个 for 循环内,它将起作用。

for(x=0;x<3;x++)
{
    max=s[x][0];
    ...

此外,您必须将每行的总计设置为零,并且无需在第二个循环内计算平均值,应该在第二个循环外部计算平均值,因为这将是正确的值。

关于c - 在c编程中获取二维数组内3x4矩阵的总分、平均分、最大分和最小分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35012959/

相关文章:

c - Windows XP 上的 GCC 编译器错误

c - C 中的符号扩展实现

c++ - 简单的 C++ 数组搜索

c - 关于指针数组的指针

algorithm - “Finding Maximum Sum of Subsequent Elements”算法分析

c++ - 如何在 AES_set_encrypt_key 中使用 openssl 中的自定义 key

java - 在 Xpages 中从服务器端 javascript/java 调用 C 代码?

c - 如何避免库查找 CMakeLists 功能

python - 从不同大小的 numpy 数组创建 pandas 数据框

c++ - int 与 int[] 的效率