我在获取总分、平均分、最高分和最低分时遇到问题。我在我的程序中找不到问题。我想要一些关于我的代码的帮助。非常感谢所有帮助。我使用 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/