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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

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

 
0
  #1
Oct 17th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 12
Reputation: gkbush is an unknown quantity at this point 
Solved Threads: 0
gkbush gkbush is offline Offline
Newbie Poster

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

 
0
  #2
Oct 17th, 2007
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!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #3
Oct 17th, 2007
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:
  1. double sum = 0;
  2.  
  3. for ( int i = 0; i < 30; i++ )
  4. sum += a[i];
  5.  
  6. cout<<"Average: "<< sum / 30 <<'\n';
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

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

 
0
  #4
Oct 17th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC