Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 获取最大值的程序出错_C - Fatal编程技术网

C 获取最大值的程序出错

C 获取最大值的程序出错,c,C,我试图写一个程序,以获得最大值,但它不工作。该计算在名为max\u number的函数中执行 错误是什么 #include <stdio.h> int max_number(int storeX[], int i) { int max=0,x; for(x=0;x<i;x++) { if(storeX[x]<max) { max = storeX[x]; } r

我试图写一个程序,以获得最大值,但它不工作。该计算在名为
max\u number
的函数中执行

错误是什么

#include <stdio.h>
int max_number(int storeX[], int i)
{
    int max=0,x;
    for(x=0;x<i;x++)
    {
        if(storeX[x]<max)
        {
            max = storeX[x];
        }
        return max;
    }
    return 0;
}

int main()
{
    int i,x,numbers,max;
    printf("how many numbers do you want to compare?\n");
    scanf("%d",&i);
    int storeX[i];
    for(x=0;x<i;x++)
    {
        printf("the %d number is:",x+1);
        scanf("%d",&numbers);
        numbers=storeX[x];
    }
    max=max_number(storeX,i);
    printf("the max number is: %d",max);
    return 0;
}
#包括
int max_编号(int storeX[],int i)
{
int max=0,x;

对于(x=0;x代码中存在一些问题,我们已对代码进行了更改以使其正常工作。请检查代码中的注释以理解

#include <stdio.h>
#include <limits.h>

int max_number(int storeX[], int i)
{
    int max=INT_MIN,x;   //In case you array has all negative numbers
    for(x=0;x<i;x++)
    {
        if(storeX[x]>max)  // This if condition is wrong
        {
            max = storeX[x];
        }
        //return max;        // You need to iterate the whole array
    }
    return max;
}

int main()
{
    int i,x,numbers,max;
    printf("how many numbers do you want to compare?\n");
    scanf("%d",&i);
    int storeX[i];
    for(x=0;x<i;x++)
    {
        printf("the %d number is:",x+1);
        scanf("%d",&storeX[x]);  // Need to pass a reference to the array index
        //numbers=storeX[x];    //dosen't assign the value to storeX[x] instead the otherway around
    }
    max=max_number(storeX,i);
    printf("the max number is: %d\n",max);
    return 0;
}
#包括
#包括
int max_编号(int storeX[],int i)
{
int max=int_MIN,x;//如果数组中有所有负数
for(x=0;xmax)//如果条件错误,则为
{
max=storeX[x];
}
//return max;//需要迭代整个数组
}
返回最大值;
}
int main()
{
整数i,x,数字,最大值;
printf(“要比较多少个数字?\n”);
scanf(“%d”、&i);
int storeX[i];

对于(x=0;xy),您告诉
scanf
将它捕获的数字存储到
numbers
中,但随后您只需使用
storeX
数组中的一些未初始化数据覆盖它。行为与预期完全一致