1

I tried to make several functions which passes a heap as a parameter through the code below. However, it did not turned out to be what I expected it to be.

#include<stdio.h>

void upHeap_min2 (int *heap, int index)
{
    if (index == 0)
        return;

    int parentIdx = getParentIdx(index);
    if (heap[index] < heap[parentIdx])
    {
        int temp = heap[index];
        heap[index] = heap[parentIdx];
        heap[parentIdx] = temp; 

        upHeap_min2(heap, parentIdx);
    }
}

void pushValue (int *heap, int count, int value)
{
    count++;
    heap[count] = value;
    upHeap_min2(heap, count);   
}

void view(int *heap, int count)
{
    printf("Values inside heap: ");
    for (int i = 0; i < count; i++)
    {
        printf("%d ", heap[i]);
    }
    printf("\n");
}

int main()
{
    int heapDemo[101];
    int count = -1;
    pushValue(heapDemo, count, 30);
    pushValue(heapDemo, count, 20);
    pushValue(heapDemo, count, 40);
    pushValue(heapDemo, count, 90);
    pushValue(heapDemo, count, 10);

    view(heapDemo, count);

    return 0;
}

Function to get parent index:

int getParentIdx (int index)
{
    return (index-1)/2;
}

The code above should have printed

10 20 40 90 30

But instead it printed nothing. I have also thought to pass it as a double pointer as well but i did not work. Does this mean I cannot pass a heap as a parameter (which means i have to declare the heap as a global variable) or there is another way to do this?

Brian989
  • 15
  • 5
  • 1
    I'm curious why you do not pass `count` as a pointer on line `pushValue(heapDemo, count, 30);`. Am I missing sth? Wouldn't you need yo pass `count` via pointer, like `pushValue(heapDemo, &count, 30);` – ssd May 31 '20 at 11:38

2 Answers2

3

Your pushValue function takes the count argument by value (which means the function receives a copy of the data), so it is never modified in the main function. Instead, you should pass count as a pointer, and (thus) will need to dereference it inside the function:

void pushValue(int* heap, int* count, int value)
{
    ++(*count);
    heap[*count] = value;
    upHeap_min2(heap, *count);
}

Then, in main, you should call it using the address of count:

    pushValue(heapDemo, &count, 30); // And similarly for the other calls

Also, the loop in your view function stops one short of the end. Change the loop limit to i <= count. (This function uses count but doesn't modify it, so passing by value is OK.):

void view(int* heap, int count)
{
    printf("Values inside heap: ");
    for (int i = 0; i <= count; i++) {
        printf("%d ", heap[i]);
    }
    printf("\n");
}

Feel free to ask for further clarification and/or explanation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • May I ask... Why did you use `++(*count)` instead of `(*count)++` ? – Brian989 May 31 '20 at 12:00
  • @Brian989 In this case, there's no difference in the outcome. I was just doodling around with the code, made that change (more my style than anything else) and neglected to change it back. One reason I think that the pre-increment is better, though, is if you 'forget' the brackets: Although `++*count` will work, `*count++` will not! – Adrian Mole May 31 '20 at 12:04
  • Thanks for the help @Adrian. I appreciate it. – Brian989 May 31 '20 at 13:12
1

This is a valid solution, let me explain

#include<stdio.h>

void upHeap_min2 (int *heap, int index)
{
    if (index == 0)
        return;

    int parentIdx = getParentIdx(index);
    if (heap[index] < heap[parentIdx])
    {
        int temp = heap[index];
        heap[index] = heap[parentIdx];
        heap[parentIdx] = temp; 

        upHeap_min2(heap, parentIdx);
    }
}
int getParentIdx (int index)
{
    return (index-1)/2;
}

void pushValue (int *heap, int *count, int value)
{
    *count = *count + 1;
    heap[*count] = value;
    upHeap_min2(heap, *count);   
}

void view(int *heap, int *count)
{
    printf("Values inside heap: ");
    for (int i = 0; i <= *count; i++)
    {
        printf("%d ", heap[i]);
    }
    printf("\n");
}

int main()
{
    int heapDemo[101];
    int conter = -1; //new var
    int *count = &conter;
    pushValue(heapDemo, count, 30);
    pushValue(heapDemo, count, 20);
    pushValue(heapDemo, count, 40);
    pushValue(heapDemo, count, 90);
    pushValue(heapDemo, count, 10);

    view(heapDemo, count);

    return 0;
}

The main error is that you were passing counter as simple variable, and you should do it as a pointer, this causes that counter never get incremented, so I created a pointer to that variable and now counter is incremented and it prints what you expect. In addition I have modified the body of the for you were using to print the values

Josep
  • 162
  • 1
  • 9
  • 1
    This does not explain why passing `count` rather than `&count` did not work. That may be obvious to you, but a person asking about this does not have that knowledge, so an answer should provide it. – Eric Postpischil May 31 '20 at 11:41
  • Ok. The fact is that if you pass count, you are passing the variable as value, so it will pass a copy of the variable to the method, and the original variable ```count``` will notnever be increased. If you pass a reference ```&count```, now you are increasing ```count``` variable. Here you can find more info: https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – Josep May 31 '20 at 11:45
  • Thanks @Josep! I literally thought that I passed the array improperly.... It worked just like what you said. – Brian989 May 31 '20 at 11:53