Week-6 Algorithm: (OOP Through C++ Lab) Write A C++ Program To Sort An Array of Integer Numbers
Week-6 Algorithm: (OOP Through C++ Lab) Write A C++ Program To Sort An Array of Integer Numbers
Algorithm
1.Declare array
2.read the array size
3.read the array elements
4.call the sort function
5.display array elements after sorting
Sort Function algorithm:
1.take the temp variable
2.sort element by element ,and then swap
3.return control back to main function
PROGRAM
#include<iostream.h>
#include<conio.h>
void sort (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];
sort(a,size); /* sorting function calling */
cout<<"After sorting elements are :\n";
for(i=0;i<size;i++)
cout<<a[i]<<endl;
getch();
return 0;
}
void sort(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;
}
}
INPUT & OUTPUT