以函数形式实现数组查找最大值的函数,并在主函数中调用 函数 int *findlargest(.)

如题所述

#include <stdio.h>
#define M 40
int MAX(int *arr, int arr_size)
{
int i = 0;
int max = *arr;
for (i = 1; i < arr_size; i++)
{
if (*(arr+i) > max)
{
max = *(arr+i);
}
}
return max;
}
int main(void)
{
int i = 0;
int array[M]={0};

printf("please input a array: \n");
for (i = 0; i < M; i++)
{
scanf("%d", &array[i]);
}
printf("The max num is: %d\n", MAX(array, M));
return 0;

}

或者:

#include <stdio.h>
#define M 40
void MAX(int *arr, int arr_size, int *max)
{
*max = *arr;
while (arr_size-- >= 1)
{
if (*arr > *max)
{
*max = *arr;
}
arr++;
}
}
int main(void)
{
int i = 0;
int array[M]={0};
int max = 0;

printf("please input a array: \n");
for (i = 0; i < M; i++)
{
scanf("%d", &array[i]);
}
MAX(array, M, &max);
printf("The max num is: %d\n", max);
return 0;
}
温馨提示:答案为网友推荐,仅供参考