Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
A program to take 3 variables (int, float, double) and output them in ascending AND descending order. Without using loops.

What I have tried:

#include <iostream>

using namespace std;


void sortNum(int&, float&, double&);

int main()
{
int num1, num2, num3;

cout << "This program will ask you to enter 3 integer numbers and it will ";

cout << "sort the numbers into their ascending order. " << endl << endl;


cout << "Enter the 3 integers that you wish to sort: " << endl;

cin >> num1 >> num2 >> num3;

cout << "\n" << "The ascending order for the 3 numbers you just entered are: " << endl;


sortNum(num1, num2, num3);
}

// sortNum function definition
void sortNum(int& num1, float& num2, double& num3)
{
// declare the lowest, middle, and highest variables
int lowest, middle, highest;

if (num1 < num2 && num2 < num3)
{
lowest = num1;
middle = num2;
highest = num3;

cout << lowest << endl;
cout << middle << endl;
cout << highest << endl;
}
else if (num2 < num1 && num1 < num3)
{
lowest = num2;
middle = num1;
highest = num3;

cout << lowest << endl;
cout << middle << endl;
cout << highest << endl;
}
else if (num3 < num2 && num2 < num1)
{
lowest = num3;
middle = num2;
highest = num1;

cout << lowest << endl;
cout << middle << endl;
cout << highest << endl;
}
else if (num3 < num1 && num1 < num2)
{
lowest = num3;
middle = num1;
highest = num2;

cout << lowest << endl;
cout << middle << endl;
cout << highest << endl;
}
else if (num2 < num3 && num3 < num1)
{
lowest = num2;
middle = num3;
highest = num1;

cout << lowest << endl;
cout << middle << endl;
cout << highest << endl;
}
else if (num1 < num3 && num3 < num2)
{
lowest = num1;
middle = num3;
highest = num2;

cout << lowest << endl;
cout << middle << endl;
cout << highest << endl;
}
}
Posted
Updated 6-Mar-20 19:51pm
Comments
Patrice T 6-Mar-20 23:17pm    
And you have a question or a problem ?

1 solution

Read the instructions, they are quite explicit:
Quote:
A program to take 3 variables (int, float, double)
That's not the same as reading three integers!
You will then need to convert each to the "highest" type: double.
So the first thing to do is prompt him for an integer, and read it. Convert it to a double called Value1
Then prompt him for an float, and read it. Convert it to a double called Value2
Compare them. and swap them over if Value1 is greater than Value2.
Now prompt him for an double, and read it into a variable called Value3.
If Value3 is smaller than Value2, swap them.
If Value2 is smaller than Value1, swap them.

You now have your three values in ascending order:
Value1 <= Value2 <= Value3
Print them in that order for the ascending, and in reversed order for descending.

No loops used!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900