954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Determin the average of an array that generates 30 random numbers.

I am trying to write a program that takes a specif range from the user and generates 30 random numbers in that range. I have that part worked out and the code works fine but I have no clue how to take the average of those 30 random numbers. Could anyone offer some advice that would be great

Thanks

cl3m0ns
Light Poster
30 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Init a variable to ie Sum = 0;

Run a for loop thirty times to get a total sum.

for( int i = 0; i < 30; i++)
{
Sum = Sum + array[i];
}

Then divide by 30

return Sum/30

That's the average!!

gkbush
Newbie Poster
13 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

Add them to a sum using a loop, then divide by 30. That's generally how one would find the average of a list of numbers:

double sum = 0;

for ( int i = 0; i < 30; i++ )
  sum += a[i];

cout<<"Average: "<< sum / 30 <<'\n';
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks for you help I don't know why I didn't think of that I think it's just the array thing threw me off or something.

Anyways Thanks alot

cl3m0ns
Light Poster
30 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You