File: /home/saiesh/ADS/Assignment No 9/ads9.cpp
#include<iostream>
#include<stdlib.h>
using namespace std;
class heap
{
int i,j,temp;
public:
void accept(int a[20],int n);
void display(int *a,int n);
void build_maxheap(int *a,int n);
void build_minheap(int *a,int n);
void max_heapify(int *a,int i,int n);
void min_heapify(int *a,int i,int n);
};
void heap::accept(int marks[20],int n)
{
for(i=1;i<=n;i++)
{
cout<<"Enter the marks for roll number "<<i<<"\t: ";
cin>>marks[i];
}
cout<<"\nEntered marks are\t\t\t: ";
for(i=1;i<=n;i++)
{
cout<<marks[i]<<"\t";
}
}
void heap::build_maxheap(int *a,int n)
{
for(i=n/2;i>=1;i--)
{
max_heapify(a,i,n);
}
}
void heap::build_minheap(int *a,int n)
{
for(i=n/2;i>=1;i--)
{
min_heapify(a,i,n);
}
}
void heap::max_heapify(int *a,int i,int n)
{
temp=a[i];
j=2*i;
while(j<=n)
{
if(j<n&&a[j+1]>a[j])
j=j+1;
if(temp>a[j])
break;
else if(temp<=a[j])
{
a[j/2]=a[j];
j=j*2;
}
a[j/2]=temp;
return;
}
}
Page 1 of 3File: /home/saiesh/ADS/Assignment No 9/ads9.cpp
Page 2 of 3
void heap::min_heapify(int *a,int i,int n)
{
temp=a[i];
j=2*i;
while(j<=n)
{
if(j<n&&a[j+1]<a[j])
j=j+1;
if(temp<a[j])
break;
else if(temp>=a[j])
{
a[j/2]=a[j];
j=j*2;
}
a[j/2]=temp;
return;
}
}
int main()
{
heap obj;
int ch,n,i,x,marks[20];
char ans;
cout<<"\n\tSE ONLINE EXAMINATION\n";
do
{
cout<<"\n\t\tMENU";
cout<<"\n1.Accept the marks";
cout<<"\n2.Maximum score";
cout<<"\n3.Minimum Score";
cout<<"\n4.Exit";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter the number of students\t\t: ";
cin>>n;
obj.accept(marks,n);
break;
case 2: obj.build_maxheap(marks,n);
cout<<"\nMax heap is\t\t: ";
for(i=1;i<=n;i++)
{
cout<<marks[i]<<"\t";
}
cout<<"\nHighest score is\t: ";
cout<<marks[1];
break;
case 3: obj.build_minheap(marks,n);
cout<<"\nMin heap is\t\t: ";
for(i=1;i<=n;i++)
{
cout<<marks[i]<<"\t";
}
cout<<"\nMinimum score is\t: ";
cout<<marks[1];
break;
case 4: exit(0);
}
cout<<"\nDo you want to continue? ";
cin>>ans;
}while(ans=='y'||ans=='Y');
return 0;
}File: /home/saiesh/ADS/Assignment No 9/ads9.cpp
/*
Page 3 of 3
OUTPUT
saiesh@saiesh :~$ g++ ads9.cpp
saiesh@saiesh :~$ ./a.out
SE ONLINE EXAMINATION
MENU
1.Accept the marks
2.Maximum score
3.Minimum Score
4.Exit
Enter your choice: 1
Enter
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
the
number of
marks for
marks for
marks for
marks for
marks for
students
roll number
roll number
roll number
roll number
roll number
1
2
3
4
5
Entered marks are
Do you want to continue? y
:
:
:
:
:
:
5
40
35
68
90
55
: 40 35 68
40 68 35 55
90 68 40 55
MENU
1.Accept the marks
2.Maximum score
3.Minimum Score
4.Exit
Enter your choice: 2
Max heap is
: 90
Highest score is
: 90
Do you want to continue? y
MENU
1.Accept the marks
2.Maximum score
3.Minimum Score
4.Exit
Enter your choice: 3
Min heap is
: 35
Minimum score is
: 35
Do you want to continue? y
MENU
1.Accept the marks
2.Maximum score
3.Minimum Score
4.Exit
Enter your choice: 4
saiesh@saiesh :~$ */
90
55
#include<iostream>
#include<stdlib.h>
using namespace std;
class heap
{
int i,j,temp;
public:
void accept(int a[20],int n);
void display(int *a,int n);
void build_maxheap(int *a,int n);
void build_minheap(int *a,int n);
void max_heapify(int *a,int i,int n);
void min_heapify(int *a,int i,int n);
};
void heap::accept(int marks[20],int n)
{
for(i=1;i<=n;i++)
{
cout<<"Enter the marks for roll number "<<i<<"\t: ";
cin>>marks[i];
}
cout<<"\nEntered marks are\t\t\t: ";
for(i=1;i<=n;i++)
{
cout<<marks[i]<<"\t";
}
}
void heap::build_maxheap(int *a,int n)
{
for(i=n/2;i>=1;i--)
{
max_heapify(a,i,n);
}
}
void heap::build_minheap(int *a,int n)
{
for(i=n/2;i>=1;i--)
{
min_heapify(a,i,n);
}
}
void heap::max_heapify(int *a,int i,int n)
{
temp=a[i];
j=2*i;
while(j<=n)
{
if(j<n&&a[j+1]>a[j])
j=j+1;
if(temp>a[j])
break;
else if(temp<=a[j])
{
a[j/2]=a[j];
j=j*2;
}
a[j/2]=temp;
return;
}
}
Page 1 of 3File: /home/saiesh/ADS/Assignment No 9/ads9.cpp
Page 2 of 3
void heap::min_heapify(int *a,int i,int n)
{
temp=a[i];
j=2*i;
while(j<=n)
{
if(j<n&&a[j+1]<a[j])
j=j+1;
if(temp<a[j])
break;
else if(temp>=a[j])
{
a[j/2]=a[j];
j=j*2;
}
a[j/2]=temp;
return;
}
}
int main()
{
heap obj;
int ch,n,i,x,marks[20];
char ans;
cout<<"\n\tSE ONLINE EXAMINATION\n";
do
{
cout<<"\n\t\tMENU";
cout<<"\n1.Accept the marks";
cout<<"\n2.Maximum score";
cout<<"\n3.Minimum Score";
cout<<"\n4.Exit";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter the number of students\t\t: ";
cin>>n;
obj.accept(marks,n);
break;
case 2: obj.build_maxheap(marks,n);
cout<<"\nMax heap is\t\t: ";
for(i=1;i<=n;i++)
{
cout<<marks[i]<<"\t";
}
cout<<"\nHighest score is\t: ";
cout<<marks[1];
break;
case 3: obj.build_minheap(marks,n);
cout<<"\nMin heap is\t\t: ";
for(i=1;i<=n;i++)
{
cout<<marks[i]<<"\t";
}
cout<<"\nMinimum score is\t: ";
cout<<marks[1];
break;
case 4: exit(0);
}
cout<<"\nDo you want to continue? ";
cin>>ans;
}while(ans=='y'||ans=='Y');
return 0;
}File: /home/saiesh/ADS/Assignment No 9/ads9.cpp
/*
Page 3 of 3
OUTPUT
saiesh@saiesh :~$ g++ ads9.cpp
saiesh@saiesh :~$ ./a.out
SE ONLINE EXAMINATION
MENU
1.Accept the marks
2.Maximum score
3.Minimum Score
4.Exit
Enter your choice: 1
Enter
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
the
number of
marks for
marks for
marks for
marks for
marks for
students
roll number
roll number
roll number
roll number
roll number
1
2
3
4
5
Entered marks are
Do you want to continue? y
:
:
:
:
:
:
5
40
35
68
90
55
: 40 35 68
40 68 35 55
90 68 40 55
MENU
1.Accept the marks
2.Maximum score
3.Minimum Score
4.Exit
Enter your choice: 2
Max heap is
: 90
Highest score is
: 90
Do you want to continue? y
MENU
1.Accept the marks
2.Maximum score
3.Minimum Score
4.Exit
Enter your choice: 3
Min heap is
: 35
Minimum score is
: 35
Do you want to continue? y
MENU
1.Accept the marks
2.Maximum score
3.Minimum Score
4.Exit
Enter your choice: 4
saiesh@saiesh :~$ */
90
55
Comments
Post a Comment