用c语言输出:请定义一个函数 int findMax(int arr[], int size),该函数接收一个整型数组和数组大小,返回数组中的最大值。在main函数中定义一个长度为5的数组,并从键盘输入数组中的元素
时间: 2024-12-20 12:15:51 浏览: 69
在C语言中,你可以定义一个名为`findMax`的函数,它接收一个整数数组`arr`和数组的大小`size`,然后遍历数组找到最大值。在`main`函数中,我们可以创建一个长度为5的数组,通过循环从用户那里获取输入,并调用`findMax`函数来找出最大值。以下是完整的示例代码:
```c
#include <stdio.h>
#include <limits.h> // 引入limits.h库以获得INT_MAX
// 定义函数 findMax
int findMax(int arr[], int size) {
int max = INT_MIN; // 初始假设最小值
for (int i = 0; i < size; i++) {
if (arr[i] > max) {
max = arr[i]; // 更新最大值
}
}
return max;
}
int main() {
const int arraySize = 5;
int arr[arraySize];
// 提示用户输入数组元素
printf("Enter 5 integer values for the array:\n");
for (int i = 0; i < arraySize; i++) {
scanf("%d", &arr[i]);
}
// 调用 findMax 函数并打印结果
int maxValue = findMax(arr, arraySize);
printf("The maximum value in the array is: %d\n", maxValue);
return 0;
}
```
在这个程序中,用户会被提示输入五个整数,然后`findMax`函数会计算并返回这些数字中的最大值。
阅读全文
相关推荐


















