Thread: Need a hint as to how to make a program that prints two highest numbers from user inp

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Need a hint as to how to make a program that prints two highest numbers from user inp

    ut. (the title bar wasn't long enough :P) Anyways, that sums it up. I need to write a program where a user inputs a bar of numbers, and the two highest ones are printed. Now, I have the input working correctly, but I can't figure out the two highest numbers thing. The input cins are inside a loop that runs <inputed variable> number of times, but I can't figure out how to count the highest and second highest- or just the highest for that matter. Below is my current code which just prints 0, if someone could give me a hint as to what to do (I tried putting various things inside the loop to try and record the highest, didn't work) that would be appreciated.
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int number;
        int othvar;
        int highest = 0;
        cout << "How many numbers? ";
        cin >> number;
        for(int track = 0; track < number; track++) {
        cin >> othvar;
        
    }
             cout << "highest: " << highest;
         
         }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If I gave you the numbers 1, 5, 3, in that order, how would you go about, after each number I give you, determine which is highest?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    5 is the highest as 5 > 3 and 5 > 1.
    I still don't see how I could record a variable instance for that.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kelton2
    5 is the highest as 5 > 3 and 5 > 1.
    I still don't see how I could record a variable instance for that.
    Try a slightly longer list of numbers, e.g., 3 2 4 5 1

    Write these down on little cards or pieces of paper, line them up and then turn them face down. Pretend that you have forgotten what the numbers are. One by one, turn them over and look at the number, turning it face down again before going on to the next number. How would you know which is the highest?

    Wouldn't you try and remember what is the highest number that you have seen thus far (and which card that was)? Perhaps you might write this down somewhere.

    If you find it too easy to remember all of them at once with just 5 numbers, take out a deck of playing cards and try the experiment again. For cards with the same number, you could rank them by suit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    So a variable should record the highest number so far? Makes sense, but how would I implement that? Do I need the variable to be set to the user input variable every time and somehow record it? I understand the concept, but I don't see how it translates into the program.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need a variable to store the user input, and a variable to record the highest number thus far.

    Do this first, then later introduce another variable for second highest.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A variable is a piece of memory that stores a value you assign to it and remembers it until the variable is destroyed. Example:

    Code:
    int main()
    {
        int First;
        int Entered = 1;
        std::cout << "Enter number: ";
        std::cin >> First;
    
        while (Entered != 0)
        {
            std::cout << "Enter number (exit with 0): ";
            std::cin >> Entered;
            std::cout << "You entered: " << Entered << "\n";
        }
    
        std::cout << "The first number you entered was: " << First << "\n";
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I already have the user input variable (othvar), but how would I do the highest number thus far thing? Making a variable that keeps setting itself to othvar every loop iteration doesn't work, I've tried it. I see what you're saying (store the highest number thus far that the user has input, it makes sense) but the implementation itself is confusing me. How would I record the highest number so far?
    Quote Originally Posted by Elysia View Post
    A variable is a piece of memory that stores a value you assign to it and remembers it until the variable is destroyed. Example:

    Code:
    int main()
    {
        int First;
        int Entered = 1;
        std::cout << "Enter number: ";
        std::cin >> First;
    
        while (Entered != 0)
        {
            std::cout << "Enter number (exit with 0): ";
            std::cin >> Entered;
            std::cout << "You entered: " << Entered << "\n";
        }
    
        std::cout << "The first number you entered was: " << First << "\n";
    }
    I understand what a variable is, I just don't know what I would record.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kelton2
    How would I record the highest number so far?
    There are two basic approaches:
    • Set the variable to the lowest possible number that can be stored in a variable of that type. <limits.h> can come in handy.
    • Set the variable to the value of the very first number in the list, in this case the very first number entered by the user.

    Once you have done either of these, you just need to check if the current number is larger than the highest known.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kelton2 View Post
    I understand what a variable is, I just don't know what I would record.
    Well, again, in the "real world" scenario, how would you do it?
    Assuming you have a card that represents the highest number so far, when would you write to it and what would you write to it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I got it working:
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int number;
        int othvar;
        int highest = 0;
        cout << "How many numbers? ";
        cin >> number;
        cin >> othvar;
        int rec = othvar;
        for(int track = 1; track < number; track++) {
        cin >> othvar;
        if(rec < othvar) {
               rec = othvar;
               }
    }
    highest = rec;
             cout << "highest: " << highest;
         
         }
    Now I need to figure out the second highest. How would I do that?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, how would you do it in real life? Amend the previous discussion on finding the max with finding the second highest max.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I did this and it still doesn't work for the second one, what do I need to change?
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int number;
        int othvar;
        int highest = 0;
        int second = 0;
        cout << "How many numbers? ";
        cin >> number;
        cin >> othvar;
        int rec = othvar;
        second = othvar;
        for(int track = 1; track < number; track++) {
        cin >> othvar;
        if(rec < othvar) {
               rec = othvar;
               }
               if(second < othvar && second < rec) {
                         second = othvar;
                         }
    }
              highest = rec;
              int sprint = second;
              cout << endl;
             cout << "Highest: " << highest;
             cout << endl;
             cout << "Second: " << sprint;
         
         }

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would find the highest first. After you find the highest, you can find the second highest by doing essentially the same process. The only thing that is different is the second highest number is always less than the highest.

  15. #15
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    That's basically what I did in the code I just posted- or tried to do. It doesn't work for some reason. The highest works fine, it's just that the second highest doesn't.
    Last edited by Kelton2; 01-19-2015 at 11:57 PM. Reason: typo fix

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  2. Replies: 8
    Last Post: 12-30-2010, 10:08 PM
  3. Program the prints prime numbers
    By cloudstrife910 in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2010, 03:03 PM
  4. How to make a program that prints it's own source code???
    By chottachatri in forum C++ Programming
    Replies: 38
    Last Post: 03-28-2008, 07:06 PM
  5. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM