定义大小为100的整型数组a,输入正整数n,然后输入n个整数存放到数组a中,求这n个数中的最大值及其下标,最小值及其下标,输出结果,c语言
时间: 2024-03-23 15:40:21 浏览: 13
以下是求解最大值及其下标、最小值及其下标的C语言代码:
```c
#include <stdio.h>
int main() {
int n, a[100], max, min, max_index, min_index;
printf("请输入正整数n:");
scanf("%d", &n);
printf("请输入%d个整数:", n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (i == 0) { // 初始化max、min、max_index、min_index
max = min = a[0];
max_index = min_index = 0;
} else {
if (a[i] > max) { // 更新max、max_index
max = a[i];
max_index = i;
}
if (a[i] < min) { // 更新min、min_index
min = a[i];
min_index = i;
}
}
}
printf("最大值为%d,下标为%d\n", max, max_index);
printf("最小值为%d,下标为%d\n", min, min_index);
return 0;
}
```
示例输出:
```
请输入正整数n:5
请输入5个整数:32 45 12 78 9
最大值为78,下标为3
最小值为9,下标为4
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)