1. ## Heapify

I've tried to make a few different heapify functions, but none of them have worked. Why isn't this actually doing anything?

Code:
```void heapify(int array[], int i) {
int largest = 0;
int temp = 0;
int n = sizeof(array);
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);
}

}```

2. > int n = sizeof(array);
Are you expecting this to tell you how many elements there are in an array?

It's just telling you the size of the pointer (to the start of the array).

You need to pass the actual size of the array as another parameter.

3. Originally Posted by Salem
You need to pass the actual size of the array as another parameter.
Or you can do
template<typename N>
void foo(int arr[N])

Or even
template<typename N>
void foo(int (&arr)[N])
(Not tested)

Where you will have N as the size of your array.
You will likely get a compile error if you try to pass in a pointer, so you're all set.

4. Originally Posted by Elysia
Or even
template<typename N>
void foo(int (&arr)[N])
(Not tested)
typename should be std::size_t.

Originally Posted by Elysia
Or you can do
template<typename N>
void foo(int arr[N])
You would have to explicitly specify the template argument anyway, so you might as well pass a pointer argument instead.

5. Ah, error on my part. So it should be

template<std::size_t N>
void foo(int arr[N])

template<std::size_t N>
void foo(int (&arr)[N])

It should not require having to specify N in this case. The compiler should be able to deduce that if arr is an int array passed to the function.

6. Originally Posted by Elysia
It should not require having to specify N in this case. The compiler should be able to deduce that if arr is an int array passed to the function.
Yeah, my comment about explicitly specifying the template argument was with respect to the one where the array would be converted to a pointer to its first element, since the N in the parameter is then not considered.

7. Ah, you may be right. I don't have a compiler to test right now >_<

8. Originally Posted by Elysia
Ah, you may be right. I don't have a compiler to test right now >_<

9. Thank you for all of the responses!

However, when I tried to do this the simplest way that I could (simply making n equal to the size of the array I'm testing for), it still doesn't do anything.
I've got it set up to make an array of random numbers between 1 and 99. Here's running it and printing the array before heapify:
Code:
```***** WELCOME TO LINELL'S HEAP OF STUFF! *****
Let me print the array of random numbers:
39
35
5
2
89
48
27
66
70
61```
Now I'll run the heapify and this should change the order to reflect the array becoming a heap. However, all I get for output is the exact same array.

11. 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);
}

}```

12. Walk through what's supposed to happen in heapify when you call it with the parameters you do: i=0. It doesn't loop.

13. What should I pass through to it? I've tried passing a nine through, which should work, right? Same problem.

14. You need to understand the algorithm. You don't have it quite right.