0

I tried to build a heap and finally print the elements in the form of an array.

Here it is the code (I know this doesn't really make sense but I just wanted to test my knowlwdge of heap and dynamic arrays):

#include <stdio.h>
#include <stdlib.h>
void heapiify(int *arr,int n, int i)
{
    int largest=i;
    int l=2*i+1;    // left node
    int r= 2*i+2;   // right node
    if(l<=n && *arr[l]>=*arr[i])
        largest=l;
    if (r <=n && *arr[r]<=*arr[i])
        largest= r;
    if(largest !=i)
    {
        int temp=*arr[i];
        *arr[i]=*arr[largest];
        *arr[largest]=temp;
    }
    heapify(*arr,n,largest);
}

void buildh(int *arr,int n,int r,int c)
{
    int i;
    for(i=n/2-1;i>=0;i--)
        heapify(*arr,n,i);
   output(*arr,r,c);
}

void output(int *arr,int r,int c)
{
    int i,j;
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("%d",*arr[i*c+j]);
        }
        printf("\n");
    }
}

int main()
{
    int i,j,r,c;

    printf("enter the number of rows");
    scanf("%d",&r);
    printf("enter the number of columns");
    scanf("%d",&c);

    int n=r*c;
    int *arr=malloc(n*sizeof(int));
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
            scanf("%d",&arr[i*c+j]);
    }

    buildh(*arr,n,r,c);
}

I'm getting 9 errors which are all the same

invalid argument type of unary '*'( have int)

2 Answers 2

1

Your arr variable is of type pointer to int:

int *arr=malloc(n*sizeof(int));

So when you call buildh, which takes the same type, you have to pass it as-is:

buildh(arr,n,r,c);

Same for the other cases.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks! in what circumstances should we pass *arr as an argument to any function,generally asking.
@supernova2706 Generally speaking, when the argument of the function is of type int (in case of int * arr). When it is an array, like in your question, pass *arr when you need to pass the value of the first element of the array. Please note that *(arr+i) is equivalent to arr[i]
@supernova2706 You're welcome! You are most likely to see the *arr as an argument when you have a pointer to a single variable (rather than an array), and you need to give the function the value. More rarely, you will see it as the equivalent of arr[0], as @RobertoCaboni explained.
0

The problem is the dereference of arr, across your funtions in multiple places, and the passing of dereferenced *arr in your functions to int * parameters, you should pass arr, try:

//...
void heapify(int *arr, int n, int i)
{
    int largest = i;
    int l = 2 * i + 1; // left node
    int r = 2 * i + 2; // right node
    if (l <= n && arr[l] >= arr[i])  //here
        largest = l;
    if (r <= n && arr[r] <= arr[i])  //here
        largest = r;
    if (largest != i)
    {
        int temp = arr[i];  //here
        arr[i] = arr[largest]; //here
        arr[largest] = temp; //here
    }
    heapify(arr, n, largest); //here
}
void buildh(int *arr, int n, int r, int c)
{
    int i;
    for (i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);    //here
    output(arr, r, c);         //here
}
void output(int *arr, int r, int c)
{
    int i, j;
    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            printf("%d", arr[i * c + j]); //here
        }
        printf("\n");
    }
}

int main()
{
    //...
    buildh(arr, n, r, c); //here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.