Here it is in its entirety:
Code:
/*
Linell
Due on 11/11/11 <- I'm totally getting Skyrim
at 11:59 tonight.
*/
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
// The following functions will be used to make
// and manipulate the heap.
int parent(int);
int left(int);
int right(int);
void heapify(int[], int);
int main() {
int daheap[100];
cout << "***** WELCOME TO LINELL'S HEAP OF STUFF! *****";
cout << " Let me print the array of random numbers: ";
srand(time(NULL));
for (int i = 0; i < 10; i++) {
daheap[i] = rand() % 100;
}
for (int i = 0; i < 10; i++) {
cout << endl << daheap[i];
}
heapify(daheap, 0);
cout << "\n\n\n Alright, now for daheap sorted! :) ";
for (int i = 0; i < 10; i++) {
cout << endl << daheap[i];
}
getchar();
}
// Returns the parent of a certain node
int parent(int i) {
return (i/2);
}
// Returns the left child of a parent
int left(int i) {
return (2*i);
}
// Returns the right child of a parent
int right(int i) {
return (2*i+1);
}
// Heapifys an array
void heapify(int array[], int i) {
int largest = 0;
int temp = 0;
int n = 10;
int l = left(i);
int r = right(i);
if (l <= n && array[l] > array[i]) {
cout << "\n\nHERE IS THE DAMN PROBLEM";
largest = l;
}
else {
cout << "\n\nOkay, the else statement is working.";
largest = i;
}
if (r <= n && array[i] > array[largest]) {
cout << "\n\nOkay, largest = r.";
largest = r;
}
if (largest != i) {
cout << "\n\nAlright, this got down to the last step.";
temp = array[i];
array[i] = array[largest];
array[largest] = temp;
heapify(array, largest);
}
}