C 编程测验 for 循环内的平均数组

标签 c arrays

我遇到了一个问题。我正在尝试找出该程序作业中测验的平均值。这是我的代码。粗体代码就是我的问题所在。

 #include <stdlib.h>
 #include <stdio.h>
 #define STUDENTS 4
 #define QUIZZES 5

 main () {

int quizScores [STUDENTS] [QUIZZES] = {
    { 90, 90, 90, 90, 90 },
    { 90, 80, 70, 60, 50 },
    { 90, 89, 88, 87, 86 },
    { 90, 85, 80, 75, 70 }
 };
 int studentTotal = 0, quizTotal, row, col;
 double studentAverage, quizAverage;

 for ( row = 0; row < STUDENTS; row++) {
     studentTotal = 0;
     for ( col = 0; col < QUIZZES; col++) {
         studentTotal += quizScores[row][col];
     }
     studentAverage = (double) studentTotal / QUIZZES;
     printf("Student %i has average %.2lf\n", row, studentAverage);
 }

 **for ( col = 0; col < QUIZZES; col++) {
     quizTotal = 0;
     for ( row = 0; row < STUDENTS; row++) {
         quizTotal += quizScores[col][row];  
     }
     quizAverage = (double) quizTotal / STUDENTS;
     printf("Quiz %i has an average %.2lf\n",col, quizAverage);
     // output the average for this quiz
 }**

system("pause");
}

最佳答案

您的索引混合在 quizTotal += quizScores[col][row]; 中。它应该是quizTotal += quizScores[row][col];

关于C 编程测验 for 循环内的平均数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12792541/

相关文章:

c - 如何将数组的每个元素与同一数组的其他元素相加?

arrays - 如何在cassandra中存储多维数组

jQuery.Ajax 对数组数据发出错误的请求

c++ - 如何在 Go 代码中捕获 C/C++ lib 异常

C - "Run-Time Check Failure #2 - Stack around the variable ' str' 已损坏。”

c++ - 异常与 errno

c - 用C中的函数定义常量

c++ - 为什么 C++ 中的字符数组返回它的项而不是它的地址?

arrays - 删除 Swift 数组中元素的最后一次出现

c# - 声明一个长常量字节数组