Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:36:47+00:00 2026-05-30T12:36:47+00:00

#include <stdio.h> void Heapify(int num[], int start, int end) { int root = start;

  • 0
#include <stdio.h>

void Heapify(int num[], int start, int end)
{
    int root = start;
    while(root*2+1<=end)
    { // at least one child exists
        int swap = root;
        int lchild = root*2+1;
        int rchild = root*2+2;
        if(num[swap]<num[lchild]){
            swap = lchild;
        }
        if(rchild<=end && num[swap]<num[rchild]){
            swap = rchild;
        }
        if(swap!=root){
                // swap here
            int temp = num[root];
            num[root] = num[swap];
            num[swap] = temp;
            root = swap;
        }
        else
            return;
    }
}

void buildHeap(int num[]) {
    int length=sizeof(num)/sizeof(num[0]);
    int start = (length/2)-1; // Starting from last parent
    int end = length-1;
    while(start>=0){
        Heapify(num,start, end);
        if(start==0)
            break;
        start= start-1;            
    }
}

void heapsort(int num[]) {
       int length=sizeof(num)/sizeof(num[0]);
        printf("length = %d ", length);   // length = 1 (Wrong)
    buildHeap(num);
    int i;  
    //for (i = 0; i < length; i++)
        //printf("%d ",num[i]);
    int end = length-1;
    while(end>0){
            // swap first elem with last
        int temp = num[0];
        num[0] = num[end];
        num[end] = temp;
        Heapify(num,0,end-1);
        end--;
    }   
}

int main() {
    int num[]={1,7,-32,4,101,-99,16,3};
    heapsort(num);

    return 0;
}

http://codepad.org/zcfNOtye

When I print it in main, the length is showing correct but inside the function(heap sort), its showing wrong. I can’t find any mistakes in passing the array. What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:36:48+00:00Added an answer on May 30, 2026 at 12:36 pm

    Arrays decay to pointers when passed as parameters, you need to pass the length of the array as a separate parameter.

    I.e.: you can’t find the length of the array like this.

    void buildHeap(int num[]) {
       int length=sizeof(num)/sizeof(num[0]);
    } 
    

    sizeof(num) will return sizeof(int*).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include stdio.h void Square(int num, int *myPointer); int main(int argc, const char *argv[]) {
#include <stdio.h> int main(void) { char *num = 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749
#include stdio.h #include conio.h void swap(int *x,int *y); void main() { int a=10,b=20; swap(a,b);
#include<stdio.h> void foo(int **arr) { arr[1][1]++; } main() { int arr[20][20]; printf(%d\n,arr[1][1]); foo((int**)arr); printf(%d\n,arr[1][1]);
#include<stdio.h> void function(int); int main() { int x; printf(Enter x:); scanf(%d, &x); function(x); return
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.