Click to See Complete Forum and Search --> : Finding the largest number from the rand()!.:Resolved:.
voidflux
Oct 15th, 2003, 08:08 PM
Hello everyone!
I have a slight problem!
I'm suppose to generate 80 random numbers, then I need to find the sum of those random numbers, and the max of the random numbers! Here is my code
#include <iostream>
#include <cstdlib>
int main()
{
int sum = 0;
int first, max;
for(int i = 1; i <=80; i++)
{
cout << 10 + (rand() % 10) << endl;
sum = sum + (10 + (rand()%10));
}
cout <<"The sum is : " << sum << endl;
return 0;
}
THis finds the sum of the 80 random integers, but how would I get to compare the random integers?
ANy ideas?
Thanks!:wave:
Guv
Oct 15th, 2003, 08:39 PM
Initialize a variable to zero. Compare with each random number, replacing the variable if bigger. When you are finished the largest will be in that variable.
voidflux
Oct 15th, 2003, 09:04 PM
Thanks for the reply, I tried that too and I got wrong results, what am I dong wrong?
#include <iostream>
#include <cstdlib>
//Initialize a variable to zero.
//Compare with each random number, replacing the variable if bigger.
//When you are finished the largest will be in that variable
int main()
{
int sum = 0;
int second, max;
//srand(10); //set the seed
for(int i = 1; i <=80; i++)
{
cout << 10+(rand()%10) << endl;
max = 10 + (rand() % 10);
second = 10 + (rand() % 10);
if(max < second)
{
max = second;
}
sum = sum + (10 + (rand()%10));
}
cout <<"The max of the numbers is: " << max << endl;
cout <<"The sum of the numbers is: " << sum << endl;
return 0;
}
when I output the random numbers, I see higher numbers then what my max gives me!:confused:
Guv
Oct 15th, 2003, 09:29 PM
You are generating 80 pairs of random numbers.
You need a temporaty variable to store each random number. compare the temporary with the variable holding the tentative maximum.
voidflux
Oct 15th, 2003, 09:51 PM
okay this is what I came out with:
Does it look good?
#include <iostream>
#include <cstdlib>
int main()
{
int sum=0, number=10, max;
max = 10+rand()%10;
for(int i=0; i<80; i++)
{
cout <<i << endl;
if(number >= max)
{
max = number;
}
number=10+rand()%10;
sum=sum+number;
}
cout <<"The max of the numbers is: " << max << endl;
cout <<"The sum of the numbers is: " << sum << endl;
return 0;
}
sql_lall
Oct 16th, 2003, 06:01 AM
Yeah, that looks ok, just the ordering is a bit weird.
Firstly, usually you set 'max' storing variables to -1 if they are only comparing positive integers. Setting it to 10 is ok, just that it's always easier to understanf if you set it to -1. However, u are setting it to 10 + rand() % 10, which could be dangerous as this could be larger than all your other random numbers.
It is often also simpler to generate all the variables inside the loop: so this is a simplified way:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int sum = 0;
int max = -1;
int number = 0;
for(int i = 0; i<80; i++)
{
cout<<i<<endl;
number = 10 + rand() % 10;
sum+=number;
if(number > max)
max = number;
}
cout...;
cout...;
return 0;
}
nice to see someone using C++;
oh, and there are some things u can do to tidy it up a bit, but they are just getting picky.
like #include <algorithm>, then you can say: max = max(max, number) - not sure if ud have to rename the int max though.
oh, and a cooler way to get around it is:
max = (max > number) ? max : number;
this is using the _ ? _ : _ ; format, which is a shortened if-else thing.
Guv
Oct 16th, 2003, 11:53 AM
VoidFlux: I am not a C programmer, but it seems to me that your code does not include the first number in the sum.
twanvl
Oct 16th, 2003, 03:42 PM
It does include the first random number, random numbers are only generated inside the loop, and directly afterwards the sum and max are computed.
voidflux
Oct 16th, 2003, 05:27 PM
yah my first version of my program was terriable!
I re-wrote it and I think this one works alot better!
#include <iostream>
#include <cstdlib>
int main()
{
int sum=0, number=0, max=0;
for(int i=0; i<80; i++)
{
if(number >= max)
{
max = number;
}
number=10+rand()%10;
sum=sum+number;
}
cout <<"The max of the numbers is: " << max << endl;
cout <<"The sum of the numbers is: " << sum << endl;
return 0;
}
:blush:
Guv
Oct 16th, 2003, 10:13 PM
VoidFlux: Perhaps the if statement should come after the number=10+rand()%10; statement.
voidflux
Oct 17th, 2003, 09:02 AM
VoidFlux: Perhaps the if statement should come after the number=10+rand()%10; statement.
In this case, I would get the same result. Thanks for the help though! :thumb:
voidflux
Oct 17th, 2003, 09:42 AM
I didn't even see ur post SQL, I had the lab yesterday morning but luckly we came out to basically the same thing!
Guv
Oct 20th, 2003, 11:55 PM
VoidFlux: If the last pseudo random number generated happens to be the maximum, your code will print the next to the maximum.
When testing code, you must check for or think about special case situations like maximum first, maximum last.
voidflux
Oct 21st, 2003, 07:20 AM
ahh good point Guv!
thanks!:bigyello:
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.