2023自学中 2023-10-08 08:45 采纳率: 40%
浏览 6
已结题

输入读取10高尔夫分数,打印总分平均分差分。把3个for循环合并成1个for循环


#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;
}
  • 写回答

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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月17日
  • 已采纳回答 10月9日
  • 修改了问题 10月8日
  • 修改了问题 10月8日
  • 展开全部