1 #include <stdio.h> 2 3 void max_min(int a[], int n, int *pmax, int *pmin); 4 int main() 5 { 6 int i = 0; 7 int arr[100] = {0}; 8 int num = 0; 9 int max = 0; 10 int min = 0; 11 printf("请输入需要比较的数:"); 12 for(i=0;i<10;i++) 13 { 14 scanf("%d",arr+i); 15 num ++; 16 if(getchar()==' ') 17 { 18 break; 19 } 20 } 21 printf("输入的数据个数是:%d ", num); 22 max_min(arr, num, &max, &min); 23 printf("max=%d min=%d ", max, min); 24 return 0; 25 26 } 27 28 void max_min(int a[], int n, int *pmax, int *pmin) 29 { 30 int j = 0; 31 int max = 0; 32 int min = 0; 33 max = a[0]; 34 min = a[0]; 35 for(j=1;j<n;j++) 36 { 37 if(max<a[j]) 38 { 39 max = a[j]; 40 } 41 if(min>a[j]) 42 { 43 min = a[j]; 44 } 45 } 46 *pmax = max; 47 *pmin = min; 48 }
执行结果:
momo@momo ~/myfile/mydev/cc
% compare !2150
请输入需要比较的数:1 2 8 9 21
输入的数据个数是:5
max=21
min=1