0

This is my code, and I am not able to find out where am I going wrong.

void heapify(int arr[], int n)
{
int i=n/2 , j, temp;
for(i=n/2; i>0; i--)
{
    if(arr[2i]<arr[i])
    {
        temp = arr[2i];
        arr[2i]= arr[i];
        arr[i]= temp;
    }
    if(arr[2i+1]<arr[i])
    {
        temp = arr[2i];
        arr[2i]= arr[i];
        arr[i]= temp;
    }
}
printf("Output:\n");
for(j=1; j<=n; j++)
{
    printf("%d ", arr[j]);
}
}

int main()
{
int arr[11]={0,12,54,21,74,1,46,91,13,76,22}, n=10;
heapify(arr, n);
return 0;
}

It throws up the following error:

[Error] invalid types 'int*[__complex__ int]' for array subscript

Please help..Am struggling to implement heapsort here. Thanks in advance

Paul R
  • 208,748
  • 37
  • 389
  • 560
Ankit Gupta
  • 99
  • 2
  • 5

1 Answers1

3

In C 2i is a complex (imaginary) number (the compiler error message is actually telling you exactly this). You need to use the * operator for multiplication. Change all occurrences of arr[2i] to arr[2*i], e.g.

    arr[2i] = arr[i];

should be:

    arr[2*i] = arr[i];
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Thanks a lot! it helped, and it was worth knowing! – Ankit Gupta Sep 02 '15 at 21:05
  • You're welcome - I recommend you get a [good introductory book on C](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and study it so that you can become familiar with the basics of the language - that way you'll be more productive and learn faster. – Paul R Sep 02 '15 at 21:09