0% found this document useful (0 votes)
11 views

DSA Assignment 10

The document contains a C++ program that implements a heap data structure to manage and display student marks. It allows for the insertion of marks into both a max heap and a min heap, enabling the retrieval of maximum and minimum marks. The program prompts the user for the number of students and their respective marks, then outputs the sorted marks for both the highest and lowest values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

DSA Assignment 10

The document contains a C++ program that implements a heap data structure to manage and display student marks. It allows for the insertion of marks into both a max heap and a min heap, enabling the retrieval of maximum and minimum marks. The program prompts the user for the number of students and their respective marks, then outputs the sorted marks for both the highest and lowest values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment No.

10

Program :

#include<iostream>
using namespace std;

class hp
{
int heap[20],heap1[20],x,n1,i;
public:
hp()
{ heap[0]=0; heap1[0]=0;
}
void getdata();
void insert1(int heap[],int);
void upadjust1(int heap[],int);
void insert2(int heap1[],int);
void upadjust2(int heap1[],int);
void minmax();
};
void hp::getdata()
{
cout<<"\n Enter the no. of students : ";
cin>>n1;
cout<<"\n Enter the marks : ";
for(i=0;i<n1;i++)
{ cin>>x;
insert1(heap,x);
insert2(heap1,x);
}
}
void hp::insert1(int heap[20],int x)
{
int n;
n=heap[0];
heap[n+1]=x;
heap[0]=n+1;

upadjust1(heap,n+1);
}
void hp::upadjust1(int heap[20],int i)
{
int temp;
while(i>1&&heap[i]>heap[i/2])
{
temp=heap[i];
heap[i]=heap[i/2];
heap[i/2]=temp;
i=i/2;
}
}
void hp::insert2(int heap1[20],int x)
{
int n;
n=heap1[0];
heap1[n+1]=x;
heap1[0]=n+1;

upadjust2(heap1,n+1);
}
void hp::upadjust2(int heap1[20],int i)
{
int temp1;
while(i>1&&heap1[i]<heap1[i/2])
{
temp1=heap1[i];
heap1[i]=heap1[i/2];
heap1[i/2]=temp1;
i=i/2;
}
}
void hp::minmax()
{
cout<<"\n Max marks : "<<heap[1];
cout<<"\n";
for(i=1;i<=n1;i++)
{ cout<<"\n"<<heap[i]; }
cout<<"\n\n Min marks : "<<heap1[1];
cout<<"\n";
for(i=1;i<=n1;i++)
{ cout<<"\n"<<heap1[i]; }
}
int main()
{
hp h;
h.getdata();
h.minmax();
return 0;
}

OUTPUT:
Enter the no. of students : 5

Enter the marks: 10 24 6 8 9


Max marks : 24

24
10
6
8
9

Min marks : 6

6
8
10
24
9

You might also like