Page 1 of 1

IN C++, Using Notepad ++, Compiled in Developer Command Prompt for VS 2019. Don't answer this question if you are not go

Posted: Tue Jul 12, 2022 8:11 am
by answerhappygod
IN C++, Using Notepad ++, Compiled in Developer CommandPrompt for VS 2019. Don't answer this question if you arenot going to use and edit the code I havedone.
For this assignment fill an array of 10 integers with randomvalues ranging from 0 to 100.
While it is a good idea to test your functions in a mainprogram, I will be writing my own to test your code with, so you donot need to submit one yourself. Please submit your heap.h andheap.cpp files, keeping the names of the files and functionsprecise, along with the number of parameters specified.
Code for heap.h
#ifndef HEAP_H#define HEAP_H
void insertHeap(int heap[], int key, int &lastIndex);void reheapUp(int heap[], int locIndex);void deleteHeap(int heap[], int &lastIndex);void reheapDown(int heap[], int locIndex, int lastIndex);void heapify(int heap[], int &lastIndex, int size);void printHeap(int heap[], int lastIndex);
#endif
Code done for heap.cpp
#include "heap.h"#include <iostream>using namespace std;
void insertHeap(int heap[], int key, int &lastIndex){lastIndex++;heap[lastIndex] = key;reheapUp(heap, lastIndex);}
void reheapUp(int heap[], int locIndex){if(locIndex == 0) //Indicates we're at the root. Exitrecursion{return;}int parentIndex = (locIndex - 1)/2;if(heap[parentIndex] < heap[locIndex]){int hold = heap[parentIndex];heap[parentIndex] = heap[locIndex];heap[locIndex = hold];reheapUp(heap, parentIndex);}}
void deleteHeap(int heap[], int &lastIndex){if(lastIndex < 0){cout << "Heap is empty.";return;}heap[0] = heap[lastIndex];lastIndex--;reheapDown(heap, 0, lastIndex);}
void reheapDown(int heap[], int locIndex, int lastIndex){int leftIndex, rightIndex, biggestIndex, leftValue,rightValue;leftIndex = 2*locIndex + 1;if(leftIndex <= lastIndex) //Means left child exists{leftValue = heap[leftIndex];rightIndex = leftIndex + 1;if(rightIndex <= lastIndex){rightValue = heap[rightIndex];}else{//Have left child, but no right child.rightValue = leftValue - 1; //Always less than leftValue}if(leftValue > rightValue){biggestIndex = leftIndex;}else{biggestIndex = rightIndex;}// Check if we should swapif(heap[biggestIndex] > heap[locIndex]){int hold = heap[locIndex];heap[locIndex] = heap[biggestIndex];heap[biggestIndex] = hold;reheapDown(heap, biggestIndex, lastIndex);}}}