•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Legacy and Other Languages section within the Software Development category of DaniWeb, a massive community of 427,196 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,192 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Legacy and Other Languages advertiser: Programming Forums
Views: 771 | Replies: 3
![]() |
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Rep Power: 1
Solved Threads: 0
Hi guys,
I'm sure this is pretty simple really, but I can't get my head around it.
I have a long equation that I'm using c++ to work out. It involves a random number generator, so each time the program is run, it replaces one of the variables in the equation with a random number. This gives me a different result each time
What I want to do is run the program a million times, store every result that I obtain, and then get an average at the end.
I'm thinking I should put a for loop at the start of my equation, but do I use a counter to record every result I get? I'm really not sure.
Thanks for any help you can give me, if you would like me to post some code I can do...
Andy.
I'm sure this is pretty simple really, but I can't get my head around it.
I have a long equation that I'm using c++ to work out. It involves a random number generator, so each time the program is run, it replaces one of the variables in the equation with a random number. This gives me a different result each time

What I want to do is run the program a million times, store every result that I obtain, and then get an average at the end.
I'm thinking I should put a for loop at the start of my equation, but do I use a counter to record every result I get? I'm really not sure.
Thanks for any help you can give me, if you would like me to post some code I can do...
Andy.
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Rep Power: 1
Solved Threads: 0
ok, instead i did a while loop like this:
while(repeats != 1000000){
Equation = x + y;
Cout << Equation;
repeats++;
}
So basically, it prints the above 1000000 times. Instead, what I want it to do is record every value of x, add them all up, and then divide by 1000000 to get an average. I then want this to be displayed to screen.
Any ideas?
while(repeats != 1000000){
Equation = x + y;
Cout << Equation;
repeats++;
}
So basically, it prints the above 1000000 times. Instead, what I want it to do is record every value of x, add them all up, and then divide by 1000000 to get an average. I then want this to be displayed to screen.
Any ideas?
•
•
Join Date: Feb 2008
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 1
Try summing the results of the equation - printing out a million things is absolutely useless - then dividing by 1e6 at the end of the loop. Better yet, use an array-based language to avoid finding a problem where there isn't any.
•
•
•
•
ok, instead i did a while loop like this:
while(repeats != 1000000){
Equation = x + y;
Cout << Equation;
repeats++;
}
So basically, it prints the above 1000000 times. Instead, what I want it to do is record every value of x, add them all up, and then divide by 1000000 to get an average. I then want this to be displayed to screen.
Any ideas?
![]() |
•
•
•
•
•
•
•
•
DaniWeb Legacy and Other Languages Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
Other Threads in the Legacy and Other Languages Forum
- Previous Thread: PROLOG problem
- Next Thread: Countdown timer


Linear Mode