用C语言解答,输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。要求写3个函数:①输入10个数;②进行处理;③输出10个数。
时间: 2023-06-14 07:03:49 浏览: 148
以下是用C语言编写的程序:
```c
#include <stdio.h>
void input(int arr[]);
void process(int arr[]);
void output(int arr[]);
int get_min_index(int arr[]);
int get_max_index(int arr[]);
int main() {
int arr[10];
input(arr);
process(arr);
output(arr);
return 0;
}
void input(int arr[]) {
printf("请输入10个整数:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
}
void process(int arr[]) {
int min_index = get_min_index(arr);
int max_index = get_max_index(arr);
int tmp = arr[0];
arr[0] = arr[min_index];
arr[min_index] = tmp;
tmp = arr[9];
arr[9] = arr[max_index];
arr[max_index] = tmp;
}
void output(int arr[]) {
printf("处理后的数组为:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
}
int get_min_index(int arr[]) {
int min_index = 0;
for (int i = 1; i < 10; i++) {
if (arr[i] < arr[min_index]) {
min_index = i;
}
}
return min_index;
}
int get_max_index(int arr[]) {
int max_index = 0;
for (int i = 1; i < 10; i++) {
if (arr[i] > arr[max_index]) {
max_index = i;
}
}
return max_index;
}
```
程序中定义了四个函数:`input()`、`process()`、`output()`和`get_min_index()`、`get_max_index()`。其中:
- `input()`函数用于输入10个整数;
- `process()`函数用于进行处理,即将最小的数与第一个数对换,将最大的数与最后一个数对换;
- `output()`函数用于输出处理后的10个数;
- `get_min_index()`和`get_max_index()`函数分别用于获取数组中最小值和最大值的下标。
程序的主函数通过调用这四个函数来完成整个程序的运行。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)