# Thread: Program to determine whether numbers in an array are higher or lower

1. ## Program to determine whether numbers in an array are higher or lower

Program is supposed to put out messages saying this number is x lower than the average. This number is x larger than the average.

The value 1.00000 is 44.000000 smaller than the average (should be 4.50000000. Even for the larger numbers for eg value 8 it puts out that it is 36.000000 smaller despite the fact it is 4.5 bigger. What have I done wrong?

Code:
```#include <stdio.h>

void main()

{

float list[10];
int count = 0;
float difference = 0.0;
float average;
float value = 0;

list[0]=1.0; list[1]=2.0; list[2]=3.0; list[3]=4.0; list[4]=5.0;
list[5]=6.0; list[6]=7.0; list[7]=8.0; list[8]=9.0; list[9]=0.0;

while (count < 10)
{

value = value + list[count];

count++;

}

average = value/10;

printf("The average value in the array is %f.\n", average);

count=0;

while (count < 10)
{

difference = value-list[count];

if(value > list[count])

printf("The value %f is %f smaller than the average %f.\n", list[count], difference, average);

else

printf("The value %f is %f greater than the average %f.\n", list[count], difference, average);

count++;

}

return;
}```

2. Because you are subtracting from the total ("value"), not the average.

The return type of main() must be int, not void.

3. Thanks, I see that now. Problem is, it doesn't recognise the else anymore (it says I'm using else without a previous if but there is an if there). Syntax?

Code:
```#include <stdio.h>

int main()

{

float list[10];
int count = 0;
float difference = 0.0;
float average;
float value = 0;

list[0]=1.0; list[1]=2.0; list[2]=3.0; list[3]=4.0; list[4]=5.0;
list[5]=6.0; list[6]=7.0; list[7]=8.0; list[8]=9.0; list[9]=0.0;

while (count < 10)
{

value = value + list[count];

count++;

}

average = value/10;

printf("The average value in the array is %f.\n", average);

count=0;

while (count < 10)
{

if(average > list[count])

difference = average-list[count];

printf("The value %f is %f smaller than the average %f.\n", list[count], difference, average);

else

difference = list[count]-average;

printf("The value %f is %f greater than the average %f.\n", list[count], difference, average);

count++;

}```

4. I moved the maths because it was producing x is -4.5000 greater than y etc.

5. Your if else is illegal in C. Learn to indent your code so all the subordinate lines of code are indented by 2 to 5 spaces, and you'll see it immediately.

In C, you can't do this:
Code:
```if(someTest)
do something;
print something;  <<== this is illegal. else statement must go here
else
do something else

But you can do this:
if(someTest)  {
do something;
print something;  <<== this is now legal, inside the if {   }
}
else
do something else```
Which is perhaps, what you intended.

Your if else is illegal in C. Learn to indent your code so all the subordinate lines of code are indented by 2 to 5 spaces, and you'll see it immediately.

In C, you can't do this:
Code:
```if(someTest)
do something;
print something;  <<== this is illegal. else statement must go here
else
do something else

But you can do this:
if(someTest)  {
do something;
print something;  <<== this is now legal, inside the if {   }
}
else
do something else```
Which is perhaps, what you intended.
Perfect! Thank-you!