[英]C Programming: Using struct Accessing an array within an array during a for loop
[英]c programming quiz average array within for loop
我陷入一個問題。 我正在嘗試查找此程序作業中測驗的平均值。 這是我的代碼。 用粗體顯示的代碼是我的問題所在。
#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[col][row];
。 應該是quizTotal += quizScores[row][col];
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.