-3

i want to know how to turn this if else statement to a switch statement.this is a c++ program to out put the integers in the right order. i can't figure out a way.please help.thank you in advance.

int x, y, z;
cout << "please enter three integers:";
cin >> x >> y >> z;
if (x <= y && y <= z)
    cout << x << " " << y << " " << z << endl;
else if (x <= z && z <= y)
    cout << x << " " << z << " " << y << endl;
else if (y <= z && z <= x)
    cout << y << " " << z << " " << x << endl;
else if (y <= x && x <= z)
    cout << y << " " << x << " " << z << endl;
else if (z <= x && x <= y)
    cout << z << " " << x << " " << y << endl;
else
    cout << z << " " << y << " " << x << endl;

if (x <= y) {
    if (z <= x) {
        cout << z << " " << x << " " << y << endl;
    } else {
        if (z <= y) {
            cout << x << " " << z << " " << y << endl;
        } else {
            cout << x << " " << y << " " << z << endl;
        }
    }
} else {
    if (z >= x) {
        cout << y << " " << x << " " << z << endl;
    } else {
        if (y >= z) {
            cout << z << " " << y << " " << x << endl;
        } else {
            cout << y << " " << z << " " << x << endl;
        }
    }
}
2
  • 2
    I find your lack of formatting disturbing... Mar 2, 2014 at 6:42
  • How would a switch statement work for this? What would be the individual case values? The case values are compile time constants, not runtime values. Either stick with these if() statements, or come up with a better solution. Mar 2, 2014 at 6:51

5 Answers 5

2

That is not a good way to write a program. What if you need 4 integers? One way to do it would be to use a list like std::vector and sort it.

vector<int> numbers;

int number = 0;
while (numbers.size() < 3 && cin >> number)
    numbers.push_back(number);

sort(cbegin(numbers), cend(numbers)); // sorts ascending by default

for (auto number : numbers)
    cout << number << " ";

cout << endl;

You can also use std::multiset which sorts its items automatically as they are inserted.

2
  • This will successfully fill the numbers with 0 for the input like "not a number"
    – Spock77
    Mar 2, 2014 at 7:01
  • @Alex You are right, I didn't bother with input evaluation, I wanted to focus on the program flow. The question is about the if/switch construction, but I will edit the answer.
    – Excelcius
    Mar 2, 2014 at 7:04
1

Instead of this approach, why don't you store the input in an array and try any of the sorting alogrithms to sort your input?

0

The answer in your particular case is you can't,
Your if statements have particular conditions, which,
wouldn't really work inside a switch.

An example of if/else if/else statements that can be
translated into a switch would be:

if(i == 1) {
    // code here
} else if(i == 2) {
    // code here
} else if(i == 3) {
    // code here
} else {
    // code here
}

which would translate to:

switch(i) {
    case 1:
        //code here
        break;
    case 2:
        //code here
        break;
    case 3:
        //code here
        break;
    default:
        //code here
        break;
}

Hope that helps a bit to understand a little more about switch statements.

0

If the challenge is that you can't sort the numbers, and the limit is 3 integers, then there are better ways to accomplish what you're doing.

#include <iostream>
using namespace std;

int main()
{
   int combo[6][3] = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}};
   int values[3];
   cin >> values[0] >> values[1] >> values[2];
   for (int i = 0; i < 6; ++i )
   {
      if ( values[combo[i][0]] <= values[combo[i][1]] && 
           values[combo[i][1]] <= values[combo[i][2]] )
      {
          cout << values[combo[i][0]] << " " << values[combo[i][1]] <<
                " " << values[combo[i][2]];
          break;
      } 
   }
}

The combo array holds all of the combinations that can occur with 3 slots. Also note that the input is an array. Even if you didn't use the loop, the maximum if() statements you would need is 6.

However you should mention up front that this is a challenge, and what the restrictions are. Otherwise, please look at the other answers concerning storing and sorting these numbers.

0

You only need three conditional swaps to sort a list of three elements:

int x, y, z;
cout << "please enter three integers:";
cin >> x >> y >> z;

if (y < x) swap(y, x);
if (z < y) swap(z, y);
if (y < x) swap(y, x);

cout << x << " " << y << " " << z << endl;

Note that this approach doesn't scale. For 10 numbers, you would already need 45 conditional swaps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.