3

I'm working through the exercises in Bjarne Stroustrup's book "Principles and Practice Using C++" (chapter 3 exercise 6). You're asked to make a program that takes user input (3 integer values) and prints the 3 values in order from smallest to largest. Here's my solution,

#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << "Please enter 3 integers: ";
    int num1, num2, num3;
    while (cin >> num1 >> num2 >> num3) {

    if (num1 < num2 && num2 < num3)
        cout << num1 << ", " << num2 << ", " << num3 << endl;

    if (num2 < num1 && num1 < num3)
        cout << num2 << ", " << num1 << ", " << num3 << endl;

    if (num3 < num1 && num1 < num2)
        cout << num3 << ", " << num1 << ", " << num2 << endl;

    if (num1 > num2 && num1 > num3 && num2 < num3)
        cout << num2 << ", " << num3 << ", " << num1 << endl;

    if (num1 > num2 && num1 > num3 && num2 > num3)
        cout << num3 << ", " << num2 << ", " << num1 << endl;

    if (num1 < num2 && num1 < num3 && num2 > num3)
        cout << num1 << ", " << num3 << ", " << num2 << endl;

    if (num1 == num2 && num1 < num3)
        cout << num1 << ", " << num2 << ", " << num3 << endl;

    if (num1 == num3 && num3 < num2)
        cout << num1 << ", " << num3 << ", " << num2 << endl;

    if (num1 == num2 && num1 > num3)
        cout << num3 << ", " << num2 << ", " << num1 << endl;

    if (num1 == num3 && num2 < num3)
        cout << num2 << ", " << num3 << ", " << num1 << endl;

    if (num3 == num2 && num1 < num3)
        cout << num1 << ", " << num2 << ", " << num3 << endl;

    if (num3 == num2 && num1 > num3)
        cout << num3 << ", " << num2 << ", " << num1 << endl;
   }
}

Although this worked for me, it just looks like a lot of code and I would really appreciate a more "simple" take on the problem.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Njgardner90
  • 61
  • 1
  • 7

6 Answers6

6

You can add the values to a vector, then sort it, and output the values in order.

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::cout << "Please enter 3 values" << '\n';
    std::vector<int> values;
    for (int i = 0; i < 3; ++i)
    {
        int value;
        std::cin >> value;
        values.push_back(value);
    }
    std::sort(values.begin(), values.end());
    std::cout << values[0] << ", " << values[1] << ", " << values[2] << '\n';

    return 0;
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • be bad-ass 'till the end: print using ranged-based for – bolov Aug 08 '17 at 13:27
  • @bolov Yeah but then you need the to check where to insert the commas, then [that turns into a whole debacle](https://stackoverflow.com/questions/5689003/how-to-implode-a-vector-of-strings-into-a-string-the-elegant-way).... so I got lazy :) – Cory Kramer Aug 08 '17 at 13:28
  • 2
    `std::sort(begin(values), end(values));` always looks more natural to me, but I guess that's a matter of taste. (For future readers, both variants do exactly the same thing.) – Baum mit Augen Aug 08 '17 at 13:30
  • 1
    cheat: print with spaces instead of commas :)) – bolov Aug 08 '17 at 13:30
  • 1
    He hasn't covered vectors or arrays in his book yet. I thought it was a bit of a weird exercise for what he has already taught, but thank you for the help and will surely be looking back on it when he does cover vectors or arrays – Njgardner90 Aug 08 '17 at 17:24
0

If you were to store the numbers in a sequential data structure (array, lits, deque) instead of separate variables then a simpler algorithm is possible:

  • Sort the sequence.
  • Print the sequence in order.

C++ standard library has an algorithm for sorting: std::sort.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Functions can help reduce redundancy. You can create a function to take three numbers and print them out in order.

void PrintNumbers(int a, int b, int c) {
    cout << a << ", " << b << ", " << c << endl;
}

Then all you would have to do is call the method and pass the numbers in a different order instead of rewritting your print logic every time. Exmaple

