Thread: Order numbers by size, is the solution optimal?

  1. #1
    Registered User EAX101010's Avatar
    Join Date
    Aug 2014
    Posts
    6

    Order numbers by size, is the solution optimal?

    Hello...

    I'm learning C++ from "Programming - Principles and Practice using C++", 2nd edition.

    In chapter 3, an exercise wants me to:

    Write a program that prompts the user to enter three integer values,
    and then outputs the values in numerical sequence separated by commas.

    So, if the user enters the values 10 4 6, the output should be 4, 6, 10.
    If two values are the same, they should just be ordered together.

    So, the input 4 5 4 should give 4, 4, 5.
    Is the following solution optimal, or can it be written in a more simple way?

    Code:
    #include "std_lib_facilities.h"
    
    int main()
    {
        cout << "Enter three integers, separated by space: ";
        int a, b, c, temp1 = 0, temp2 = 0;
        cin >> a >> b >> c;
    
        while (a > b || b > c) {
            temp1 = c;
            temp2 = b;
            c = a;
            b = temp1;
            a = temp2;
        }
        cout << a << ", " << b << ", " << c << "\n";
        return 0;
    }

    UPDATE:
    My first solution has a bug, so here's the corrected solution,
    written using only features I have learned in the first three chapters:

    Code:
    #include "std_lib_facilities.h"
    
    int main()
    {
        cout << "Enter three words, separated by space: ";
        string a, b, c, temp;
        cin >> a >> b >> c;
    
        if (a > b) {
            temp = a;
            a = b;
            b = temp;
        }
        if (b > c) {
            temp = b;
            b = c;
            c = temp;
        }
        if (a > b) {
            temp = a;
            a = b;
            b = temp;
        }
    
        cout << a << ", " << b << ", " << c << "\n";
        return 0;
    }
    Last edited by EAX101010; 08-13-2014 at 11:07 AM. Reason: My first solution has a bug

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <array>
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
    	std::array<int, 3> Ints;
    	std::cout << "Enter three integers, separated by space: ";
    	std::cin >> Ints[0] >> Ints[1] >> Ints[2];
    	std::sort(Ints.begin(), Ints.end());
    	std::cout << Ints[0] << ", " << Ints[1] << ", " << Ints[2] << "\n";
    	return 0;
    }
    More general form (making it easy to scale for any N):
    Code:
    #include <array>
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
    	std::array<int, 3> Ints;
    	std::cout << "Enter three integers, separated by space: ";
    	for (size_t i = 0; i < Ints.size(); i++)
    		std::cin >> Ints[i];
    	std::sort(Ints.begin(), Ints.end());
    	for (size_t i = 0; i < Ints.size() - 1; i++)
    		std::cout << Ints[i] << ", ";
    	std::cout << *(Ints.end() - 1) << "\n";
    	return 0;
    }
    Last edited by Elysia; 08-13-2014 at 10:21 AM.
    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 EAX101010's Avatar
    Join Date
    Aug 2014
    Posts
    6
    Thank you.

    I haven't learned yet some of the code that you use.
    Also, my first solution has a bug.

    Here is the corrected solution, using only code I have learned in the first three chapters:

    For integers as input:

    Code:
    #include "std_lib_facilities.h"
    
    int main()
    {
        cout << "Enter three integers, separated by space: ";
        int a, b, c, temp;
        cin >> a >> b >> c;
    
        if (a > b) {
            temp = a;
            a = b;
            b = temp;
        }
        if (b > c) {
            temp = b;
            b = c;
            c = temp;
        }
        if (a > b) {
            temp = a;
            a = b;
            b = temp;
        }
    
        cout << a << ", " << b << ", " << c << "\n";
        return 0;
    }
    For strings as input:

    Code:
    #include "std_lib_facilities.h"
    
    int main()
    {
        cout << "Enter three words, separated by space: ";
        string a, b, c, temp;
        cin >> a >> b >> c;
    
        if (a > b) {
            temp = a;
            a = b;
            b = temp;
        }
        if (b > c) {
            temp = b;
            b = c;
            c = temp;
        }
        if (a > b) {
            temp = a;
            a = b;
            b = temp;
        }
    
        cout << a << ", " << b << ", " << c << "\n";
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    You could test for all 6 possible cases, which minimizes compares and moves depending on the case.

    Code:
    //
        if (a > b) {
            if(b > c){
                temp = a;   // reverse (swap a c)
                a = c;
                c = temp;
            } else if (a > c){
                temp = a;   // rotate left
                a = b;
                b = c;
                c = temp;
            } else {
                temp = a;   // swap a b
                a = b;
                b = temp;
            }
        } else if (b > c){
            if(a > c){
                temp = a;   // rotate right
                a = c;
                c = b;
                b = temp;
            } else {
                temp = b;   // swap b c
                b = c;
                c = temp;
            }
        }                   // do nothing
    Last edited by rcgldr; 08-13-2014 at 09:38 PM.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Note - my previous post is only for the case of 3 objects, for n objects, there are n! permutations, such as 4 objects = 24 cases, so it's not worth trying to implement a bunch of if / else statements. Once at 4 or more objects, it's better to implement some type of sort algorithm.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rcgldr View Post
    ...Once at 4 or more objects, it's better to implement some type of sort algorithm.
    Or use one that's already available - std::sort.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-20-2012, 07:09 AM
  2. Getting numbers in order
    By SimpleSun29 in forum C Programming
    Replies: 2
    Last Post: 09-23-2011, 05:38 PM
  3. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  4. Looking for optimal solution to: bool HasSameChars()
    By brooksbp in forum C Programming
    Replies: 16
    Last Post: 10-22-2009, 02:54 PM
  5. display numbers in order
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2001, 09:16 PM