求一个数组中的最大值,并输出C语言
时间: 2024-02-18 18:04:21 浏览: 31
可以使用循环遍历数组中的每个元素,找到其中的最大值,并使用 `printf` 函数输出结果。示例代码如下:
```c
#include <stdio.h>
int main() {
int arr[] = {3, 1, 5, 2, 4};
int n = sizeof(arr) / sizeof(int);
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("数组中的最大值为:%d\n", max);
return 0;
}
```
输出结果为:
```
数组中的最大值为:5
```
相关问题
求一个数组中的最大值C语言
可以使用循环遍历数组中的每个元素,找到其中的最大值。示例代码如下:
```c
#include <stdio.h>
int main() {
int arr[] = {3, 1, 5, 2, 4};
int n = sizeof(arr) / sizeof(int);
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("最大值为:%d\n", max);
return 0;
}
```
输出结果为:
```
最大值为:5
```
练习1:求一维数组的最大值,输出最大值以及位置c语言
好的,以下是求一维数组最大值及其位置的 C 语言代码示例:
```c
#include <stdio.h>
int main() {
int arr[] = {5, 3, 9, 1, 7};
int n = sizeof(arr) / sizeof(arr[0]);
int max_val = arr[0];
int max_idx = 0;
for (int i = 1; i < n; i++) {
if (arr[i] > max_val) {
max_val = arr[i];
max_idx = i;
}
}
printf("最大值为:%d\n", max_val);
printf("最大值位置:%d\n", max_idx);
return 0;
}
```
该程序会输出数组中的最大值以及其在数组中的位置。你可以根据需要修改数组的内容。