Basic Exercise

I am trying to come up with code that prompts a user to input three numbers (integers). The program should then output the numbers in ascending order.
I am having troubles with 1: deciding which is the very simplest way of going about this. 2: Receiving any output with this provided code. 3: where the proper placement for parenthesis are. I am a beginner, of course.

#include <iostream>


using namespace std;

int main()
{
int a;
int b;
int c;

int smallest;
int middle;
int largest;



cout << "Please enter three positive digits for this program "
<< "to sort in ascending order: " << endl;
cin >> a >> b >> c;


if (a < b && a < c)
{
if (b < c)
a = smallest;
b = middle;
c = largest;
cout << " " << a << b << c; endl;

else
a = smallest;
c = middle;
b = largest;
cout << " " << a << c << b << endl;
}
else
if (b < a && b < c)
{
if (a < c)

b = smallest;
a = middle;
c = largest;

cout << " " << b << a << c << endl;
}
else
b = smallest;
c = middle;
a = largest;

cout << " " << b << c << a << endl;


else
if (c < a && c < b)
{
if (a < b)

c = smallest;
a = middle;
b = largest;

cout << " " << c << a << b << endl;

else
c = smallest;
a = largest;
b = middle;

cout << " " << c << b << a << endl;


else

cout << "You did not enter three valid numbers." << endl;

}




return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm> // sort()
using namespace std;

int main()
{
	int ar[3] = {3,1,2};
		
	sort(ar, ar+3);
	
	for(auto x: ar)
		cout << x << " "; // 1 2 3

return 0;
}
Thanks for your response, anup. However, this assignment is located in very early chapters of a relatively large textbook. So, I would have to think there is a more simple way. The sections before this assignment only go as far as introducing if, else statements and switch functions for beginners.
in that case, learn some sorting algorithms. bubble sort is the easiest (& slowest) sorting algorithm. for that you need to understand nested loop first.
In my textbook, sorting algorithms such as bubble sort is not addressed or introduced until way further down the line..there' still about 8 more chapters until sorting algorithms are actually taught. The solution to this assignment must be in the early stages of learning C++.
> So, I would have to think there is a more simple way.
I wonder what is your meaning for `simple'.
You'll end up sorting the numbers, how you do it was encapsulated in anup30's post.
Before starting to code try to describe in your natural language how to solve the problem

> Receiving any output with this provided code.
Your code does not compile, perhaps want to start with that.
If you just ask your IDE to indent your code properly, the errors would be obvious

> where the proper placement for parenthesis are
the condition needs to be enclosed in parenthesis
¿is that your doubt?
Last edited on
For this kind of homework your professor is not interested in your ability to leverage the STL. To sort and print three numbers you only need some if statements.

You don't really need much in the way of parentheses; I am personally of the habit of being explicit with my parentheses -- it has made a difference before and relieves me of having to memorize a large operator precedence table.

When you run your program, use explorer to navigate to the directory with your executable file, click in the address bar and type cmd (and press Enter). In the console window that pops up, type the name of your program and press Enter, and you'll see your program run and all its output.

To simplify, you only need one cout statement, after you select a, b, and c.

Also, I would input a, b, and c, and assign to smallest,....

Hope this helps.
its "manual sort" without looping

e.g. for two variables: cin>> a; cin>> b;
sorted x and y
x = a < b ? a : b;
y = a > b ? a : b;
cout << x << " " << y;


alternatively,
if(a<b) cout << a << " " << b;
else cout << b << " " << a;


Topic archived. No new replies allowed.