Tuesday, 8 October 2024

Journal Program 5

 /*

Def: WAP to build a class MyArray having integer array as

a data member and following member fuctions:

(a)getData() : To get n values for integer array

(b)sort_asc() : To sort an array in ascending order

(c)sort_desc() : To sort an array in descending order

(d)putData() : To print all values

Write a menu driven program for sorting either ascending

and descending order and also for exit from a program.

Tut Date:

Lab Date:


*/

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<process.h>

class MyArray

{

int a[10];

int i,j,n,temp;

public:

void getData();

void sort_asc();

void sort_des();

void putData();

};

void MyArray::getData()

{

cout<<"Enter no of total elements(Maximum 10) : ";

cin>>n;


for(i=0;i<n;i++)

{

cout<<"a["<<i<<"]=";

cin>>a[i];

}

}

void MyArray::sort_asc()

{

for(i=0;i<n;i++)

{

for(j=0;j<n-1;j++)

{

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

}

void MyArray::sort_des()

{

for(i=0;i<n;i++)

{

for(j=0;j<n-1;j++)

{

if(a[j]<a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}


}

void MyArray::putData()

{

for(i=0;i<n;i++)

{

cout<<"\n a["<<i<<"]="<<a[i];

}

}


int main()

{

clrscr();

MyArray m;

int choice;

char ch;

m.getData();

do

{

cout<<"\n 1:Ascending Order";

cout<<"\n 2:Descending Order";

cout<<"\n 3:Exit";

cout<<"\n Enter your choice=";

cin>>choice;

switch(choice)

{

       case 1:

m.sort_asc();

m.putData();

break;

       case 2:

m.sort_des();

m.putData();

break;

       case 3:

exit(0);

break;

default:

cout<<"\n Invalid choice";

}

cout<<"\n Do you want to cont...";

cin>>ch;

}while(ch=='y'||ch=='Y');

return 0;

}

No comments:

Post a Comment