DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Legacy and Other Languages (http://www.daniweb.com/forums/forum42.html)
-   -   Running an equation a million times (http://www.daniweb.com/forums/thread112057.html)

andyg55 Mar 3rd, 2008 6:46 pm
Running an equation a million times
 
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.

andyg55 Mar 3rd, 2008 7:12 pm
Re: Running an equation a million times
 
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?

DevonMcC++ Mar 10th, 2008 6:03 pm
Re: Running an equation a million times
 
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.

Quote:

Originally Posted by andyg55 (Post 551530)
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?


xylog Apr 12th, 2008 6:30 am
Re: Running an equation a million times
 
I would use a running average and only store the last value and average value in memory. Storing millions of values will just waste memory and slow you down.


All times are GMT -4. The time now is 2:03 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC