Skip to content

Instantly share code, notes, and snippets.

@fsalehpour
Created June 8, 2011 04:35
Show Gist options
  • Save fsalehpour/1013775 to your computer and use it in GitHub Desktop.
Save fsalehpour/1013775 to your computer and use it in GitHub Desktop.
Sorting using array in an object
/*
* P:#33
* Write a programme to sort n numbers.
*/
#include <iostream.h>
#include <conio.h>
class Array
{
int *a, n, i;
public:
void get(int);
void sort();
void outdata();
};
void Array::get(int x)
{
n = x;
a = new int[n];
cout << "Enter " << n << " numbers one by one:\n";
for (i = 0; i < n; i++)
{
cout << "> ";
cin >> a[i];
}
}
void Array::sort()
{
int j;
for(i = 0; i < n; i++)
for(j=i+1; j<n; j++)
{
if(a[i] > a[j])
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
void Array::outdata()
{
for(i=0;i<n;i++)
cout << a[i] << "\n";
}
void main()
{
Array ar;
int s;
clrscr();
cout << "Enter how many numbers: ";
cin >> s;
ar.get(s);
ar.sort();
ar.outdata();
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment