I need to write a program (using C) which receives an unknown amount of numbers (last one would be -1). The program calculates the amount of numbers which are higher than the total average. Now the main problem is, that we're not allowed to use any sort of lists,stacks,arrays or recursions. We can use loops though, that's what this is all about. This is our third lesson and we have absolutely no idea how to use the rest of the options, nor we're allowed to. The teacher wants us to practice loops as much as possible, hence we were given this task.
I'd be very glad if any of you could give me a hand with this, I've been smashing my head against the wall the whole day, trying to figure it out. I have absolutely no idea how to even begin writing my code, absolutely none ideas. All I need is a way or explanation, it is very logical and a bit hard for me.
Thank you very much.
Here's a brief example:
For the next input -
1 2 3 4 5 -1
The output shout be 2, because only 4 and 5 are higher than the average, which is 3 in our case.
I can't find any way to store the numbers without using stuff which I'm not supposed to use.
I'd be very glad to be given a tip on how to get to this or anything like that, there's nothing much I can add to this post so please refrain from locking it once again, I really need some help with this.
This is what I came with so far:
#include <stdio.h>
void main()
{
int sum = 0, idx, num;
int count = 0;
for(scanf("%d", &num),idx=1;num>0;scanf("%d", &num),idx++)
(num>sum/idx)?(count++,sum+=num):(sum+=num);
printf("%d", count);
}
That's not how it should be done though. It calculates the avg in each step within my code, that's not the real solution.
Any ideas?

New Topic/Question
Reply



MultiQuote








|