请用C++编写输入10个整数,将其中最小的数与第一个数对调,把最大的数与最后一个对调,最后输出对调后新的数组。
时间: 2023-12-04 22:20:10 浏览: 40
以下是C++代码实现:
```c++
#include<iostream>
using namespace std;
int main(){
int arr[10];
int min_index = 0, max_index = 0;
int temp;
// 输入数组
cout << "请输入10个整数:" << endl;
for(int i=0; i<10; i++){
cin >> arr[i];
}
// 找到最小值和最大值的下标
for(int i=1; i<10; i++){
if(arr[i] < arr[min_index]){
min_index = i;
}
if(arr[i] > arr[max_index]){
max_index = i;
}
}
// 交换最小值和第一个数的位置
temp = arr[0];
arr[0] = arr[min_index];
arr[min_index] = temp;
// 交换最大值和最后一个数的位置
temp = arr[9];
arr[9] = arr[max_index];
arr[max_index] = temp;
// 输出新的数组
cout << "交换后的数组为:" << endl;
for(int i=0; i<10; i++){
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
运行结果如下:
```
请输入10个整数:
5 3 8 7 2 6 1 4 9 0
交换后的数组为:
0 3 8 7 2 6 1 4 5 9
```