Is there an easier way to get three digits from the user and print them in ascending order?

I'm working on exercises in Bjarne Stroustup's book Principles and Practice Using C ++ (Exercise 6 in Chapter 3). You are asked to make a program that takes user input (3 integer values) and prints 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;
   }
}

      

While this worked for me, it just looks like a lot of code, and I would really appreciate a "simpler" problem.

+3


source to share


5 answers


You can add values ​​to vector

, then sort

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;
}

      

+5


source


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

  • Sorting the sequence.
  • Print the sequence in order.


C ++ standard library has a sorting algorithm: std::sort

.

0


source


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

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

      

Then all you have to do is call the method and pass the numbers in a different order, rather than rewrite your printing logic every time. Exmaple

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

      

Also, if you are learning about using arrays, you can create a sort method that takes an array and returns a sorted array. Alternatively, if you look at variable variables by reference (instead of value). Then you can write a method that will make it num1

always the smallest, num2

always in the middle, and num3

always the largest. This will basically allow you

Having these two methods (one for sorting and one for printing) will turn this part of your main function into two lines of code.

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

      

Or, if you are using arrays (and if you can, I recommend you do)

int[] sortedNumbers = SortNumbers(myNumbers);
PrintNumbers(sortedNumbers);

      

0


source


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]);

}

      

0


source


Some rules.

If you have a bunch of data that you want to process evenly, except perhaps in order, you want to use an array.

If you want to arrange things in a specific order, you sort them.

If they are not formatted afterwards, you sort them in place.

And using namespace std;

this is not really a 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 insert a newline and flush the io buffer. Only do this when you really want to flush the buffer.

We can improve above:

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 the amount of integers I am working with by changing count

in one place. In computer science, there are 4 numbers: 0, 1, 2, and infinity.

Now, here we will compile the number of integers at compile time. Sometimes it's a good idea, sometimes it's 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";
}

      

Now this asks the user how many integers are there per unit.

0


source







All Articles