Sunday, June 1, 2014

Sorting Three Numbers in C++

When is the last time we try to arrange or sort as series of numbers using pen and paper? That’s ok if you are trying to sort or arrange numbers that are very few but what is your are dealing of many numbers it seems that job is not easy using manual operation just like using pen and paper it very prone to errors and it takes a lot of effort in our part to sort each number in proper order. One of the benefits of your computers in general it helps us making our lives easier and less effort. The computer is a complex machine and has the ability to perform fast computation compared to its human counterparts. Sorting in computer science mean it is the arrangement of numbers, letters or works in ascending or descending order. Let me explain the two types of sorting arrangement in details.
Ascending sort mean that we arrange the numbers, texts or words small the smallest value to the highest number values. In terms of letters we start with letter A to Z as simple as that in ascending order. On the other hand descending order is the opposite of ascending order in this case from the biggest number value to the smallest number value or Z to A in terms in alphabets. In our sample code here I am not using any sorting algorithms to sort these three numbers. All we need is the use of the if – else statement to compare which of the three numbers is the smallest, middle and the highest number.
I call this program Sorting Three Numbers this program is not difficult but we must able to understand its inner logic with its conditional statement we use here that is If-Else statement in C++. The first thing that our program will do is to ask the user to enter three numbers simultaneously. In this case we assign three variables A,B,C. After we ask the user to enter three integer number our program will process this values using a series of conditional statements in C++.
if(a<b){
if(a<c){
cout<<a<<", ";
if(c<b)
cout<<c<<", "<<b<<"\n";
else cout<<b<<", "<<c<<"\n";
}
In this code if variable a is less than B then proceed with the second if statement if (a < c) if the condition in true then the smallest number is A will be displayed in our screen. This if-else statement that we have right now is called compounded or nested if-else statement because we have the outer if statement and inner if-else statement inside of it. The third condition is we select variable C that is the smallest number from the list of three given earlier by our user. So the arrangement if variable C is smallest will be cout<<c<<", "<<b<<"\n"; this means c is the smallest value and followed by b. If the condition is not true then it proceed with next statement that is cout<<b<<", "<<c<<"\n"; that meaning of this command is the variable B is the smallest value and then followed by the second value that is variable C.
else{
cout<<c<<", ";
if(a<b) cout<<a<<", "<<b<<"\n";
else cout<<b<<", "<<a<<"\n";
}
}
In this section of our code I will explain that if both two conditions is false then this will be the last condition of our first if-else statement by this time the value of variable C will be displayed in the screen and then we have another condition that tells us if (a<b) then the display will be cout<<a<<", <<b<<"\n"; and will followed by our else statement cout<<b<<", "<<a<<"\n";. At this point we can clearly see that we just rearrange the placement of our variable the fits in our condition. We are just dealing only with three numbers.
The second if-else statement we are dealing the if (b <c) then the display will be cout <<b<<”.”; Another condition if (a <c) the it will display cout << a <<”,” <<c<<”\n”; but it the condition is false it will display the cout << c<<”,”<<a<<”\n”;
if(b<c){
cout<<b<<", ";

if(a<c) cout<<a<<", "<<c<<"\n";

else cout<<c<<", "<<a<<"\n";
}
This is the last part of our code by this time we are dealing with the variable C is the condition is false then it will display the value of variable C. Next will follow the condition of if (a<b) then it will display cout << a << “,” <<c<<”\n”; and then if it is false it will display the following statements to the user cout<<b<<", "<<a<<"\n";
else{
cout<<c<<", ";
if(a<b) cout<<a<<", "<<c<<"\n";

else cout<<b<<", "<<a<<"\n";
}
}
After the execution of our program it will display a message that thanking the user for using our program and then it will return to your operating system. I hope you have learned something new in this article sorting three numbers. In this article I’m using CodeBlocks as my text editor and Dev C++ that is being integrated within our Codeblocks text editor. If you have some comments, questions and suggestions feel free to email me.

Thank you very much.


Sample Output Of Our Program


Code::Blocks text editor that is being used in writing this program


Program Listing

#include <iostream>
using namespace std;

int main()
{
int a=0, b=0, c=0;
cout << "\t <====== SORTING THREE NUMBERS =====>";
cout << "\n\n";
cout<<"Please Enter Three Numbers :=> ";
cin>>a>>b>>c;
cout << "\n\n";
cout<<"Numbers sorted in Ascending Order : ";
if(a<b){


if(a<c){
cout<<a<<", ";
if(c<b)
cout<<c<<", "<<b<<"\n";
else cout<<b<<", "<<c<<"\n";
}
else{
cout<<c<<", ";
if(a<b) cout<<a<<", "<<b<<"\n";
else cout<<b<<", "<<a<<"\n";
}
}
else{
if(b<c){
cout<<b<<", ";
if(a<c) cout<<a<<", "<<c<<"\n";
else cout<<c<<", "<<a<<"\n";
}
else{
cout<<c<<", ";
if(a<b) cout<<a<<", "<<c<<"\n";
else cout<<b<<", "<<a<<"\n";
}
}
cout << "\n\n";
cout << "THANK YOU FOR USING THIS PROGRAM";
cout << "\n\n";
system("pause");

}

No comments:

Post a Comment