Program Code:
#include<iostream.h>
#include<conio.h>
main()
{
int a[50],size;
char ch;
cout<<"Enter size of array\n";
cin>>size;
cout<<"Enter the order of sorting\n";
cout<<"Enter a for ascending or d for decending\n";
cin>>ch;
cout<<"Enter elements of array\n";
for(int i=0;i<size;i++)
{
cin>>a[i];
}
for(int i=0;i<size-1;i++)
{
for(int j=i+1;j<size;j++)
{
if(a[i]<a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"The sorted array is\n";
if(ch=='d')
{
for(int i=0;i<size;i++)
{
cout<<a[i];
}
}
else if(ch=='a')
{
for(int i=size-1;i>=0;i--)
{
cout<<a[i]<<"\t";
}
}
getch();
}
Output:
0 comments:
Post a Comment