Struggling with logic puzzle

Okay so I am having a tremendous amount of trouble with this program. I am a newbie and this is part of an assignment. My mind is still stuck in this "linear thinking" mode and it is hard as a new person learning c++ to break through. Anyways, the instructions for this program are.

write a program which will take 3 numbers entered by the user and put them in ascending and descending order and display the ascending and descending order to the user. The orders should be labeled correctly.You should also use a loop to get in the three numbers.

Note: You can only use if/else as well as the loop for this program.


Okay now the instructions are done. Being as fresh as I am, this is the only start I can think of for this program. Thanks in advanced.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int s1 = 0;
	int s2 = 0;
	int s3 = 0;

	cout << "Enter three numbers: " << endl;
	cin >> s1 >> s2 >> s3;
	
	if (s1 < s2 && s1 < s3)
		cout << "First: " << s1 << endl;
	else if (s2 > s1 && s2 < s3)
		cout << "Second: " << s2 << endl;
	else if (s3 > s1 && s3 > s2)
		cout << "Third: " << s3 << endl;

	system("pause");
	return 0;
	
}
  
Last edited on
Try creating a flowchart for this problem. This usually helps visualizing the problem you need to solve. If you don't know what a flowchart is or how to make it, this article might help you out:
https://www.programiz.com/article/flowchart-programming
Try doing something like this:

Given three numbers, a, b, and c:
1
2
3
4
5
if a > b, swap a and b.
if b > c, swap b and c. 
if a > b, swap a and b.
Postcondition:  
a, b, and c are in strictly non-decreasing order.


Last edited on
try observing the data in action also. You might note, for example, that solving either acending or decending problem, you can swap the outside values to reverse it. that is

1 2 3 ... swap 3 and 1, the outsides, and you get...
3 2 1 .... which only works for 3 numbers, but hey, it does work.

I have done a partial solution to your question to help you get going. Once the third number has been entered you need to tweak the remaining code to get it to work.

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
#include <iostream>
using namespace std;

int main()
{
    int num1, num2, num3, o1 = 0, o2 = 0, o3 = 0;
    cout << "enter first number)" << endl;
    cin >> num1;
    cout << "enter second number" << endl;
    cin >> num2;
    if(num1 < num2)
    {
        o1 = num1;
        o2 = num2;
    }
    else
    {
        o1 = num2;
        o2 = num1;
    }
    cin >> num3;
    if(num3 < num1 && num3 < num2)
    {
        o1 = num3;
        o2 = num1;
        o3 = num2;
    }
    else if(num3 > num1 && num3 < num2)
    {
        o1 = num1;
        o2 = num3;
        o3 = num2;
    }
    else
    {
        o1 = num1;
        o2 = num2;
        o3 = num3;    
    }
    cout << "Num 1 = " << o1 << "  Num 2 = " << o2 << " Num 3 = " << o3;
}
Thanks, Im going to work on this tonight and Ill report back once I figure it out!
Topic archived. No new replies allowed.