Average of user inputs (in a loop)

This is a discussion on Average of user inputs (in a loop) within the C Programming forums, part of the General Programming Boards category; My problem is I'm not sure how to get the program to identify each user input uniquely (in a loop). ...

Average of user inputs (in a loop)

My problem is I'm not sure how to get the program to identify each user input uniquely (in a loop). I suppose it would have been a lot easier if the users input, in this particular program,was fixed and not looped.

I have to get the average of the scores entered in each loop. As you will notice, a negative integer is used to exit the loop. The negative integer is not included in the loop counting or the average.

I dont think you want the "user_input++;" line, as its immediately overwritten in the "scanf" line.

For an average you need two things: how many numbers to include, and the the sum of these numbers. You already have the "how many" part, represented by your "loop_count" variable. Note that if the first number entered by the user in the loop is <= -1, then it will report as loop_count = 2 numbers entered, when they really "entered" 0. So there's that bug.

Next you need a running total. Just declare an "int", initialized to 0, and add each user input to this running total variable. Then do the division to store the average.

Probably more significantly, you are not computing a value for user_input_average. An average of a set of values is normally computed by adding the values up and dividing by the number of values. So you need to keep track of the number of values input and the sum of the values entered.

Just remember that integer division does not result in a float, so some conversion is necessary if you want a floating point result for the average.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

If on the very first iteration the user enters a number <= -1, then loop_count will be 0, and you will be dividing by zero. Also, your second pair of "{}"s doesn't do anything, so you could just as well remove them. Alternatively, I think as someone suggested, you could put the code after the loop into an "if" statement, and only calculate the average if the loop_count > 0.

If you want further help, post your complete (100% complete) code, as well as what your input and output is. Describe any other details we must know in order to help. If you leave any one of these out, it will only be hurting you because the help will be delayed until we get all of that information.

If you want further help, post your complete (100% complete) code, as well as what your input and output is. Describe any other details we must know in order to help. If you leave any one of these out, it will only be hurting you because the help will be delayed until we get all of that information.

Well...The below code works just fine, but when I implement the advised code, that would just loop until I break it off...

Does that even compile? If you are trying to negate the statement, don't put the "!" in the middle or whatever, put it at the start, like

Code:

}while( !(scanf("%d", &user_input)<=-1) );

Also,

Code:

}while(scanf("%d", &user_input)>=0);

will "almost always" be true. "scanf" returns the number of items read in, which I think will always be ">= 0", except in the "rare" case of an actual error. So this statement, for "practical" purposes will always be true. Since you're reading in 1 item, you maybe want to compare it to "==1".

In my opinion, I'd choose to do it the way you initially were (reading in "user_input" then comparing that in your while loop condition). It's much more readable, and the extremely small space/cost efficiency gain you get from doing it the new way your trying to do is certainly not worth the added "complexity".

Try and fix these things, then if you still have problems post your exact code you're running, so we simply have one piece of code to work on.