Home > Net >  Sorting smallest to largest number of 3 digits input only in C using conditional statements
Sorting smallest to largest number of 3 digits input only in C using conditional statements

Time:10-08

My assignment is to sort the inputted 3 digits in ascending order, using conditional statements only in C

My failed code:

#include <iostream>
using namespace std;

int main()
{
    int n1, n2, n3;
    int largest, middle, smallest;

    cin >> n1 >> n2 >> n3;

    // smallest:
    if (n1 < n2 && n1 < n3)
    {
        smallest = n1;
    }
    else if (n2 < n1 && n2 < n3)
    {
        smallest = n2;
    }
    else if (n3 < n1 && n3 < n2)
    {
        smallest = n3;
    }
        
    cout << smallest;
        
    // middle:
    if ((n1 >= n2 && n1 >= n3) && (n1 <= n2 && n1 <= n3))
    {
        middle = n1;
    }
    else if ((n2 >= n1 && n2 >= n3) && (n2 <= n1 && n2 <= n3))
    {
        middle = n2;
    }
    else if ((n3 >= n1 && n3 >= n2) && (n3 <= n1 && n3 <= n2))
    {
        middle = n3;
    }
    
    cout << " " << middle;

    // largest:
    if (n1 > n2 && n1 > n3)
    {
        largest = n1;
    }
    else if (n2 > n1 && n2 > n3)
    {
        largest = n2;
    }
    else if (n3 > n2 && n3 > n1)
    {
        largest = n3;
    }
    
    cout << " " << largest;

    return 0;
}
Input: 6 1 3
My Output: 1 0 6
Expected Output: 1 3 6

As you can see, I couldn't figure out how to sort the middle digit to be sorted to the middle of the 3 digits. I don't have a problem with the largest and smallest digits since I can use < and > to them, except for the middle.

CodePudding user response:

You don't need six variables to sort three numbers.

#include <algorithm>  // std::swap
#include <iostream>

int main() {
  int large, middle, small;

  // Read first two numbers then arrange them accordingly
  std::cin >> large >> middle;

  if (middle > large) {
    std::swap(middle, large);
  }

  // Read final number and place accordingly
  std::cin >> small;
  if (small > large) {
    // We need two swaps because we first move the value of large to
    // to small, but now small is greater than middle, so we swap
    // those two variables.
    std::swap(small, large);
    std::swap(small, middle);
  }
  else if (small > middle) {
    std::swap(small, middle);
  }

  std::cout << small << ' ' << middle << ' ' << large << '\n';
}

There is no need for crazy comparisons, or lots of them for the case of three numbers only. Naturally if the input gets bigger we'd defenestrate this idea entirely and use an actual sorting algorithm.

CodePudding user response:

After you find smallest & largest, middle = n1 n2 n3 - largest - smallest. (This may be vulnerable to an integer overflow, thanks for the comment.)

You can also use six if statement:

if(n1 >= n2 && n2 >= n3)
    max = n1, middle = n2, min = n3;
else if(n1 >= n3 && n3 >= n2)
    max = n1, middle = n3, min = n2;
// ... fill the following 4 permutations

CodePudding user response:

if ((n1 >= n2 || n1 >= n3) && (n1 <= n2 || n1 <= n3))
{
    middle = n1;
}
else if ((n2 >= n1 || n2 >= n3) && (n2 <= n1 || n2 <= n3))
{
    middle = n2;
}
else if ((n3 >= n1 || n3 >= n2) && (n3 <= n1 || n3 <= n2))
{
    middle = n3;
}

CodePudding user response:

Consider an alternate approach to finding the smallest of three ints. Much cleaner. You're apparently not allowed to use loops, but the dataset is small enough that we can essentially just unroll the loop.

int smallest_of_three(int a, int b, int c) {
    int smallest = a;

    if (b < smallest) {
        smallest = b;
    }

    if (c < smallest) {
        smallest = c;
    }

    return smallest;
}

We can do the same for the largest.

int largest_of_three(int a, int b, int c) {
    int largest = a;

    if (b > largest) {
        largest = b;
    }

    if (c > largest) {
        largest = c;
    }

    return largest;
}

We can then use these to find the middle by checking to see if each of a, b, and c are not either the smallest or largest. A default case of returning a handles the case where there are duplicates.

int middle_of_three(int a, int b, int c) {
    int s = smallest_of_three(a, b, c);
    int l = largest_of_three(a, b, c);

    if (a != s && a != l) {
        return a;
    }
    else if (b != s && b != l) {
        return b;
    }
    else if (c != s && b != l) {
        return c;
    }
    else {
        return a;
    }
}
  •  Tags:  
  • c
  • Related