PrintNumbers(num1, num2, num3);   
PrintNumbers(num2, num3, num1);
PrintNumbers(num3, num2, num1);

Also, if you look into using arrays, you can create a sort method that takes an array and returns a sorted array. Alternatively, if you look into passing variables by reference (instead of by value). Then you can write a method that will make it so num1 is always the smallest, num2 is always the middle, and num3 is always the largest. This would allow you to basically

Having these two methods (One to sort, and one to print) would turn this part of your main function into basically two lines of code.

SortNumbers(num1, num2, num3);
PrintNumbers(num1, num2, num3);

Or if you use arrays (and if you can, I recommend that you do)

int[] sortedNumbers = SortNumbers(myNumbers);
PrintNumbers(sortedNumbers);
MBurnham
  • 381
  • 1
  • 9
0

You can do something like this:

int main()
{
    std::vector<int> values;

    for (int i = 0; i < 3; ++i)
    {
        int value;
        std::cin >> value;
        values.push_back(value);
    }
    std::cout << std::fminf(std::fminf(values[0], values[1]), values[2]);
    std::cout << std::fmaxf(std::fminf(values[0], values[1]), std::fminf(values[1], values[2]));
    std::cout << std::fmaxf(std::fmaxf(values[0], values[1]), values[2]);

}
maayanwis
  • 5
  • 3
0

Some rules.

If you have a bunch of data you want to treat uniformly, except possibly order-wise, you want to use an array.

If you want to place things in a particular order, you sort them.

If you don't need them unsorted afterwards, you sort them in place.

And don't using namespace std; it is a really bad habit.

std::cout << "Please enter 3 integers: ";
int num[3]; // an array of 3 integers
while (std::cin >> num[0] >> num[1] >> num[2] {
  std::sort( std::begin(num), std::end(num) );
  std::cout << num[0] << ", " << num[1] << ", " << num[2] << "\n";
}

std::endl both inserts a newline and flushes the io buffer. Only do it when you really, really want to flush the buffer.

We can improve the above further:

enum {count = 3};
int num[count]; // an array of 3 integers
std::cout << "Please enter " << count << " integers: ";
while ( [&]{
  for (int& n:num)
    if (!std::cin >> n) return false;
  return true;
}()) {
  std::sort( std::begin(num), std::end(num) );
  for (int& n:num) {
    if (&n != num) std::cout << ", ";
    std::cout << n;
  }
  std::cout << "\n";
}

and now I can change how many integers I'm working with by changing count at one spot. There are 4 numbers in computer science: 0, 1, 2 and infinity.

Now, here we hard-code how many integers at compile time. This is sometimes a good idea, sometimes not.

std::size_t count; 
std::cout << "How many integers at a time?";
if (!std::cin >> count) return -1;
std::vector<int> num(count); // an array of count integers
std::cout << "Please enter " << count << " integers: ";
while ( [&]{
  for (int& n:num)
    if (!std::cin >> n) return false;
  return true;
}()) {
  std::sort( std::begin(num), std::end(num) );
  for (int& n:num) {
    if (&n != num.data()) std::cout << ", ";
    std::cout << n;
  }
  std::cout << "\n";
}

This now asks the user how many integers per clump.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
0

My solution without using the array:

void order_integer() { int a, b, c;

cout << "Enter 3 integers: \n";
cin >> a >> b >> c;
if (a <= b && a <= c) {
    if (b <= c) cout << a << ' ' << b << ' ' << c << endl;
    if (c <= b) cout << a << ' ' << c << ' ' << b << endl;
}

else if (b <= a && b <= c) {
    if (a <= c) cout << b << ' ' << a << ' ' << c << endl;
    if (c <= a) cout << b << ' ' << c << ' ' << a << endl;
}

else if (c <= a && c <= b) {
    if (a <= b) cout << c << ' ' << a << ' ' << b << endl;
    if (b <= a) cout << c << ' ' << b << ' ' << a << endl;
}

}