1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
using namespace std;
void sort(int& num1, int& num2, int& num3);
int main()
{
int num1, num2, num3;
cin >> num1 >> num2 >> num3;
sort(&num1, &num2, &num3); //error cannot change int* to int & ???
//it is also saying the initial value of reference to non-const must be an lvalue
cout << num1 << " " << num2 << " " << num3 << " \n ";
}
void sort(int& num1, int& num2, int& num3) {
//largest -> smallest
int* first;
first = &num1;
int* second;
second = &num2;
int* third;
third = &num3;
if (num2 > num1 && num2 > num3&& num1 > num3) {
// num2, num1, num3
*first = num2;
*second = num1;
}
else if (num1 < num2 && num2 < num3 && num1 < num3) {
//num3, num2, num1;
*first = num3;
*third = num1;
}
else if (num1 < num2 && num2 > num3&& num1 < num3) {
//num2, num3, num1;
*first = num2;
*second = num3;
*third = num1;
}
else if (num1 > num2 && num2 < num3 && num1 > num3) {
//num1, num3, num2;
*second = num3;
*third = num2;
}
else if (num1 > num2 && num2 < num3 && num1 < num3) {
//num3 ,num1, num2;
*first = num3;
*second = num1;
*third = num2;
}
else {
*first = num1;
*second = num2;
*third = num3;
}
}
|