Not a clue

This is a discussion on Not a clue within the C++ Programming forums, part of the General Programming Boards category; This is what I have, but I'm struggling to achieve a goal which I'll explain shortly. Here is the code ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    Not a clue

    This is what I have, but I'm struggling to achieve a goal which I'll explain shortly. Here is the code

    Code:
    // Practise Exercise
    
    #include <iostream> 
    using namespace std;
    
    int main()
    {  
    
       int counter = 1;
       int number;
       int largest = 0;
       int next_largest = 0;
       
       while ( counter <= 10 )
       {
          cout << counter << ". Enter a number: ";
          cin >> number; 
          
          // works out largest number entered by user      
          if ( largest < number )
             largest = number;
       
          ++counter;
       }
       
       cout << "Largest number entered:\t" << largest << '\n'
            << "Second lagest number entered:\t" << next_largest << endl; 
    
       system( "pause" );
       return 0;
    }
    I can easily work out the largest number entered by the user, but now I'm stuck on working out the second largest. I've tried many things, but I cannot get my mind around the maths to get to the second largest. Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    Code:
    if ( largest < number )
    {
             next_largest=largest;
             largest = number;
    }
    else if(next_largest < number)
            next_largest=number;

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    I guess the biggest problem is that you've written your code already, but you haven't worked out yet what you want the code to do

    Even for a program this small, it's worth getting the steps right in your mind before you code.

  4. #4
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    I appreciate the good tip Omnibus. See, I wrote this program to correspond with the requirements of an exercise I was given. Then about 2 exercises later it returned to the one I had written and asked for it to produce the second largest number along with the largest. Normally I would have written out the steps, but this time I had to add to the original program.

    erikj I am highly impressed! I sat for a while trying to figure it out. How may I ask do you figure problems like this out, or have you done it before?

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    The problem isn't really that hard and i haven't done it before. You will solve problems like these with ease in a couple of months, trust me.

    For me it helps to try to visualize the problem on paper before coding it. When you have a solution or two that might work, try to code it.

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    What happends if the user desides to only enter negative numbers???
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    it will still work, why shouldn't it?

    but you probably want to initialize largest and next_largest with MIN_INT or equivalent
    Last edited by erikj; 12-28-2003 at 06:07 AM.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by erikj
    but you probably want to initialize largest and next_largest with MIN_INT or equivalent
    Or INT_MIN

  9. #9
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    What happends if the user desides to only enter negative numbers???
    it will still work, why shouldn't it?
    No, because variable number will never be greater than variable largest(0).

    but you probably want to initialize largest and next_largest with MIN_INT or equivalent
    Where do you see that in his/hers code (before you said it)???

    Another solution(which I find to be simpler) is to store all elements(ints in this case) in a vector and sort them.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    Where do you see that in his/hers code (before you said it)???
    I didn't. Thats why a mentioned it.

    What happends if the user desides to only enter negative numbers???
    If you knew the answer why did you ask?

  11. #11
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Where do you see that in his/hers code (before you said it)???
    I didn't. Thats why a mentioned it.
    And in your reply to me you said
    it will still work, why shouldn't it?
    Iīm kinda getting double messages that it works and at the same time it doesnīt.

    If you knew the answer why did you ask?
    At the time I replied for the first time I saw a bug which none of the previous answer had noticied. Insteed of giving the whole code for him/her I wanna give him/her a chance to find and solve the problem and if he/she canīt Iīll help.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    ok. point taken =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. Replies: 8
    Last Post: 04-19-2006, 10:07 AM
  3. No clue what the problem is...
    By FingerPrint in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2006, 03:16 PM
  4. A really weird problem. I understand the error, but not a clue why I get the errors.
    By Finchie_88 in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-20-2005, 09:48 AM
  5. No CLUE what to do...
    By stewade in forum C Programming
    Replies: 7
    Last Post: 05-03-2004, 01:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21