| | |
adding a bunch of random numbers
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
hi i need some help with adding a bunch of random numbers.
i dont understand how to add them all.
heres the code:
i wanna know how to add up all 10 random numbers generated from this.
thank you.
i dont understand how to add them all.
heres the code:
C++ Syntax (Toggle Plain Text)
#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.
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
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
set a variable at the beginning then add them all up:
eg.
eg.
C++ Syntax (Toggle Plain Text)
int total = 0; for ( int i = 0; i < 10; i++ ) { total = total + i; } cout << total;
Last edited by iamthwee; Sep 17th, 2009 at 5:14 pm.
*Voted best profile in the world*
![]() |
Similar Threads
- Help needed filling array with unique random numbers (C++)
- Need Help on random numbers and adding them and displaying statement (C++)
- Generate 6 Random Numbers (C++)
- 2D array with uniqqe random numbers... (C++)
- A lovely mixture of random numbers and arrays (C++)
- C++ Random Numbers (C++)
- Generating random numbers using VC6 (C++)
- Adding Random Numbers help (C++)
Other Threads in the C++ Forum
- Previous Thread: Problem with a cout test statement
- Next Thread: are their I/O redirection and pipes in C++?
Views: 591 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






