Sunday 26 June 2016

C++ Program to Calculate Sort Numbers into Ascending Order

This section helps to create a program that will actually help to sort ten numbers into ascending Order regardless of the kind of numbers inputed by users.
Here is the script.


#include <iostream>
using namespace std;
int main()
{
    int num[10],a,b,c;
    cout <<"INPUT NUMBERS" << endl;
    for(int enter=0; enter<10; enter++) {
            cin >> num [enter];
    }
for(a=0; a<10-1; a++)    
{
for(b=a+1; b<10; b++){
 if(num[a]>num[b]){
 c=num[a];
num[a]=num[b];
num[b]=c;
         }                    
 cout <<"HERE IS THE NUMBER IN ASCENDING ORDER   " << endl;

}}
for (a=0; a<10; a++)
    cout<< num[a]<< endl;
   return 0;
}
   
This    int num[10],a,b,c; will create an array capable of storing ten numbers into the num variable, along side with a,b,c which are just normal variable for c++.
This    for(int enter=0; enter<10; enter++) {cin >> num [enter];} will loop ten times to collect ten numbers from keyboard and store it in the num array. 
The second and third for loop work together, that is when the FIRST FOR LOOP loop once, the SECOND FOR LOOP will loop 10 times, also when the FIRST FOR LOOP Loop the second time, the SECOND FOR LOOP will loop another 10 time.  
The SECOND FOR LOOP use if(num[a]>num[b]){c=num[a]; num[a]=num[b]; num[b]=c;} by comparing each element or numbers in the array with the IF STATEMENT and swapping bigger number with  smaller number. Accessing the array element and swapping it is possible because it uses the number of times the for loop provide as a means to access the index of the array.
The FOUTH FOR LOOP access the sorted array index with the number the loop provide and print out their values.