qfedu-c-level/day8/homework/h3.c

25 lines
700 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 设计函数,接收一个数组和数组大小,返回数组中最大的元素。 【要求】函数的数组参数使用指针
#include <stdio.h>
int max(int *arr, int size); // 声明函数
int main(void)
{
int arr[] = {100, 23, 31, 48, 51, 6532, 7};
int size = sizeof(arr) / sizeof(arr[0]);
printf("max = %d\n", max(arr, size));
return 0;
}
int max(int *arr, int size)
{
int max = arr[0]; // 假设第一个元素最大
for (int i = 1; i < size; i++) // 从第二个元素开始遍历
{
if (arr[i] > max) // 如果当前元素大于max则更新max
{
max = arr[i];
}
}
return max; // 返回最大值
}