Untitled

 avatar
irfan
plain_text
2 years ago
721 B
2
Indexable
6) Write a program to sort the numbers in an array using separate functions for read, display, sort and swap.
 
#include<iostream>
using namespace std;
void read (int a[ ], int &n)
{
cout<<"Enter the number of elements:"<<endl;
cin>>n;
cout<<"Enter the elements: "<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void sort(int a[ ], int n)
{
for(int i=0;i<n;i++)
{
for (int j=0; j<n-1;j++)
{
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
}
} 
void disp(int a[ ], int n)
{
cout<<"THE SORTED ARRAY IS: "<<endl;
for (int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
int main()
{
int a[10],n;
read(a,n);
sort(a,n);
disp(a,n);
return 0;
}
Editor is loading...
Leave a Comment