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 sort(int a[],int n);
void main()
{
clrscr();
int n,a[10];
cout<<"enter number of elements in array";
cin>>n;
cout<<"\n enter the value of elements of array";
for(int 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";
sort(a,n);
getch();
}
void sort(int x[],int y)
{
for(int i=0;i<y;i++)
{
for(int j=0;j<y;j++)
{
if(x[i]<x[j])
{
int temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
for(i=0;i<y;i++)
{
cout<<x[i]<<"\n";
}
}
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 sort(int a[],int n);
void main()
{
clrscr();
int n,a[10];
cout<<"enter number of elements in array";
cin>>n;
cout<<"\n enter the value of elements of array";
for(int 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";
sort(a,n);
getch();
}
void sort(int x[],int y)
{
for(int i=0;i<y;i++)
{
for(int j=0;j<y;j++)
{
if(x[i]<x[j])
{
int temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
for(i=0;i<y;i++)
{
cout<<x[i]<<"\n";
}
}
0 comments:
Post a Comment