100% found this document useful (1 vote)
33 views

Week-6 Algorithm: (OOP Through C++ Lab) Write A C++ Program To Sort An Array of Integer Numbers

The document describes a C++ program to sort an integer array. It includes an algorithm to read in an array, call a sort function, and display the sorted array. The sort function uses a nested for loop to compare array elements and swap any out of order using a temporary variable. The sample input has the user enter 5 random numbers which are then output sorted in ascending order.

Uploaded by

Lavanya_123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
33 views

Week-6 Algorithm: (OOP Through C++ Lab) Write A C++ Program To Sort An Array of Integer Numbers

The document describes a C++ program to sort an integer array. It includes an algorithm to read in an array, call a sort function, and display the sorted array. The sort function uses a nested for loop to compare array elements and swap any out of order using a temporary variable. The sample input has the user enter 5 random numbers which are then output sorted in ascending order.

Uploaded by

Lavanya_123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Week-6

(OOP through C++ Lab) R13 --2-1

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

enter the array size:


5
enter the array elementS:
83971
after sorting elements are :
1
3
7
8
9

You might also like