# Thread: Array problem averaging numbers

1. ## Array problem averaging numbers

Hi guys, I'm new on here and iv searched through the forum for something to fix my problem but alas i havnt found anything!I am trying to write a program using arrays and loops to determine the average of a set of numbers and then comparing each individual value of the array to the average to see which is greater. I have attached comments to say what happens when it compiles.

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

int main ()

{

int i, array[10]={0,1,2,3,4,5,6,7,8,9,};

i= 0;

float average,sum;

average=sum=0.0;

while(i<10)
{

sum=sum+array[i];

i++;
}

average=sum/i;

printf("The average is %f\n", average);

i=0;

while (i<10)
{
if (array[i]> average){

printf("%d\n is greater than average %f", array[i],average);

i++;

}

if	(array[i]< average){

printf("%d\n is less than average %f", array[i],average);

i++;
} /*when compiled i get (a blank space) is less than the average 4.5 and so on */
}

return 0;

}```

2. Originally Posted by Thedon
when compiled i get (a blank space) is less than the average 4.5 and so on
You are probably just misinterpreting your output because you insert a newline in a strange place. Try changing this:
Code:
`printf("%d\n is less than average %f", array[i],average);`
to:
Code:
`printf("%d is less than average %f\n", array[i], average);`
similiarly, change the printf for "greater than".

By the way, you should indent your code properly. It makes it easier to read and hence understand your program.

3. Thank you for your help although it hasnt seem to change the outcome. I have indented my code to what i hope is better if it makes it easier to read!

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

int main ()

{

int i, array[10]={0,1,2,3,4,5,6,7,8,9,};

i= 0;

float average,sum;

average=sum=0.0;

while(i<10)
{

sum=sum+array[i];

i++;
}

average=sum/i;

printf("The average is %f\n", average);

i=0;

while (i<10)
{
if (array[i]>average){

printf("%d is greater than average %f", array[i],average);

i++;
}

if	(array[i]<average){

printf("%d is less than average %f", array[i],average);

i++;
} /*when compiled i get (a blank space) is less than the average 4.5 and so on */

}

return 0;

}```

4. Better, but here is how I might format your code:
Code:
```#include <stdio.h>

int main()
{
int i, array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,};
float average, sum;
average = sum = 0.0;

i = 0;
while (i < 10)
{
sum = sum + array[i];
i++;
}
average = sum / i;
printf("The average is %f\n", average);

i = 0;
while (i < 10)
{
if (array[i] > average) {
printf("%d is greater than average %f\n", array[i], average);
i++;
}

if (array[i] < average) {
printf("%d is less than average %f\n", array[i], average);
i++;
}
}

return 0;
}```
I have taken the liberty of moving the first i = 0; to just before the while loop that uses it. Also, I have added in the newlines as I demonstrates to make the output more readable. Other than that, your logic is entirely the same. Compile and run the program. You might notice something strange. Trace through the flow of control in your program to find out why it happens.

In particular, observe when you increment i in the second while loop. Are you doing it at the right place?

5. I have done all that you said and the program works fine!im pretty sure the second i is in the right place because it needs to be reset for the second loop. Thanks so much for your help!also as regards my indenting i have been losing marks all semester from it but you are the first to properly help me with it so thank you!

Also can you tell me how to edit this thread title so its solved?

6. Originally Posted by Thedon
im pretty sure the second i is in the right place because it needs to be reset for the second loop.
I get an extra line of output because of that. The thing is, you only need to increment once in the loop, hence you should write:
Code:
```    while (i < 10)
{
if (array[i] > average) {
printf("%d is greater than average %f\n", array[i], average);
}

if (array[i] < average) {
printf("%d is less than average %f\n", array[i], average);
}
i++;
}```

7. Yeah i did that tidied up the program a bit too thanks!