C 使用函数查找数组中的最大值

C 使用函数查找数组中的最大值,c,arrays,function,C,Arrays,Function,有人能解释一下我做错了什么吗。我需要使用一个数组来查找百分比数组的最大值,并在相应的年份数组中显示该金额和年份 #include <stdio.h> #include <stdlib.h> #include <ctype.h> int findHighNumber (double percentages[], int elements); int main (void) { int index; int elements; int yea

有人能解释一下我做错了什么吗。我需要使用一个数组来查找百分比数组的最大值,并在相应的年份数组中显示该金额和年份

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int findHighNumber (double percentages[], int elements);

int main (void)
{

  int index;
  int elements;


  int years[] = {2000, 2002, 2004, 2006, 2008, 2010, 2012};
  double percentages[] = {6.7, 6.6, 8, 9, 11.3, 14.7, 14.6};
  int sampleSizes[] = {187761, 444050, 172335, 308038, 337093, 1000, 346978};


   int  high = findHighNumber(percentages, elements);
   printf ("%i had the highest percentage with autism at %.2lf%%.\n", years[high], percentages[high]);


   return 0;
}

 int findHighNumber (double percentages[], int elements)
 {
    int index, high;
    high = percentages[0];

    for (index = 1; index < elements; index++)
       if (high < percentages[index])
       {
        high = percentages[index];
    }
   return high;
 }
#包括
#包括
#包括
int findHighNumber(双百分比[],int元素);
内部主(空)
{
整数指数;
int元素;
国际年[]={2000、2002、2004、2006、2008、2010、2012};
双百分比[]={6.7,6.6,8,9,11.3,14.7,14.6};
int采样[]={187761、444050、172335、308038、337093、1000、346978};
int high=findHighNumber(百分比、元素);
printf(“%i患有孤独症的比例最高,为%.2lf%.\n”,年[高],百分比[高];
返回0;
}
int findHighNumber(双百分比[],int元素)
{
int指数高;
高=百分比[0];
对于(索引=1;索引<元素;索引++)
if(高<百分比[指数])
{
高=百分比[指数];
}
高回报;
}

我不确定这是否是一个打字错误,但它是在打电话时出现的

 int  high = findHighNumber(percentages, elements);
这里没有定义变量
元素
。这就是说,您有另一个语法错误(缺少结尾
}

if(高<百分比[指数])
{
高=百分比[索引];///在此之后。

最后,在
findHighNumber()
中,
元素是函数的本地元素,因此,
elements=index+1;
的唯一用法是无用的。

我不确定这是否是一个输入错误,但它在调用时出现

 int  high = findHighNumber(percentages, elements);
这里没有定义变量
元素
。也就是说,附近有另一个语法错误(缺少结尾
}

if(高<百分比[指数])
{
高=百分比[索引];///在此之后。

最后,在
findHighNumber()
中,
元素是函数的本地元素,因此,
elements=index+1;
的唯一用法是无用的。

考虑到存储和使用数据的方式,应该返回较高元素所在的索引,而不是其值。
此外,在传递未初始化的值时,必须传递正确大小的数组(并且不要在函数内部更改它)。

根据存储和使用数据的方式,应该返回较高元素所在的索引,而不是其值。
此外,在传递未初始化的值时,必须传递正确大小的数组(并且不要在函数内部更改它)。

您应该发送索引而不是值

int findIndexOfMax (double percentages[], int elements) {

    int index = 0;
    int highIndex = 0;
    double high = 0.0;
    high = percentages[highIndex];

    for (index = 1; index < elements; index++) 
    {
        if (high < percentages[index]) 
        {
            high = percentages[index];
            highIndex = index;
        }       
    }

    return highIndex;
}
int findIndexOfMax(双百分比[],int元素){
int指数=0;
int-highIndex=0;
双高=0.0;
高=百分比[高指数];
对于(索引=1;索引<元素;索引++)
{
if(高<百分比[指数])
{
高=百分比[指数];
高指数=指数;
}       
}
回归高指数;
}

您应该发送索引而不是值

int findIndexOfMax (double percentages[], int elements) {

    int index = 0;
    int highIndex = 0;
    double high = 0.0;
    high = percentages[highIndex];

    for (index = 1; index < elements; index++) 
    {
        if (high < percentages[index]) 
        {
            high = percentages[index];
            highIndex = index;
        }       
    }

    return highIndex;
}
int findIndexOfMax(双百分比[],int元素){
int指数=0;
int-highIndex=0;
双高=0.0;
高=百分比[高指数];
对于(索引=1;索引<元素;索引++)
{
if(高<百分比[指数])
{
高=百分比[指数];
高指数=指数;
}       
}
回归高指数;
}

首先,必须在
main()
中声明
元素
变量:

或者更好:

int elements = sizeof(percentages) / sizeof(*percentages);
它会自动计算数组大小

然后,不要修改循环中的
元素
变量。 如果执行此操作,则每次更改当前最大值时,都会将循环编程为在下一个元素之后停止,这可能会丢失可位于数组末尾的实际最大值

然后,正如Bob所说,您应该返回最大值的索引,这样,您可以检索相应的样本大小或年份。
当你做
years[high]
时,你使用的是百分比作为数组索引,它可以取[0100]中的任何实际值,而不是必须保持在{0,1,2,3,4,5,6}中的索引

此外,您将
high
存储在整数变量中,导致其值被截断。您可能更喜欢将其存储为变量

因此
findHighNumber()
可以变成:

    int findHighNumber(double percentages[], int elements)
    {
            int index;
            double high;
            int high_index;

            high_index = 0;
            high = percentages[high_index];

            for (index = 1; index < elements; index++)
                    if (high < percentages[index]) {
                            high = percentages[index];
                            high_index = index;
                    }

            return high_index;
    }
然后,一些小建议:

  • 不需要放置
    findHighNumber()
    的原型,只需在需要的位置上方定义函数即可。这样,当你真的必须为函数放置原型时,它会提示你是被迫这样做的(例如,对于相互递归的函数),并且会缩短代码
  • 您应该定义一个,例如,
    struct\u sample
    ,如下所示:

    struct autism_sample {
            int year;
            int sample_size;
            double percentage;
    };
    
    这样,您只需定义一个数组:

    struct autism_sample autism_samples[] = {
        {
            .year = 2000,
            .sample_size = 187761,
            .percentage = 6.7,
        },
        {
            .year = 2002,
            .sample_size = 444050,
            .percentage = 6.6,
        },
        ...
    };
    
    通过这种方式,您的数据以更合理的方式组织,不易维护错误,并且在findHighNumber()的实现中,您可以选择返回最大值的索引,或者直接返回一个指向包含最大值的元素的指针,这样索引就变得无用了。
    更重要的是,它更容易(取消)序列化


  • 首先,您必须在
    main()
    中声明
    元素
    变量:

    或者更好:

    int elements = sizeof(percentages) / sizeof(*percentages);
    
    它会自动计算数组大小

    然后,不要修改循环中的
    元素
    变量。 如果执行此操作,则每次更改当前最大值时,都会将循环编程为在下一个元素之后停止,这可能会丢失可位于数组末尾的实际最大值

    然后,正如Bob所说,您应该返回最大值的索引,这样,您可以检索相应的样本大小或年份。
    当您使用
    年[high]
    时,您使用的是<