Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
#include <stdio.h>
#include <stdlib.h>

#define MAX 20

void BuildHeap(int[], int);
void heapify(int[], int, int);
void swap(int *, int *);

void swap(int *x, int *y)
{

    int temp = *x;
    *x = *y;
    *y = temp;
}
int main()
{

        
    int n, i;
    printf("Enter Array Size\n");
    scanf("%d", &n);

    int arr[n];
    printf("Enter Array Elements\n");
    for (i = 0; i < n; i++)
        scanf("%d", arr[i]);

    BuildHeap(arr, n);

    printf("Sorted array is given in the following way \n");
    for (i = 0; i < n; i++)
        printf("%d", arr[i]);
    printf("\n");
}
void BuildHeap(int arr[], int size)
{
    int i;
    for (i = size / 2; i >= 0; i--)
        heapify(arr, i, size);
}

void heapify(int arr[], int index, int size)
{
    int left = 2 * index + 1;
    int right = left + 1;
    int max = index;

    if (left <= size && arr[left] > arr[max])

        max = left;

    if (right <= size && arr[right] > arr[max])

        max = right;

    if (index != max)
    {
        swap(&arr[max], &arr[index]);
        heapify(arr, max, size);
    }
}


What I have tried:

i'ive tried to changes but nothing happning
Posted
Updated 14-Jul-22 22:40pm
v2
Comments
CHill60 15-Jul-22 4:22am    
You need to list the specific problem(s) you are having

Quote:
i tried so many things but still not able to fix the errors.

What about stopping trying random things and rather watch your code preforming ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
The array declaration in the following code is not allowed in C. You can only declare fixed size arrays.
C++
scanf("%d", &n);

int arr[n];

If you need a variable length array then you should use malloc thus:
C++
int* arr = malloc(sizeof(int) * n); // allocate s[pace for n integers


Also, your scanf further down needs to be modified:
C++
for (i = 0; i < n; i++)
    scanf("%d", arr + i); // address of the next element
 
Share this answer
 
v2
Comments
OriginalGriff 15-Jul-22 4:42am    
C99 allowed variable sized arrays to be declared: https://www.tutorialspoint.com/initialization-of-variable-sized-arrays-in-c#:~:text=Variable%20sized%20arrays%20are%20data,that%20allows%20variable%20sized%20arrays.
Richard MacCutchan 15-Jul-22 4:59am    
Compiling with MS compiler and options -std:C11 or std:C17 it is still not allowed.
merano99 15-Jul-22 19:15pm    
In C, VLAs have been allowed for a long time. However, they are not without controversy, since one has no control over what happens when memory is not available. The questioner has not yet said whether he uses Visual-Studio, nor has he given a C standard. Whether VLAs are a possible cause of errors here is not clear.

Microsoft states that Visual-Studio supports C99 with a few exceptions, but this is not true, for example, in this point. see: docs.microsoft.com

GCC had VLA as an extension before C99, one that also extends into its C++ dialect.
In C++ it is expected to use a vector in these cases anyway.

The topic is also discussed here: Link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900