c语言中求数组元素的最大值和最小值

 

1、

#include <stdio.h>
#define NUMBER 5

int main(void)
{
    int i;
    int a[NUMBER];
    
    puts("please input the score of the students.");
    for (i = 0; i < NUMBER; i++)
    {
        printf("%dst = ", i + 1);
        scanf("%d", &a[i]);
    }
    
    int max = a[0];
    int min = a[0];
    for (i = 0; i < NUMBER; i++)
    {
        if(a[i] > max)
            max = a[i];
        if(a[i] < min)
            min = a[i];
    }
    printf("max = %d\n", max);
    printf("min = %d\n", min);
    return 0;
}

 

 

2、

#include <stdio.h>
#define NUMBER 8

int main(void)
{
    int i;
    int a[NUMBER];
    
    puts("please input the score of the students.");
    for (i = 0; i < NUMBER; i++)
    {
        printf("%dst = ", i + 1);
        scanf("%d", &a[i]);
    }
    
    int max = a[0];
    int min = a[0];
    for (i = 0; i < NUMBER; i++)
    {
        if(a[i] > max)
            max = a[i];
        if(a[i] < min)
            min = a[i];
    }
    printf("max = %d\n", max);
    printf("min = %d\n", min);
    return 0;
}

 

posted @ 2021-02-03 23:11  小鲨鱼2018  阅读(3542)  评论(0编辑  收藏  举报