我想制作一个程序来对总分进行求和,并通过使用二维数组找到最高分。 我面对一些奇怪的事情没有任何错误。 我发现循环(带有cla,stu变量)似乎被跳过了。 我尝试在每个循环上打印一些东西,但我找不到原因。 我在Windows 7中使用Visual Studio 2017。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define makeScore() (rand() % 60 + 40);
int main() {
int arScore[2][3];
int cla, stu;
int sum[2];
int maxScore = 0;
int maxc = 0, maxs = 0;
int i = 0, j = 0;
for (i = 0; i < sizeof(arScore) / sizeof(arScore[0]); i++) {
for (j = 0; j < sizeof(arScore[0]) / sizeof(arScore[0][0]); j++) {
arScore[i][j] = makeScore();
}
}
for (cla = 0; i < sizeof(arScore) / sizeof(arScore[0]); cla++) {
sum[cla] = 0;
for (stu = 0; j < sizeof(arScore[0]) / sizeof(arScore[0][0]); stu++) {
sum[cla] += arScore[cla][stu];
if (maxScore < arScore[cla][stu]) {
maxScore = arScore[cla][stu];
maxc = cla;
maxs = stu;
}
}
printf("the total score of %d class : %d\n", cla + 1, sum[cla]);
}
puts("the student with the highest score");
printf("%d class, %d student, %d score\n", maxc, maxs, maxScore);
return 0;
}
答案 0 :(得分:0)
你写错了变量,
for (cla = 0; i < sizeof(arScore) / sizeof(arScore[0]); cla++) { // i instead of cla
sum[cla] = 0;
for (stu = 0; j < sizeof(arScore[0]) / sizeof(arScore[0][0]); stu++) {
sum[cla] += arScore[cla][stu]; //j instead of stu
写这个而不是
for (cla = 0; cla < sizeof(arScore) / sizeof(arScore[0]); cla++) {
sum[cla] = 0;
for (stu = 0; stu < sizeof(arScore[0]) / sizeof(arScore[0][0]); stu++) {
sum[cla] += arScore[cla][stu];