Results 1 to 14 of 14

Thread: Finding the largest number from the rand()!.:Resolved:.

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Question 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

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    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.

  3. #3

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Talking 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

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    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.

  5. #5

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    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

  6. #6
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking 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

  7. #7
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    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.

  8. #8
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    It does include the first random number, random numbers are only generated inside the loop, and directly afterwards the sum and max are computed.

  9. #9

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    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

  10. #10
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    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.

  11. #11

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    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

  12. #12

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    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

  13. #13
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    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.

  14. #14

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    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
  •  



Click Here to Expand Forum to Full Width