Group E Assignment 22: Heap DS

 Problem Statement :

Read the marks obtained by students of second year in an online examination of

particular subject. Find out maximum and minimum marks obtained in that subject. Use

heap data structure. Analyze the algorithm.


Program :

    #include<iostream>

#include<math.h>

using namespace std;

# define max1 20



class stud

{

public:

int marks[max1],total;

stud()

    {

    for(int i=0;i<max1;i++)

      marks[i]=0;

    }

void createHeap();

void displayHeap();

void showmax();

void showmin();

};


void stud::createHeap()

{

int i,j,par,temp,M;

cout<<"\n Enter How many Stu : ";

cin>>total; //5

for(i=0;i<total;i++)  //0-4=5 times

{

cout<<"\n Enter Marks :  ";

cin>>marks[i];

M=marks[i];

j=i;//j is child

par=floor((j-1)/2);

    while(marks[j] < marks[par] && j!=0)

   {

    temp=marks[j];

    marks[j]=marks[par];

    marks[par]=temp;

    j=par;

    par=floor((j-1)/2);

    }

cout<<"\n \n Current Heap : After Inserting : " <<M<<" is : \n ";

displayHeap();

}


}


void stud::displayHeap()

{

int i=0,space=6;

cout<<endl;

while(i<total)

{

    if(i==0 || i==1 || i==3 || i==7 || i==15)

    {

    cout<<endl<<endl;

    for(int j=0;j<space;j++)

         cout<<" ";

    space-=2;   

    }

   cout<<" "<<marks[i];i++;

         

}

}




void stud::showmin()

{

cout<<marks[0];

}


void stud::showmax()

{

int max,i;

max=marks[0];

for(i=1;i<total;i++)

{

if(max < marks[i])

max=marks[i];

}

cout<<max;

}



int main()

{

stud s1;

int ch, ans;

do

{

cout<<"\n 1. Insert Marks ";

cout<<"\n 2. Display Marks ";

cout<<"\n 3. Show Max Marks ";

cout<<"\n 4. Show Min Marks ";

cout<<"\n\n Enter Your Choice : ";

cin>>ch;

switch(ch)

{

case 1: 

s1.createHeap();

break;

case 2:

s1.displayHeap();

break;

case 3: s1.showmax();

break;

case 4: s1.showmin();

break;

}

cout<<" \n Do u want to continue : (1 for continue )";

cin>>ans;

}while(ans==1);

return 0;


}



/* OUTPUT------------------------------------------------------------------------------------------

 1. Insert Marks

 2. Display Marks

 3. Show Max Marks

 4. Show Min Marks


 Enter Your Choice : 1


 Enter How many Stu : 6


 Enter Marks :  50



 Current Heap : After Inserting : 50 is :




       50


     0 0


   0 0 0

 Enter Marks :  40



 Current Heap : After Inserting : 40 is :




       40


     50 0


   0 0 0

 Enter Marks :  30



 Current Heap : After Inserting : 30 is :




       30


     50 40


   0 0 0

 Enter Marks :  20



 Current Heap : After Inserting : 20 is :




       20


     30 40


   50 0 0

 Enter Marks :  10



 Current Heap : After Inserting : 10 is :




       10


     20 40


   50 30 0

 Enter Marks :  70



 Current Heap : After Inserting : 70 is :




       10


     20 40


   50 30 70

 Do u want to continue : (1 for continue )1


 1. Insert Marks

 2. Display Marks

 3. Show Max Marks

 4. Show Min Marks


 Enter Your Choice : 2




       10


     20 40


   50 30 70

 Do u want to continue : (1 for continue )1


 1. Insert Marks

 2. Display Marks

 3. Show Max Marks

 4. Show Min Marks


 Enter Your Choice : 3

70

 Do u want to continue : (1 for continue )1


 1. Insert Marks

 2. Display Marks

 3. Show Max Marks

 4. Show Min Marks


 Enter Your Choice : 4

10

 Do u want to continue : (1 for continue )  2 */


Comments