Question
I need help displaying this array as a minheap, specifically getting rid of extra zeroes at the front of the array. The instructions are as
I need help displaying this array as a minheap, specifically getting rid of extra zeroes at the front of the array. The instructions are as follows:
First write a method to read the input data into an array representing a heap. The input data is the Test case input. Return the size of the heap. A pointer to the heap array is a parameter of readheap.
Then write a method to remove an item from the heap.
Then write a method to print the heap. Print the data as shown in the Test case output.
Sample Input 1:
1 4 3 10 12 6
Sample Output 1:
3 4 6 10 12
Sample Input 2:
1 4
Sample Output 2:
4
Sample Input 3:
1 4 3 6 5
Sample Output 3:
3 4 5 6
The code I have so far is this :
#include
#include
#include
using namespace std;
int z, i;
int temp = 0;
int array[7] = {0, 0, 0, 0, 0, 0, 0};
int readheap(int * theheap)
{
for (z=0; z<7; z++) {
cin >> array[z];
}
for (i=0; i<7; i++) {
if (array[i] > temp)
temp = array[i];
}
}
void heapRemove(int * theheap, int size)
{
int n = 7;
int j, temp;
for(i=0;i { for(j=i+1;j { if(array[i]>array[j]) { temp=array[i]; array[i]=array[j]; array[j]=temp; } } } } void heapPrint(int * theheap, int size) { for (int i = 0; i<7; i++){ std::cout << array[i] << " "; } } int main() { int * theheap = new int[10]; int size = readheap(theheap); heapRemove(theheap, size); heapPrint(theheap, size); } My output sorts the array but doesn't remove the extra zeroes, because the input doesn't tell you the size of the array in advance, which I'm not sure how to work around. Any help would be greatly appreciated, thanks
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