DESCRIPTION-: This program arrange the elements of an array in ascending order.In this firstly we enter the elements of an array and after entering the elements of array this program sort the elements of an array in ascending order.
FOR EXAMPLE-:
If we have elements of an array as-------------
5 2 12 45 99 9
Then program generate the output as----------
2 5 9 12 45 99
PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a[10],temp;
cout<<"enter number of elements in array";
cin>>n;
cout<<"\n enter the value of elements of array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter value of elements of array are"<<"\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
cout<<"elements in ascending order are"<<"\n";
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
getch();
}
FOR EXAMPLE-:
If we have elements of an array as-------------
5 2 12 45 99 9
Then program generate the output as----------
2 5 9 12 45 99
PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a[10],temp;
cout<<"enter number of elements in array";
cin>>n;
cout<<"\n enter the value of elements of array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter value of elements of array are"<<"\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
cout<<"elements in ascending order are"<<"\n";
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
getch();
}
0 comments:
Post a Comment