输入5个整数,找出其中最大整数和最小整数所在的位置,并将二者对调,然后输出调整后的5个数。
#include<stdio.h>
main()
{
int arr[5], max, min, i, j, k;
printf("Please enter five integers:\n");
for(i=0; i<5; i++)
scanf("%d", arr[i]);
min = arr[0];
for(i = 0; i < 5; i++)
if(arr[i] < min)
{
min = arr[i];
j = i;
}
max = arr[0];
for(i = 0; i < 5; i++)
if(arr[i] > max)
{
max = arr[i];
k = i;
}
arr[j] = max;
arr[k] = min;
printf("The index of minimum value is: %2d\n", k);
printf("The index of maximum value is: %2d\n", j);
for(i = 0; i < 5; i++)
printf("%5d", arr[i]);
printf("\n");
}
转载于:https://blog.51cto.com/11585815/1783914