Hi
Do you think the following code rightly address the problem of arranging the three entered numbers in descending order? Please let me know. Thanks.
Code:
// swap3numbers.cpp
// swap three number so that they appear in descending order
// it is assumed entered numbers are distinct
#include <iostream>
#include <cstdlib>
using namespace std;
int swapthree(float& dummya, float& dummyb, float& dummyc);
int main()
{
float a, b, c;
cout << "enter a: "; cin >> a;
cout << "enter b: "; cin >> b;
cout << "enter c: "; cin >> c;
if ( swapthree(a, b, c) )
{
cout << "the entered numbers in descending order: " << a << " " << b
<< " " << c << endl;
}
else if ( !swapthree(a, b, c) )
{
cout << "some error\n";
}
system("pause");
return 0;
}
//-----------------------------------------------------------------
// swapthree() definition
int swapthree(float& dummya, float& dummyb, float& dummyc)
{
if ( (dummya > dummyb) && (dummyb > dummyc) )
{
return 1;
}
else if ( (dummya > dummyc) && (dummyc > dummyb) )
{
float temp = dummyb;
dummyb = dummyc;
dummyc = temp;
return 1;
}
else if ( (dummyb > dummya) && (dummya > dummyc) )
{
float temp = dummya;
dummya = dummyb;
dummyb = temp;
return 1;
}
else if ( (dummyb > dummyc) && (dummyc > dummya) )
{
float temp = dummya;
dummya = dummyb;
dummyb = dummyc;
dummyc = temp;
return 1;
}
else if ( (dummyc > dummya) && (dummya > dummyb) )
{
float temp = dummya;
dummya = dummyc;
dummyc = dummyb;
dummyb = temp;
return 1;
}
else if ( (dummyc > dummyb) && (dummyb > dummya) )
{
float temp = dummyc;
dummyc = dummya;
dummya = temp;
return 1;
}
else
{
return 0;
}
}