Arrangement of Element or Members of Array in Ascending Order Using C++ Programming

Ascending order:  To arrange the elements from smaller one to larger one.

Algorithm: Go for comparison.

Recommended Articles: Read these articles for better understanding.


Program code:


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout<<"**********************************************************"<<endl;
cout<<"               Elements in  Ascending order               "<<endl;
cout<<"**********************************************************"<<endl<<endl;
int a[6];
int temp;
char redo;

do
{
cout<<"Enter Elements in any order one by one "<<endl;
for(int i = 0; i<6; i++)
{
cin>>a[i];
}
for(int i= 0;i< 6 ; i++)
{
for(int j = i ; j<6 ;j++)
{
if(a[i]>a[j+1])
       {
        temp = a[i];
       a[i]=a[j+1];
       a[j+1]=temp;
       }
}
}
cout<<endl<<endl<<"Elements in Ascending order are as follows:  "<<endl<<endl;
for(int z = 0 ; z < 6 ; z++)
{
cout<< a[z] << "   ";
    }
    cout<<endl;
    cout<<"**********************************************************"<<endl;
    cout<<"Enter y or Y for next calculation  =   ";
    cin>>redo;
    cout<<endl<<endl;
    
}while(redo =='y' || redo == 'Y');
getch();
}


Output: After compile and run , output will b like this

Click on Pic to Enlarge it .

Hope that you will find this helpful.

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.