Question
For this exercise you are to implement a heap, using an array. The heap will be built by repeatedly: 1. Reading an input from a
For this exercise you are to implement a heap, using an array.
The heap will be built by repeatedly:
1. Reading an input from a file (in this case an integer)
2. Adding it after the last element of the current heap
3. Sift it up into the correct position, to make a min heap.
At the end of all the inputs you are to print out the first five elements of the min heap array. You should then convert the array into a max heap by calling makeheap and again print the first 5 elements. Your heap should be able to hold 100 integers. A pseudo?code outline for the program is given below:
Begin main
display a prompt for the file name
read in the file name
try to open the file
if (the file fails to open)
print an error message on the screen and exit
fi
while we can read an int from the file
insert the int into the array (to make a min heap)
elihw
close the file
print the first 5 elements of the heap
makeheap() (to make a max heap)
print the first 5 elements of the heap
rof
End main
-
Do not implement the heap using a class or with STL. The heap must be implemented using a fixed size array of ints (500 entries should be enough). The heap array and the index to the last item in the heap should be global variables.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started