Skip to main content

Thread: how to int pass array to functions


here program compiling , running without syntax errors.how ever not sort array.the problem lies in passing array in function
code:
    #include<stdio.h>     #include<string.h>     int partition (int *,int,int);     void quicksort (int *,int,int);     static int call=0;     int main()     {     int i,j,choice;     int length;     int a[]={81, 12, 90, 3, 49, 108, 47};     i=0;      length=sizeof(a)/sizeof(a[0]);     quicksort(a,0,length-1);     printf("the sorted array is\n");     for(i=0;i<length;i++)      printf (" %d ",a[i]);     }     int partition(int *num,int p,int r)     {      int x,j,i,temp,bak;       x=num[r];       i=-1;            for(j=0;j<=r-1;j++)       {               if(num[j]<=x)          {           i=i+1;            temp=num[i];            num[i]=num[j];            num[j]=temp;                       {            printf(" %d",num[bak]);             }                 }	       }       num[i+1]=num[r];          return i+1;     }          void quicksort (int *num,int p,int r)     {       int q;      if (p<r)       {     	call++;         	q=partition(num,p,r);             quicksort(num,p,q-1);             quicksort(num,q+1,r);       }     }
the above way of passing array in functions right want know because giving problem in function partition.

inside function partition when swapping happens tried printing array there (it not sorted array see upto point things reached) saw 2 or 3 elements of array had passed being printed , rest of array lost where.so doubt array not being passed properly.



able see problem array passing in function wrote smaller program ka1.c

code:
    #include<stdio.h>     void pass(int *);     int main ()     {     int a[]={3,5,61,32,12};     pass(a);     }     void pass (int *num)     {     int i,j;      j=sizeof(num)/sizeof(num[0]);      (i=0;i<j;i++)      printf(" %d",num[i]);     }
now when run above code output just

code:
     3 5
i expecting complete array printed in output.
if notice rest of array of second program ka1.c not getting printed.where did go?
have used same logic in quicksort hence feel error same in both cases.

use gcc-4.4.3 on 64 bit machine.

checked length of array being passed in quicksort.c parititon function
code:
    int a[]={81, 12, 90, 3, 49, 108, 47};
when array of 7 length passed partition function,it receives array of length 2.
checked in partition function as
code:
length=sizeof(num)/sizeof(num[0]);
the length returned 2 instead of 7.
same case ka1.c program.where program unable find out length of array being passed.
why problem , fault such way of passing array's?

you have pass in length of array function. can't compute pointer first element of array. information isn't there compute it.

value of j in function pass wrong; pass int function length of array , it'll fine.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] how to int pass array to functions


Ubuntu

Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support