Click here to Skip to main content
15,886,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am learning processing and there is an assignment question that tells me to use while loop to print out random numbers between -100 and 100, then print out the average of those numbers. I know how to do the first part but not the second...

What I have tried:

I didn't know any alternatives to do it I'm just a beginner to programming
Posted
Updated 8-Oct-17 6:38am
Comments
Richard MacCutchan 8-Oct-17 12:32pm    
That's not programming it's mathematics. As you generate the numbers keep a running total, and also a count. When you get to the end divide the total by the count and that is the average.

1 solution

You can create a loop, and inside it you have a variable which holds the current number.
All you need is a variable which holds a running total (declare it outside the loop, and initialize it to zero), to add the current number to it each time round the loop, and a count of how many numbers you generated.
After the loop, divide the total by the count and you have the average.

But it's your homework, so even if I had a clue what language you are supposed to use, I still wouldn't give you the code! Give it a try - it's not at all complicated.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900