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

时间:2017-03-15 07:02:46

标签: 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 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;
 }

5 个答案:

答案 0 :(得分:2)

我不确定这是否是拼写错误,但在调用

时出现
 int  high = findHighNumber(percentages, elements);

您没有定义变量elements。也就是说,您在

附近有另一个语法错误(错过了结束}
if (high < percentages[index])
       {
        high = percentages[index];  ///after this.

最后,在findHighNumber ()内,elements是函数的本地,因此,用作elements = index+1;是无用的。

答案 1 :(得分:2)

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

int elements = 7;

甚至更好:

int elements = sizeof(percentages) / sizeof(*percentages);

自动计算数组大小。

然后,不要修改循环中的elements变量。 如果这样做,每次更改当前最大值时,都会将循环编程为在下一个元素之后停止,可能会错过可以位于数组末尾的实际最大值。

然后,就像Bob说的那样,你应该返回最大索引,这样你就可以检索相应的sampleSize或year。
执行years[high]时,您使用百分比作为数组索引,可以在[0,100]中取任何实际值,而不是必须保留在{0的索引, 1,2,3,4,5,6}。

更重要的是,您将high存储在整数变量中,从而导致其值被截断。您可能更愿意将其存储为 double 变量。

所以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;
    }

main()函数现在可以变成:

    int main (void)
    {
            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,
                    46978};
            int elements = sizeof(percentages) / sizeof(*percentages);
            int high_index = findHighNumber(percentages, elements);

            printf ("%i had the highest percentage with autism at %.2lf%% (sample"
                    "size was %d).\n", years[high_index], percentages[high_index],
                    sampleSizes[high_index]);

            return 0;
    }

然后,一些小建议:

  1. 不需要放置findHighNumber()的原型,只需将函数定义在需要它的地方之上。这样,当你真的必须为一个函数放置一个原型时,它会提示你被迫这样做(例如,对于相互递归的函数),加上它会缩短代码。 / LI>
  2. 你应该像这样定义一个struct autism_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()的实现中获得选择,以返回最大索引或直接返回,指向如果元素保持最大值,那么索引就变得无用了 更重要的是,(联合国)序列化更容易......

答案 2 :(得分:1)

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

答案 3 :(得分: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;
}

答案 4 :(得分:0)

您返回错误的值...您不应该返回最高值,但您应该返回它的索引。

或者您只需在您正在呼叫的功能中打印所需的结果。

另外,我没有看到你已经初始化了变量元素。