So I'm currently in my first year in university studying electrical engineering. I'm taking a intro to C course, and I'll say it now, I'm not sure if I'm going to be able to pass the exam. Even with the help of my father I find programming extremely hard. If you give me a code to trace and figure out what's being printed, if it will have any errors, I'm pretty good at doing that. However, when it comes to writing a program from scratch, this is my real struggle. Even if I'm given a set of instructions on how to do it, I still won't be able to do it. For example, I'm doing a past exam that asks me to prompt the user to enter integers until the user enters 0. Spent 3 hours thinking about it to no ado. At this point the program will take all the user inputted values and take an average.

So this is how I thought it out:

  • I need to use a for loop or a while loop to prompt the user to enter an integer.
  • I need to find some way to store the variables the user enters.
  • If the user enters 0, I'll use an if statement to calculate the average, print it out, and break.

So I'm stuck on finding some way to store all the variables and this has stumped me for hours.

closed as unclear what you're asking by DeadMG, Arseni Mourzenko, user40980, Robert Harvey, gnat Nov 27 '14 at 4:39

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You could have two variables - one that holds the sum and one that holds the number of numbers inputted. So, each time a user inputs a number, add the number to the sum variable and increment the variable with the total number of numbers by one. Then, when you get a zero, just divide the sum by the number of numbers and you have the average. – mmk Nov 27 '14 at 0:10
  • Or, you can use some kind of list, since arrays are more difficult to work with because you don't know how many numbers are needed (because then, you would have to do some copying into larger arrays, etc.) – mmk Nov 27 '14 at 0:11
  • Have you sat down and talked to your TA or a tutor? Have you been doing all your assignments (and by this I mean you doing your assignments rather than searching Stack Overflow for some code)? – user40980 Nov 27 '14 at 1:15

An important way of learning programming (and many other things) is by reading the work of others, especially masters.

Equally important to programming is to do what you have been asked to do and nothing else.

So, if the questions asks

prompt the user to enter integers until the user enters 0.

then that's all you should do. The first cut of the logic might look something like

output the prompt
get the number
if number not zero then loop

So what tools or pieces do we have to build this from?

Maybe you could use printf to output the prompt, scanf to get the input, and while to form the loop. Does that make sense?

But those pieces don't match the initial logic (unless you know about a do ... while loop). So we would start with the loop

while (n != 0) {
    printf("Please input an integer\n"); // output the prompt
    scanf("%d", &n); // get the number

Of course, you'll see that we haven't defined n, so we better add it

int n = -1; // initialize n so that the loop will work the first time
while (n != 0) {
    printf("Please input an integer\n"); // output the prompt
    scanf("%d", &n); // get the number

The question didn't ask for a function, or a working program, or the correct #includes, so I haven't written them.

The key to learning to programm is (apart from reading code) is to write code. Lots of it. Make mistakes. Fix them. Learn from them. And do more. Do it until you can think in code. Everyone of average intelligence can do it.

And another thing. You wouldn't believe how dumb the computer is. You have to tell it everything. And it only does what you tell it, not what you intended.

And about exams - make sure you understand what is expected. For example, if the lecturer has said to always add the correct #includes, then do it. Exam papers always have context and interpretation from the semester of study.


The answer has been given to you on a plate, unfortunately its not going to help you learn and you are not a better programmer unless you think about how we got the answer so easily. (Or do you just want to pass an exam?)

"So I'm stuck on finding some way to store all the variables and this has stumped me for hours."

Did you think to ask "Is there another way" - how do you think the answerers here came up with the answer? Did we all know it already, how did we solve it so quickly. We quickly realised that the way you were thinking was never going to work.

My though process is "That list potentially has unlimited inputs, storing the numbers is not possible. How else can it be done? what is an average? - total divided by number of samples. Keep a running total and count of the number of samples. Calculate average at end.

I though that through about as fast as you will have read it. How - experience and doing it lots of times, nothing to do with us being cleverer than you.....

  • I strenuously object, your Honour! "On a plate" indeed! Seriously, though, if you are referring to my answer, the current question is not the really OP's problem. I used it as an example of how a student might go about answering such questions in future. It is the case that rep-hungry people give complete answers to what are clearly homework problems, but with 20 years Uni teaching experience, that certainly isn't me :-) – andy256 Nov 27 '14 at 3:27
  • And +1 for the description of thinking the alternatives through. – andy256 Nov 27 '14 at 3:30

You don't need to store all the variables, only their running total and how many of them there are to calculate the mean.

FTR, to store all the variables, you would need a dynamically allocated data structure, which requires malloc/free and nobody should have that shit inflicted on them.

Not the answer you're looking for? Browse other questions tagged or ask your own question.