Help C How to Convert this programming I really need help on

Help C++

How to Convert this programming, I really need help on this assignment. Can someone teach me how to type this programming?

Programming question:

to template, test with integers, double and char.

//#include <iostream>

using namespace std;

#include <cmath>

class Heaparr {

public:

Heaparr();

void insert(int da);

double getLeft(double i) { return 2 * i + 1; }

double getRight(double i) { return 2 * i + 2; }

double getParent(double i) { return (i - 1) / 2; }

double getMax() { return maxHeap[0]; }

void print();

void reheap(int num);

void makeArray();

void Build_Max_Heap(int heapArray[], int heap_size);

void Max_Heapify(int heapArray[], int i, int heap_size);

void heapSort(int heapArray[], int heap_size);

  

private:

int size;

int* maxHeap;

int index;

double i;

};

Heaparr::Heaparr() {

maxHeap = nullptr;

size = 0;

}

void Heaparr::insert(int da) {

size++;

int* tmp = new int[size];

  

for (int i = 0; i < size - 1; i++) {

tmp[i] = maxHeap[i];

}

  

tmp[size - 1] = da;

delete[] maxHeap;

maxHeap = tmp;

}

void Heaparr::heapSort(int heapArray[], int heap_size) {

size = heap_size;

int n = size;

Build_Max_Heap(heapArray, heap_size);

  

for (int i = n - 1; i >= 1; i--) {

std::swap(heapArray[0], heapArray[i]);

heap_size = heap_size - 1;

Max_Heapify(heapArray, 0, heap_size);

}

}

void Heaparr::Build_Max_Heap(int heapArray[], int heap_size) {

int n = size;

for (int i = floor((n - 1) / 2); i >= 0; i--) {

Max_Heapify(heapArray, i, heap_size);

}

return;

}

void Heaparr::Max_Heapify(int heapArray[], int i, int heap_size) {

// int n = size;

int largest = 0;

int l = getLeft(i);

int r = getRight(i);

  

if ((l < heap_size) && (heapArray[l] < heapArray[i])) {

largest = l;

} else {

largest = i;

}

  

if ((r < heap_size) && (heapArray[r] < heapArray[largest])) {

largest = r;

}

  

if (largest != i) {

std::swap(heapArray[i], heapArray[largest]);

Max_Heapify(heapArray, largest, heap_size);

}

return;

}

int main(int argc, char* argv[]) {

int hArray[31] = {1,3,27,22,18,4,11,26,42,19,6,2,15,16,13};

  

Heaparr t;

t.heapSort(hArray, sizeof(hArray)/sizeof(hArray[0]));

for (auto v : hArray) {

  

std::cout << v << \", \";

}

std::cout << std::endl;

return 0;

}

Solution

#include <iostream>

using namespace std;

#include <cmath>

// I am assuming, you want a \"generic\" class.

template<typename t> class Heaparr {

public:

Heaparr();

void insert(t data);

int getLeft(int i) {

return 2 * i + 1;

}

int getRight(int i) {

return 2 * i + 2;

}

int getParent(int i) {

return (i - 1) / 2;

}

t getMax() {

return maxHeap[0];

}

void print();

void reheap(int num);

void makeArray();

void Build_Max_Heap(t heapArray[], int heap_size);

void Max_Heapify(t heapArray[], int i, int heap_size);

void heapSort(t heapArray[], int heap_size);

private:

int size;

int* maxHeap;

int index;

double i;

};

template<typename t>

Heaparr<t>::Heaparr() {

maxHeap = nullptr;

size = 0;

}

template<typename t>

void Heaparr<t>::insert(t data) {

size++;

t* tmp = new t[size];

for (int i = 0; i < size - 1; i++) {

tmp[i] = maxHeap[i];

}

tmp[size - 1] = data;

delete[] maxHeap;

maxHeap = tmp;

}

template<typename t>

void Heaparr<t>::heapSort(t heapArray[], int heap_size) {

size = heap_size;

int n = size;

Build_Max_Heap(heapArray, heap_size);

for (int i = n - 1; i >= 1; i--) {

std::swap(heapArray[0], heapArray[i]);

heap_size = heap_size - 1;

Max_Heapify(heapArray, 0, heap_size);

}

}

template<typename t>

void Heaparr<t>::Build_Max_Heap(t heapArray[], int heap_size) {

int n = size;

for (int i = floor((n - 1) / 2); i >= 0; i--) {

Max_Heapify(heapArray, i, heap_size);

}

return;

}

template<typename t>

void Heaparr<t>::Max_Heapify(t heapArray[], int i, int heap_size) {

// int n = size;

int largest = 0;

int l = getLeft(i);

int r = getRight(i);

if ((l < heap_size) && (heapArray[l] < heapArray[i])) {

largest = l;

} else {

largest = i;

}

if ((r < heap_size) && (heapArray[r] < heapArray[largest])) {

largest = r;

}

if (largest != i) {

std::swap(heapArray[i], heapArray[largest]);

Max_Heapify(heapArray, largest, heap_size);

}

return;

}

int main(int argc, char* argv[]) {

int hArray[] = { 1, 3, 27, 22, 18, 4, 11, 26, 42, 19, 6, 2, 15, 16, 13 };

Heaparr<int> t; // creating object for int

t.heapSort(hArray, sizeof(hArray) / sizeof(hArray[0]));

for (auto v : hArray) {

std::cout << v << \", \";

}

std::cout << std::endl;

double array[] = { 3.3, 4.5, 4.0, 645.5, 1.9 };

Heaparr<double> t1; // creating object for double

t1.heapSort(array, sizeof(array) / sizeof(array[0]));

for (auto v : array) {

std::cout << v << \", \";

}

std::cout << std::endl;

char carray[] = { \'d\', \'g\', \'t\', \'y\', \'u\' };

Heaparr<char> t2; // creating object for char

t2.heapSort(carray, sizeof(carray) / sizeof(carray[0]));

for (auto v : carray) {

std::cout << v << \", \";

}

std::cout << std::endl;

return 0;

}

Help C++ How to Convert this programming, I really need help on this assignment. Can someone teach me how to type this programming? Programming question: to tem
Help C++ How to Convert this programming, I really need help on this assignment. Can someone teach me how to type this programming? Programming question: to tem
Help C++ How to Convert this programming, I really need help on this assignment. Can someone teach me how to type this programming? Programming question: to tem
Help C++ How to Convert this programming, I really need help on this assignment. Can someone teach me how to type this programming? Programming question: to tem
Help C++ How to Convert this programming, I really need help on this assignment. Can someone teach me how to type this programming? Programming question: to tem
Help C++ How to Convert this programming, I really need help on this assignment. Can someone teach me how to type this programming? Programming question: to tem
Help C++ How to Convert this programming, I really need help on this assignment. Can someone teach me how to type this programming? Programming question: to tem

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site