#include<stdio.h>
#define SIZE 10 //10个分数
#define PAR 72 //定义标准分 72
int main(void)
{
int index,score[SIZE];
int sum = 0;
float average; //平均分
printf("Enter %d golf scores:\n",SIZE); //输入10个高尔夫分数
for (index = 0;index < SIZE; index++)
scanf ("%d",&score[index]); //读取10个分数
printf("The score read in are as follows:\n"); //打印输入的分数
for (index = 0;index < SIZE;index++)
printf("%5d",score[index]);
printf("\n");
for (index = 0;index <SIZE ;index ++)
sum +=score[index]; //求总分数
average =(float)sum / SIZE; //平均分=总分数除以10
printf("Sum of scores = %d,average = %.2f\n",sum,average);
printf("That's a handicap of %.0f.\n",average- PAR);
return 0;
}
输入读取10高尔夫分数,打印总分平均分差分。把3个for循环合并成1个for循环
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
瓦贞德食泥鸭 2023-10-08 10:34关注我修改了一下楼下的代码,解决了不能跨行输入的bug
代码如下#include <stdio.h> #define SIZE 10 #define PAR 72 int main() { float avgrage; int sum = 0; int score[SIZE] = {0}; printf("Enter %d scores:",SIZE); for(index = 0;index < 2*SIZE;index++) { if(index < SIZE) { scanf("%d",&score[index]); getchar(); } else { if(index == SIZE) { printf("the score read in are as follows:\n"); } printf("%5d",score[index-SIZE]); sum += score[index-SIZE]; }; } avgrage = sum/(float)SIZE; printf("\n"); printf("Sum of scores: %d,average = %.2f\n",sum,avgrage); printf("That's a handicap of:%.0f",avgrage-PAR); return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录