1 #include <stdio.h> 2 #define ARR_SIZE 10 3 void MaxMinExchang(int *a, int n);//加了函数声明 4 int main() { 5 int a[ARR_SIZE], i, n; 6 printf("Input n(n<=10): "); 7 scanf("%d", &n); 8 printf("Input %d Numbers: ", n); 9 for (i=0; i<n; i++) { 10 scanf("%d", &a[i]); 11 } 12 MaxMinExchang(a, n); 13 printf("After MaxMinExchange: "); 14 for (i=0; i<n; i++) { 15 printf("%d ", a[i]); 16 } 17 printf(" "); 18 return 0; 19 } 20 void MaxMinExchang(int *a, int n) { 21 int maxValue = a[0], minValue = a[0], maxPos=0, minPos=0;//给了初值 22 int i, temp; 23 for (i=0; i<n; i++) { 24 if (a[i] > maxValue) { 25 maxValue = a[i]; 26 maxPos = i; 27 } 28 if (a[i] < minValue) { 29 minValue = a[i]; 30 minPos = i; 31 } 32 } 33 temp = a[maxPos]; 34 a[maxPos] = a[minPos]; 35 a[minPos] = temp; 36 }