# Thread: Average of user input in a loop problem

1. ## Average of user input in a loop problem

Anyone please help me know what I'm I doing wrong, I been trying to figure it out literally for the past 3 hours.

Here's the problem:
Write a C program to read positive integers, until the value -1 is read and print the average of the values.

This is my code, the average is off for some reason:

Code:
```#include <stdio.h>
int main (void)
{

int num,sum=0;
double avg;

printf("Type a positive number");
scanf("%d",&num);

for(num;num>=-1;num--)
{
printf("%d\n",num);
sum=sum+num;
}

avg= sum/(num+1);
printf("\n\nThe average is:%d\n",avg);

getch();
}```

2. Integer arithmetic essentially truncates any fractional result. Try:
Code:
`avg= (double)sum/(num+1);  // make sum a double to ensure division is done with floating-point math`
Now that I look at it, your loop control is completely screwed up too.

3. I tried it but still the average is not right...
My loop control is screwed up? in where? ;o

4. Your loop condition is wrong. Actually, the choice of a for loop is inappropriate here; use a while loop (or do while loop) instead.

5. "num" is -2 after exiting the 'for' loop - I'm assuming that this is not what you wanted

Code:
```avg= sum/(num+1);
= sum/(-2+1);
= sum/-1;```

6. How can I fix the value of num so it's what I typed into scanf?

I just need to know that and the avg will work, anyone please help if you can.

7. You shouldn't "fix" the value of num. Rather, you should be reading into num in the body of the loop, exiting the loop when -1 is read.

8. I feel so stupid but isn't that's what I was doing? I made that for loop from n to -1 then it exited. Please give me a hint on the code in where should I correct it.

9. You want the user to input something again and again until he hits -1.So he is going to type something again and again....So you did well for using a loop.Inside the loop you should do all the procedure for the user..
Every time,the problem says,you read the input and if it is -1,then you should exit the loop

10. Originally Posted by tadm123
I feel so stupid but isn't that's what I was doing?
No, you read into num exactly once, even if -1 was not the first input.

Originally Posted by tadm123
I made that for loop from n to -1 then it exited.
Your requirements state:
Write a C program to read positive integers, until the value -1 is read and print the average of the values.
It does not state:
Write a C program to read a positive integer. Loop from that positive integer until -1, decrementing the value on each iteration.

Therefore, you must keep reading in the body of the loop. Failure to do so is definitely a mistake.

11. I see, I misunderstood the question then. I thought it was the 2nd option ^

So using arrays is necessary then to solve this problem ?

12. No, an array is not necessary. Just use your two variables, num and sum similar to what you did in your first post. Just make the changes to your actual loop as suggested. Use a while loop, put your input into that loop. Be sure you exit the loop before you "add" your sentinel (-1) to your sum. Compute the average after you exit the loop.

Jim

Popular pages Recent additions