求大佬解答c语言问题
输入一个正整数n(1<n≤10),再输入n个整数存放到数组a中,将最大值与第一个数交换,然后输出交换后的n个数,要求附运行结果截图。
输入输出示例(先输入n为7,再输入7个数,程序实现交换后输出)
输入:
7
5 7 2 8 9 3 1
输出:After swapped:9 7 2 8 5 3 1
#include <stdio.h> #define N 50 int main(void) { int n = 0; int a[N] = {0}; int max = 0; int temp, index; printf("Please you input the number: "); scanf("%d", &n); for(size_t i = 0; i != n; ++i){ scanf("%d", &a[i]); } for(size_t i = 0; i != n; ++i){ if(a[i] > max){ max = a[i]; index = i; } } temp = a[0]; a[0] = a[index]; a[index] = temp; for(size_t i = 0; i != n; ++i){ printf("%d\t", a[i]); } return 0; }