Write a C++ program to sort an array of integer in ascending order using a function called exchange( ) which accepts two integer arguments by reference.

#include<iostream.h>
#include<conio.h>
void exchange(int (&a)[],int &n);
int main()
{

int a[10],size;
clrscr();
cout<<"Enter the Array size : ";
cin>>size;
cout<<"Enter the Array elements :\n";
for(int i=0;i<size;i++)
cin>>a[i];
exchange(a,size);
cout<<"After sorting :\n";
for(i=0;i<size;i++)
cout<<a[i]<<endl;
getch();
return 0;
}
void exchange(int (&a)[],int &n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(a[i]<a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

Output:
Enter the Array size: 10
Enter the Array elements :
15
46
89
62
-5
-78
0
5
45
9
After sorting :
-78
-5
0
5
9

15
45
46
62
89


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: