gpt4 book ai didi

c - 使用另一个函数查找最大数字

转载 作者:行者123 更新时间:2023-11-30 21:02:41 24 4
gpt4 key购买 nike

请大家帮我看看我的代码有什么问题或者我的代码缺少什么...我们有这个事件,我们必须使用另一个函数找到最大的数字..

#include <stdio.h>
#include <stdlib.h>
#define p printf
#define s scanf
int high (int n1);
int main(int argc, char *argv[])
{
int i, num[10];
p("Input 10 numbers\n");
for (i=0; i<10; i++)
{
p("Enter Number: ");
s("%d",&num[i]);
}
p("Highest Number: %d",high(num[i]));
getch();
}

int high (int n1)
{
int l;
for (l=0; l<n1; l++)
{
if (n1 > l)
return n1;
}
}

当我输入任何数字时,我总是得到 37..

最佳答案

int high (int n1); 应该是

int high (int *arr, int sz);  /* You need to pass an array */

p("最高数字:%d",high(num[i])); 应该是

p("Highest Number: %d",high(num, 10));  /* Passing array now, not one element */

int high() 应重写为:

int high (int *arr, int sz)
{
int l, mx = INT_MIN;
for (l=0; l<sz; l++)
{
if (mx < arr[l])
{
/* Left as an excercise */
}
}
return mx;
}
<小时/>

因为它被标记为 ,我建议使用 available C++ to find max in a range :

const int max = *std::max_element(&num[0], &num[10]); // #include <algorithm>

关于c - 使用另一个函数查找最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28855585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com