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

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You cannot actually implement whiteflags' suggestion in post #14 unless you store the numbers in the array, or rather unreasonably expect the user to enter the same input a second time.

    If your code for finding the highest works fine, then finding the second highest is easy: whenever you find a new highest, the previous highest must be second highest thus far.
    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

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to indent properly and add some whitespace to make it more readable:

    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;
    }
    Your logic flawed, and that's why it won't work. Grab a debugger and step through it or step through it manually. If I give you the sequence 1, 5, 3, why does it fail?
    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. #18
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    And how would I find the previous highest? Doing second = othvar would just make the output the same.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kelton2
    And how would I find the previous highest? Doing second = othvar would just make the output the same.
    You already know the previous highest: it is by definition the highest before you found a number higher than it. This is why I say that "if your code for finding the highest works fine, then finding the second highest is easy". It is so easy, in fact, that you don't have to do anything at all besides recording it!

    Look, if you don't get the logic such that you can implement an algorithm, try out concrete examples.
    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. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The previous highest is the same thing as the rec, that you just compared, before you assigned othvar to it.

  6. #21
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I have it almost working, but it still doesn't print the correct result for every combination. What am I still missing?
    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;
        
        for(int track = 1; track < number; track++) {
        cin >> othvar;
        second = rec;
        if(rec < othvar) {
               rec = othvar;
               }
    }
              highest = rec;
              int sprint = second;
              cout << endl;
             cout << "Highest: " << highest;
             cout << endl;
             cout << "Second: " << sprint;
         
         }

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kelton2
    it still doesn't print the correct result for every combination. What am I still missing?
    Your code is clearly wrong, but it only becomes clear when you indent it properly:
    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;
    
        for(int track = 1; track < number; track++) {
            cin >> othvar;
            second = rec;
            if(rec < othvar) {
                rec = othvar;
            }
        }
        highest = rec;
        int sprint = second;
        cout << endl;
        cout << "Highest: " << highest;
        cout << endl;
        cout << "Second: " << sprint;
    }
    Notice that you are assigning to second on every iteration. Frankly, you are using too many variables. You only need variables for the highest, second highest, and the current input. Get rid of what is unnecessary, and declare your variables near first use.

    Thinking about it further (I experimented with the logic myself with concrete examples), my claim is a mistake: that is, if the second highest comes after the highest, you will not find it if you only assume that the previous highest is the second highest. So you do have to check for the second highest separately.
    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

  8. #23
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by laserlight View Post
    Your code is clearly wrong, but it only becomes clear when you indent it properly:
    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;
    
        for(int track = 1; track < number; track++) {
            cin >> othvar;
            second = rec;
            if(rec < othvar) {
                rec = othvar;
            }
        }
        highest = rec;
        int sprint = second;
        cout << endl;
        cout << "Highest: " << highest;
        cout << endl;
        cout << "Second: " << sprint;
    }
    Notice that you are assigning to second on every iteration. Frankly, you are using too many variables. You only need variables for the highest, second highest, and the current input. Get rid of what is unnecessary, and declare your variables near first use.

    Thinking about it further (I experimented with the logic myself with concrete examples), my claim is a mistake: that is, if the second highest comes after the highest, you will not find it if you only assume that the previous highest is the second highest. So you do have to check for the second highest separately.
    OK, so how would I check for the second highest then? What would the check be?

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kelton2 View Post
    OK, so how would I check for the second highest then? What would the check be?
    Are you listening?

    Quote Originally Posted by laserlight View Post
    Look, if you don't get the logic such that you can implement an algorithm, try out concrete examples.
    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.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kelton2
    OK, so how would I check for the second highest then? What would the check be?
    Something like this:
    Code:
    if (input > highest) {
        second_highest = highest;
        highest = input;
    } else if (input > second_highest) {
        second_highest = input;
    }
    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

  11. #26
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by laserlight View Post
    Something like this:
    Code:
    if (input > highest) {
        second_highest = highest;
        highest = input;
    } else if (input > second_highest) {
        second_highest = input;
    }
    Still not working:
    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;
     
        for(int track = 1; track < number; track++) {
            cin >> othvar;
            if (othvar > othvar) {
        second = othvar;
        othvar = othvar;
    } else if (othvar > second) {
        second = othvar;
    }
        }
        highest = rec;
        int sprint = second;
        cout << endl;
        cout << "Highest: " << highest;
        cout << endl;
        cout << "Second: " << sprint;
    }

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Looks like we have another "do my homework for me".
    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. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kelton2
    Still not working:
    Indent your code properly and then explain to us, line by line, what the for loop does. When you do so, the reason why your code does not work will become obvious.

    Oh, and get rid of rec and sprint.
    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

  14. #29
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Now highest is broken as well:
    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;
      
        for(int track = 1; track < number; track++) {
            cin >> othvar;
            if (othvar > othvar) {
        second = othvar;
        othvar = othvar;
    }   else if (othvar > second) {
        second = othvar;
    }
        }
        cout << endl;
        cout << "Highest: " << highest;
        cout << endl;
        cout << "Second: " << second;
    }
    The if statement will never run since that is impossible.
    The else if might run but I think the wrong value is being assigned to second.
    As for highest being broken, I think I just need to add a if(highest < othvar) highest = othvar statement.
    So now that I've figured out what's broken, how do I fix it?

  15. #30
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I fixed highest.

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