gpt4 book ai didi

C程序设计语言

转载 作者:太空宇宙 更新时间:2023-11-04 06:21:35 25 4
gpt4 key购买 nike

在我第一次输入后,Codeblocks 要求我提供额外的输入,它提示我输入另一个输入,但没有为每个输入提示打印的打印语句。

#include <stdio.h>
//Computing marks and average of students using 2D arrays
void main()
{
int i,j,sum,marks[3][5];
float avg;
printf("Program to compute average marks of 3 students.\n");
for(i=0;i<3;i++)
{ for(j=0;j<5;j++)
{
printf("Enter marks for student %d in subject %d:\t",i+1,j+1);
scanf("%d ",&marks[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
sum=sum+marks[i][j];
}
avg= sum/5.0;
printf("The average marks of student %d is %f:\n",i+1,avg);
}
getch();
}

最佳答案

代码问题:您的格式字符串,如 scanf("%d ",&marks[i][j]); 在输入后需要空格,这会导致异常。

更正代码:

#include <stdio.h>
//Computing marks and average of students using 2D arrays
int main()
{
int i,j,sum = 0,marks[3][5];
float avg;

printf("Program to compute average marks of 3 students.\n");
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("Enter marks for student %d in subject %d:\t",i+1,j+1);
scanf("%d",&marks[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
sum=sum+marks[i][j];
}
avg= sum/5.0;
printf("The average marks of student %d is %f:\n",i+1,avg);
}
return 0;
}

如规范所述

Number,order and type of conversion specifications must match the number,order and type of parameters in the list

.否则,结果将不可预测,并可能终止输入/输出功能。

关于C程序设计语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33916595/

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