【C语言】(数组方式)输出一组成绩中的最高分与最低分

两种不同方式获取最大值与最小值

代码1:

#include <stdio.h>
int main()
{
    float score[5], sum = 0, max, min;
    int i;
    printf("请输入5名童鞋的成绩:\n");
    for (i = 0; i < 5; i = i + 1)
        scanf_s("%f", &score[i]);
    max = min = score[0];
    for (i = 0; i < 5; i = i + 1)
    {    
        if (max <= score[i])
            max = score[i];
    if (min >= score[i])
        min = score[i];
}
printf("最高分为:%.1f\n",max);
printf("最低分为:%.1f\n", min);
}

代码2:

#include <stdio.h>
int main()
{
    float score[5] ;
    int i,max, min;
    printf("请输入5名童鞋的成绩:\n");
    for (i = 0; i < 5; i = i + 1)
        scanf_s("%f", &score[i]);
    max = min = 0;
    for (i = 0; i < 5; i = i + 1)
    {    
        if (score[max] <= score[i])
            max = i;
    if (score[min] >= score[i])
        min = i;
}
printf("下标为:%d的成绩为最高分:%.1f\n",max,score[max]);
printf("下标为:%d的成绩为最低分:%.1f\n", min,score[min]);
}

 

posted @ 2019-11-23 20:51  木子欢儿  阅读(4990)  评论(0编辑  收藏  举报