I'm doing an assignment for my CSC class and the instructor has asked us to:
1. Ask for three numbers
2. Determine the smallest number
3. Determine the largest number
4. Determine the middle number
5. Output the numbers in ascending order
I went through and did the first 4 but forgot about 5. Now I'm not sure how to write the code so that the numbers come out in ascending order instead of smallest, largest and middle.
/************************** Compiler Directives **********************/
#include <cstdlib>
#include <iomanip>
#include <iostream>
usingnamespace std;
/**************************** main Function **************************/
int main()
{
//Local variables
int firstNumber;
int secondNumber;
int thirdNumber;
int smallNumber;
int largeNumber;
int middleNumber;
//Prompt user for the three numbers.
cout << "Enter the first number \n";
cin >> firstNumber;
cout << "Enter the second number \n";
cin >> secondNumber;
cout << "Enter the third number \n";
cin >> thirdNumber;
//Determine smallest number
//Checking if first number is the smallest
if (firstNumber <= secondNumber && firstNumber <= thirdNumber)
{
cout << "Smallest number: " << firstNumber;
cout << "\n";
}
//Checking if second number is the smallest
if (secondNumber <= firstNumber && secondNumber <= thirdNumber)
{
cout << "Smallest number: " << secondNumber;
cout << "\n";
}
//Checking if third number is the smallest
if (thirdNumber <= firstNumber && thirdNumber <= secondNumber )
{
cout << "Smallest number: " << thirdNumber;
cout << "\n";
}
//Determine Largest number
//Checking if first number is the largest
if(firstNumber >= secondNumber && firstNumber >= thirdNumber)
{
cout << "Largest number: " << firstNumber;
cout << "\n";
}
//Checking if second number is the largest
if(secondNumber >= firstNumber && secondNumber >= thirdNumber)
{
cout << "Largest number: " << secondNumber;
cout << "\n";
}
//Checking if third number is the largest
if(thirdNumber >= firstNumber && thirdNumber >= secondNumber)
{
cout << "Largest number: " << thirdNumber;
cout << "\n";
}
//Determine middle number
//Checking if first number is the middle number
if (secondNumber > firstNumber && firstNumber > thirdNumber || thirdNumber > firstNumber && firstNumber > secondNumber)
{
cout << "Middle number: " << firstNumber;
cout << "\n";
}
//Checking if second number is the middle number
if (firstNumber > secondNumber && secondNumber > thirdNumber || thirdNumber > secondNumber && secondNumber > firstNumber)
{
cout << "Middle number: " << secondNumber;
cout << "\n";
}
//Checking if third number is the middle number
if (firstNumber > thirdNumber && thirdNumber > secondNumber || secondNumber > thirdNumber && thirdNumber > firstNumber)
{
cout << "Middle number: " << thirdNumber;
cout << "\n";
}
//Indicate to OS successful termination of program
return 0;
//End main
}
#include <iostream>
int main()
{
// Prompt user for the three numbers.
int first ;
int second ;
int third ;
std::cout << "Enter three numbers: ";
std::cin >> first >> second >> third ;
// rearrange them in order from smallest to largest
if( second < first )
{ constint temp = first ; first = second ; second = temp ; } // swap them
// after this, first <= second
if( third < first )
{ constint temp = first ; first = third ; third = temp ; }
// after this, first <= third (now, the smallest number is first)
if( third < second )
{ constint temp = second ; second = third ; third = temp ; }
// after this, second <= third (now, the largest number is third)
// print results
std::cout << "smallest number: " << first << '\n'
<< " middle number: " << second << '\n'
<< " largest number: " << third << '\n'
<< "numbers in ascending order: "
<< first << ' ' << second << ' ' << third << '\n' ;
// there is an implicit return 0 ; at the end of main
}
The concept of the bold line would need to be adjusted for each if statement and after the last if statement you need a "cout" statement to display the "smallNumber", "largeNumber" and "middleNumber".
I would take a very close at JLBorges's code as it is a more compact way of doing what you did and worth learning.