C++ Program to Insert Ten Elements in Array and Sort Them

cprogramming

Insert ten elements in an array. Write a program using the function for arranging ten entered numbers into ascending as well as descending order and display the order along with the numbers entered passing array as an argument in the function.   Source Code, 

#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];
}
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(int i=0;i<y;i++)
{
cout<<"ascending order :"<<x[i]<<"n";
}
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(int i=0;i<y;i++)
{
cout<<"decending order:"<<x[i]<<"n";
}

}

Output

enter number of elements in array:10
enter the value of elements of array:3
4
5
6
7
6
5
4
5
4
ascending order :3
ascending order :4
ascending order :4
ascending order :4
ascending order :5
ascending order :5
ascending order :5
ascending order :6
ascending order :6
ascending order :7
descending order:7
descending order:6
descending order:6
descending order:5
descending order:5
descending order:5
descending order:4
descending order:4
descending order:4
descending order:3

TutsMaster.org

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.