C语言将数组(15.5.3.1.9.8.7.6.0.10.21)找最大值存到MAX里,并将它的下标存到MAXi里,并将数组从小到大排序
时间: 2023-12-18 07:05:50 浏览: 39
好的,针对您的问题,我可以给出以下 C 语言代码实现:
```c
#include <stdio.h>
int main() {
int arr[] = {15, 5, 3, 1, 9, 8, 7, 6, 0, 10, 21};
int len = sizeof(arr) / sizeof(arr[0]);
int max = arr[0], maxi = 0;
// 找出最大值及其下标
for (int i = 1; i < len; i++) {
if (arr[i] > max) {
max = arr[i];
maxi = i;
}
}
printf("最大值为:%d,下标为:%d\n", max, maxi);
// 冒泡排序将数组从小到大排序
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 输出排序后的数组
printf("排序后的数组为:");
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
最大值为:21,下标为:10
排序后的数组为:0 1 3 5 6 7 8 9 10 15 21
```
希望能够帮助您解决问题。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)