944,116 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6417
  • C++ RSS
Oct 17th, 2007
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Oct 17th, 2007
0

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

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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gkbush is offline Offline
12 posts
since Jul 2004
Oct 17th, 2007
0

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

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:
C++ Syntax (Toggle Plain Text)
  1. double sum = 0;
  2.  
  3. for ( int i = 0; i < 30; i++ )
  4. sum += a[i];
  5.  
  6. cout<<"Average: "<< sum / 30 <<'\n';
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 17th, 2007
0

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

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
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Program runs but not visible
Next Thread in C++ Forum Timeline: I need some help with basic C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC