gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:54:41 24 4
gpt4 key购买 nike

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

 #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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com