Without using arrays, write a complete program that reads three integers from the console and outputs the same three numbers in ascending order on separate lines. For example if the input is 5 4 3 then the output will be
3
4
5.
Do not prompt the user or produce any other output besides the three numbers with endlines.

Here is what I have for my solution:

#include<iostream>
using namespace std;

int main()
{
    int k, m, n, swap;

    cin >> k >> m >> n;
    if(n < m && m < k)
    {
        cout << n << endl;
        cout << m << endl;
        cout << k << endl;
    }
    else if(m < n && n < k)
    {
        cout << m << endl;
        cout << n << endl;
        cout << k << endl;
    }
    else if(n < k && k < m)
    {
        cout << n << endl;
        cout << k << endl;
        cout << m << endl;
    }
    else if(k < n && n < m)
    {
        cout << k << endl;
        cout << n << endl;
        cout << m << endl;
    }
    else if(n < k && k < m)
    {
        cout << m << endl;
        cout << k << endl;
        cout << n << endl;
    }
    else
    {
        cout << k << endl;
        cout << m << endl;
        cout << n << endl;
    }


    return 0;
}

The actual output I am getting is:
2
1
3

Is there something that I am doing wrong? What do I need to change?

Recommended Answers

All 8 Replies

It would help us if we would know what your input was :)

Here is my input:
3 2 1

Here is my current output:
2
1
3

Expected output I want to get:
1
2
3

    else if(n < k && k < m)
    {
        cout << m << endl;
        cout << k << endl;
        cout << n << endl;
    }

At least this output is wrong for the specified values, but I don't think this is the only error.

With code like this (or any code really) it is much easier to spot errors if you step through line by line in the debugger.

Also consider what happens if any of the input values are equal.

commented: Good advise. +15

I would prompt you to consider a shorter if somewhat more complex solution, one which works by sorting the three values. While a full sorting function is a bit heavyweight for this purpose, consider using three temporary values, min, mid, and max, and then testing the three input values in such a way as to put the results into the three temporary values as appropriate:

sort (a, b, c):
    if a <= b
        min = a
        mid = b
    else
        min = b
        mid = a

    if mid <= c
        max = c
    else
        max = mid
        if ...
// I'll leave the rest to you...

you would then simply print out min, mid, and max.

Its the easiest way i can do.

#include <iostream>
#include <algorithm> // for swap

using namespace std;

int main()
{
    int a = 0, b = 20, c = 128;
    if (b > c)
        swap(b, c);
    if (a > b)
        swap(a, b);
    if (b > c)
        swap(b, c);
    cout << a << endl << b << endl << c << endl;

    cin.get();  // for stopping the window
    cin.ignore(); // from closing

    return 0;
}

Are we giving a code solution or teaching him how to do it? @Sarkurd, you are giving way too much sample code... The OP won't learn anything but copy-and-paste...

Back to the OP original code, you should arrange the if-else if-else statement so it is easier to look at. Currently, you have a duplicated case... By the way, do COMMENT your code so you could see and verify what you are doing.

if(n < m && m < k) {  // first n is the highest
  cout << n << endl;
  cout << m << endl;
  cout << k << endl;
}
else if(n < k && k < m) {  // second n is the highest
  cout << n << endl;
  cout << k << endl;
  cout << m << endl;
}
else if(m < n && n < k) {  // first m is the highest
  cout << m << endl;
  cout << n << endl;
  cout << k << endl;
}
else if(k < n && n < m) {  // first k is the highest
  cout << k << endl;
  cout << n << endl;
  cout << m << endl;
}
else if(n < k && k < m) {  // duplicated condition!!!
  cout << m << endl;
  cout << k << endl;
  cout << n << endl;
}
else {  // because of duplicated, either m or k is the highest
  cout << k << endl;
  cout << m << endl;
  cout << n << endl;
}

@Taywin save time and write less. soon he will hate programming if he keep writing long answers for easy questions.
the swap function is much easier also he will learn something new.

you can ignore my answer if you don't like it :-D

@Sarkurd, it is not about like or don't like, it is about the purpose of this forum. Saving time by giving the solution is not the purpose of this forum. This is not a place where people come on to get a solution. It is a place to teach those who are looking for a solution how to achieve it...

commented: Nice. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.