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

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int i,x;
srand (time (NULL));
for (i=1; i<=10; i++){
x=rand()%10+1;
cout<<x<<endl;
}
system ("pause");
return 0;
}

i wanna know how to add up all 10 random numbers generated from this.
thank you.

dgr231 commented: Used code tags on first post! +1

Recommended Answers

All 3 Replies

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

Member Avatar for iamthwee

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

eg.

int total = 0;

for ( int i = 0; i < 10; i++ )
{
  total = total + i;
}

cout << total;

thank you to both of you for answering i understand it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.