Write a program that will read 10 integers from user and store them in an array. Implement array using pointers. The program will print the array elements in ascending and descending order.
#include<iostream.h>
#include<conio.h>
class sort
{
int *a;
public:
sort();
~sort();
void print();
void printa();
void printb();
};
sort::sort()
{
a=new int[10];
cout<<"enter 10 integers to store"<<endl;
for(int i=0;i<=9;i++)
cin>>a[i];
}
sort::~sort()
{
delete[] a;
cout<<"Deallocating memory given to array"<<endl;
}
void sort::printa()
{
cout<<"sorting the array in ascending
order"<<endl;
for(int i=0;i<=9;i++)
{
for(int j=i+1;j<=9;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"SORTED IN ASCENDING ORDER"<<endl;
print();
}
void sort::printb()
{
cout<<"sorting the array in descending
order"<<endl;
for(int i=0;i<=9;i++)
{
for(int j=i+1;j<=9;j++)
{
if(a[i]<a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"SORTED IN DESCENDING ORDER"<<endl;
print();
}
void sort::print()
{
cout<<endl<<"The array is "<<endl;
for(int i=0;i<=9;i++)
cout<<a[i]<<",";
cout<<endl;
}
void main()
{
clrscr();
sort obj;
obj.print();
obj.printa();
obj.printb();
getch();
}
Output:-
Comments
Post a Comment