anik_1996
anik_1996

Reputation: 31

calling function inside another function (but not recursion)

I have written a code for merge sort in C, but when I call function merge() within mergesort(), compilers shows error. Any reasons why so? I am trying to write mergesort algorithm There's no more details I have written it for the damned condition of stack overflow.

            #include<stdio.h>
            #include<stdlib.h>
            int mergesort(int* A,int n)
            {
                if(n==1)
                   return 0;
                int ln,rn;//to calculate length of left and right arrays
                int *L,*R;//pointer to left and right arrays
                L=(int*)malloc(ln*sizeof(int));
                R=(int*)malloc(rn*sizeof(int));
                ln=n/2;
                rn=n-ln;
                for(int i=0;i<ln;i++)
                {
                    L[i]=A[i];
                }   
                for(int i=0;i<rn;i++)
                {
                    L[i]=A[i+rn];
                }
                mergesort(L,ln);
                mergesort(R,rn);
                merge(A,L,R,n,ln,rn);
                return 0;
            }
            int merge(int*A,int*L,int*R,int n,int ln,int rn)
            {
                int i,j,k;//to access elements of A,L,R respectively
                i=j=k=0;
                while(j<ln&&k<rn)
                {
                    if(L[j]<=R[k])
                    {
                        A[i]=L[j];
                        i++;
                        j++;
                    }
                    else
                    {
                        A[i]=L[k];
                        i++;
                        k++;    
                    }
                }
                while(j<ln)//means R is fully copied to A,but not L
                {
                        A[i]=L[j];
                        i++;
                        j++;    
                }
                while(k<ln)//means L is fully copied to A,but not R
                {
                        A[i]=L[k];
                        i++;
                        k++;    
                }
                return 0;
            }
            int main()
            {
                int* A,i,n;
                //scanf("array size: %d",&n);//dunno it's valid or not
                scanf("%d",&n);
                A=(int*)malloc(n*sizeof(int));
                printf("\n");
                for(i=0;i<n;i++)
                {
                    scanf("%d",&A[i]);
                }
                mergesort(A,n);
                for(i=0;i<n;i++)
                {
                    printf("%d ",A[i]);
                }
                return 0;
            }

enter image description here

Upvotes: 1

Views: 314

Answers (3)

Russell Jonakin
Russell Jonakin

Reputation: 1996

You will need to prototype the merge() function, or define merge() before mergeSort()

here is what you need to add before the mergeSort() function:

int merge(int*,int*,int*,int,int,int);

In prototype definitions you do not need variable names for the parameters, you just need the parameter definitions.

Upvotes: 1

eyalm
eyalm

Reputation: 3366

the merge function is still not known in the scope of mergesort because it appears only after it in the file.

Add a prototype (forward declaration) in the beginning of the file (after the includes):

static int merge(int*A,int*L,int*R,int n,int ln,int rn);

Upvotes: 2

Cioaca Radu
Cioaca Radu

Reputation: 88

In C the order of your functions headers matter.

Just place merge() on top of mergesort() .

Or try to declare the merge() function header on top adding this line after the includes

int merge(int*A,int*L,int*R,int n,int ln,int rn) ;

Upvotes: 0

Related Questions