Beginner's Challenge - Book Exercise

I've finished reading my first C++ Programming TextBook ! The textbook is "C++ Programming From Problem Analysis to Program Design" by D.S. Malik

I went back to earlier chapters and started doing End-Of-Chapter programming exercises.

All the programming exercises so far were pretty easy until I got this one problem in Chapter 4: Control Structures (Selection)

It took me a good half an hour coding this exercise. Here is the problem

"Write a program that prompts the user to input three numbers. The program should then output the numbers in ascending order"

Now, if you have advanced C++ concepts, this is a very simple problem. You could utilize <algorithm> / <vector> Libraries, you could utilize a simple sort algorithm with array or vector. But remember, this problem was found in Chapter 4 and thus far, the book has NOT covered any of the arrays / vectors. It has not covered any loops either.

Here is what the book has covered so far:
Chapter 2: Variable Declaration and Variable Types
Chapter 3: Input / Output with <iostream> and <fstream>. Little bit of <cmath>
Chapter 4: Control Structures - if, else if, else, switch

Using ONLY those concepts, write the program I described above.


This is what I wrote after 30 minutes of trying (Yes, I am a beginner) and so far, it passed all the tests I've thrown at it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Algorithm:

1. Compare the first number with the second number and then with the third number.
   (This will let us know whether number 1 is the lowest value)

2. If the first number is the lowest value, print it, and then compare the second value and the third value

3. Print the next lowest number from Step 2 and print the last number.

4. Repeat Steps 1 ~ 3, this time, comparing the second number with the first number, then with the third number.

5. Repeat Steps 1 ~ 3, this time, comparing the third number with the first number, then with the second number.

6. If none of the Steps above has been executed, it means that the first number and the second number are exactly the same.
   Compare one of them with the third number then print them.
*/

#include <iostream>

int main ()
{
    std::cout << "Enter 3 numbers: ";
    double num1 , num2 , num3;
    std::cin >> num1 >> num2 >> num3;
    std::cout << std::endl;

    if ( num1 < num2 )
    {
        if ( num1 < num3 )
        {
            std::cout << num1 << " ";

            if ( num3 < num2)
                std::cout << num3 << " " << num2;
            else
                std::cout << num2 << " " << num3;
        }
        else
            std::cout << num3 << " " << num1 << " " << num2;
    }
    else if ( num2 < num1 )
    {
        if ( num2 < num3 )
        {
            std::cout << num2 << " ";

            if ( num1 < num3 )
                std::cout << num1 << " " << num3;
            else
                std::cout << num3 << " " << num1;
        }
        else
            std::cout << num3 << " " << num2 << " " << num1;
    }
    else if ( num3 < num1 )
    {
        if ( num3 < num2 )
        {
            std::cout << num3 << " ";

            if ( num1 < num2)
                std::cout << num1 << " " << num2;
            else
                std::cout << num2 << " " << num1;
        }
        else
            std::cout << num2 << " " << num3 << " " << num1;
    }
    else        // The first 2 numbers are the same value
    {
        if ( num1 < num3 )
            std::cout << num1 << " " << num2 << " " << num3;
        else
            std::cout << num3 << " " << num1 << " " << num2;
    }

    return 0;
}


If any of you seasoned programmers could come up with a simpler and more efficient algorithm / codes, I'd love to see it. Beginners are also encouraged to try this exercise as well.

Here are some Specifications
1. Must be double type Ex) 1.2 4.5 0.4
2. Some or All numbers can be the same. Ex) 1 1 2 or 3 3 3
3. Negative numbers ARE allowed. Ex) -1 3 -5

With those specifications, the program MUST print the numbers in ascending order

Thank You!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

void swap(double& first, double& second)
{
    double temp = first;
    first = second;
    second = temp;
}

int main()
{
    std::cout << "Enter 3 numbers: ";
    double num1, num2, num3;
    std::cin >> num1 >> num2 >> num3;

    if (num1 > num2) swap(num1, num2);
    if (num2 > num3) swap(num2, num3);
    if (num1 > num2) swap(num1, num2);

    std::cout << num1 << ' ' << num2 << ' ' << num3 << std::endl;

    return 0;
}
I guess you haven't covered functions either, but it would be simple to copy that swap function logic into the three places it is used.
Last edited on
@booradley60

I didn't think of utilizing other variables. That's nice.

The book has not covered user-defined functions yet. So I will try to utilize it without the void function.

Thank you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

int main ()
{
    std::cout << "Enter 3 numbers: ";
    double num1 , num2 , num3;
    std::cin >> num1 >> num2 >> num3;
    std::cout << std::endl;

    double temp;

    if ( num1 > num2 )
    {
        temp = num1;
        num1 = num2;
        num2 = temp;
    }

    if ( num1 > num3 )
    {
        temp = num1;
        num1 = num3;
        num3 = temp;
    }

    if ( num2 > num3 )
    {
        temp = num2;
        num2 = num3;
        num3 = temp;
    }

    std::cout << num1 << ' ' << num2 << ' ' << num3 << std::endl;
    return 0;
}


Ever since I started solving problems with C++, I've realized that after scratching my brain for extended period of time, I can usually solve problems, but my solution is always complicated and never as efficient as it can be. I really hope I get better at formulating algorithms.
Last edited on
An arguably simpler solution is the following.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream.h>
#include <cmath>
using namespace std;

int main(){
    double first,second,third,sum,minimum,maximum;
    cout<<"Enter First: ";
    cin>>first;
    cout<<"Enter Second: ";
    cin>>second;
    cout<<"Enter Third: ";
    cin>>third;
    sum = first+second+third;
    minimum = fmin(first,fmin(second,third));
    maximum = fmax(first,fmax(second,third));
    cout<<minimum<<", "<<sum - (minimum+maximum)<<", "<<maximum;
    return 0;
}
Last edited on
@guatemala007

Yes, it does look simpler. It didn't occur to me that <cmath> library provides fmin and fmax. I like the way you printed the middle value by subtracting min and max from the sum.

Thanks.
No prob, glad I could help!
Topic archived. No new replies allowed.