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!