最大值和最小值

问题:

  编写程序,从键盘输入num(1—30)个整数,求其中的最大数和最小数,并将结果输出

分析:

  首先输入测试数据的个数num,并判断其合理性,其次输入num个整数,置于一维数组中,然后对各个数组元素比较大小,确定最大数和最小数并输出结果。若单纯为所求输入一批整数的最大数和最小数,不一定需要数组变量,这里仅仅反映数组的一个可能的应用场合。

 1 #include<stdio.h>
 2 #define MAX 30
 3 int main(){
 4     int ia[MAX+1];
 5     int num,count,current;
 6     int maxi,mini;
 7     do{
 8         printf("Enter the number of tested data(1--%d):",MAX);
 9         scanf("%d",&num);
10     }while(!(num>=1&&num<=30));
11     printf("Enter %d integer value",num);
12     if(num>1){
13         printf("s");
14     }
15     printf(":\n");
16     for(count=1;count<=num;count++){
17         scanf("%d",&ia[count]);
18     }
19     maxi=ia[1];
20     mini=maxi;
21     for(count=2;count<=num;count++){
22         current=ia[count];
23         if(current>maxi)
24             maxi=current;
25         else if(current<mini)
26             mini=current;
27     }
28     printf("Max Value=%5d, Min  Value=%5d\n",maxi,mini);
29     return 0;
30 } 

 




posted @ 2018-04-10 22:12  无心小男  阅读(752)  评论(0编辑  收藏  举报