First, you have to declare the elements variable in main():
int elements = 7;
or even better:
int elements = sizeof(percentages) / sizeof(*percentages);
which computes the array size automatically.
Then, don't modify the elements variable in you loop.
If you do it, each time the current maximum is changed, you program your loop to stop after the next element, potentially missing the real maximum which can be located at the end of the array.
Then, like Bob said, you should return the index of the maximum, this way, you can retrieve the corresponding sampleSize, or year.
When you do years[high], you're using a percentage as an array index which can take any real value in [0,100], instead of an index which must stay in {0, 1, 2, 3, 4, 5, 6}.
What's more, you store high in an integer variable, resulting in a truncation of it's value. You may prefer to store it as a double variable.
So findHighNumber() can become:
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;
}
The main() function now can become something like:
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;
}
Then, some small advices:
- putting a prototype of
findHighNumber() is not needed, just define the function above the places where it's needed. This way, when you really have to put a prototype for a function, it a hint saying that you were forced to do so, (for example for mutually recursive functions), plus it shortens the code.
you should define a, for example, struct autism_sample like that:
struct autism_sample {
int year;
int sample_size;
double percentage;
};
This way, you just have to define one array:
struct autism_sample autism_samples[] = {
{
.year = 2000,
.sample_size = 187761,
.percentage = 6.7,
},
{
.year = 2002,
.sample_size = 444050,
.percentage = 6.6,
},
...
};
This way, your data is organized in a more logical way, less error-prone to maintain and you gain the choice, in the implementation of findHighNumber(), to return either the index of the maximum or directly, a pointer to the element holding the maximum, the index becomes then useless.
What's more, it's easier to (un)serialize...
indexof the max value, along the value itself, and return it. I would also rename the function tofindIndexOfMaxto make it obvious what it should be doing.