|
-
Oct 15th, 2003, 08:08 PM
#1
Thread Starter
Hyperactive Member
Finding the largest number from the rand()!.:Resolved:.
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
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!
Last edited by voidflux; Oct 16th, 2003 at 05:29 PM.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Oct 15th, 2003, 08:39 PM
#2
Frenzied Member
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.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Oct 15th, 2003, 09:04 PM
#3
Thread Starter
Hyperactive Member
Thanks!
Thanks for the reply, I tried that too and I got wrong results, what am I dong wrong?
Code:
#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!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Oct 15th, 2003, 09:29 PM
#4
Frenzied Member
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.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Oct 15th, 2003, 09:51 PM
#5
Thread Starter
Hyperactive Member
okay this is what I came out with:
Does it look good?
Code:
#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;
}
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Oct 16th, 2003, 06:01 AM
#6
Fanatic Member
hmmm...
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:
Code:
#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.
sql_lall 
-
Oct 16th, 2003, 11:53 AM
#7
Frenzied Member
VoidFlux: I am not a C programmer, but it seems to me that your code does not include the first number in the sum.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Oct 16th, 2003, 03:42 PM
#8
It does include the first random number, random numbers are only generated inside the loop, and directly afterwards the sum and max are computed.
-
Oct 16th, 2003, 05:27 PM
#9
Thread Starter
Hyperactive Member
yah my first version of my program was terriable!
I re-wrote it and I think this one works alot better!
Code:
#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;
}
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Oct 16th, 2003, 10:13 PM
#10
Frenzied Member
VoidFlux: Perhaps the if statement should come after the number=10+rand()%10; statement.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Oct 17th, 2003, 09:02 AM
#11
Thread Starter
Hyperactive Member
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!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Oct 17th, 2003, 09:42 AM
#12
Thread Starter
Hyperactive Member
I didn't even see ur post SQL, I had the lab yesterday morning but luckly we came out to basically the same thing!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Oct 20th, 2003, 11:55 PM
#13
Frenzied Member
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.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Oct 21st, 2003, 07:20 AM
#14
Thread Starter
Hyperactive Member
ahh good point Guv!
thanks!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|