943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1168
  • C++ RSS
Sep 17th, 2009
1

adding a bunch of random numbers

Expand Post »
hi i need some help with adding a bunch of random numbers.
i dont understand how to add them all.
heres the code:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. int main ()
  5. {
  6. int i,x;
  7. srand (time (NULL));
  8. for (i=1; i<=10; i++){
  9. x=rand()%10+1;
  10. cout<<x<<endl;
  11. }
  12. system ("pause");
  13. return 0;
  14. }

i wanna know how to add up all 10 random numbers generated from this.
thank you.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
blahbla is offline Offline
24 posts
since Sep 2009
Sep 17th, 2009
0

Re: adding a bunch of random numbers

Hi blahbla,

It seems to me that your best bet would be to create a new variable before you start your loop that you use to add all of your random numbers together. I would suggest doing the addition right before the end of your for loop so that every iteration of the loop ends by adding the new random number to the accumulation variable.

See compound operators for the correct usage:
http://www.cplusplus.com/doc/tutorial/operators/

-D
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009
Sep 17th, 2009
0

Re: adding a bunch of random numbers

set a variable at the beginning then add them all up:

eg.
C++ Syntax (Toggle Plain Text)
  1. int total = 0;
  2.  
  3. for ( int i = 0; i < 10; i++ )
  4. {
  5. total = total + i;
  6. }
  7.  
  8. cout << total;
Last edited by iamthwee; Sep 17th, 2009 at 5:14 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 17th, 2009
0

Re: adding a bunch of random numbers

thank you to both of you for answering i understand it now.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
blahbla is offline Offline
24 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Problem with a cout test statement
Next Thread in C++ Forum Timeline: are their I/O redirection and pipes in C++?





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


Follow us on Twitter


© 2011 DaniWeb® LLC