Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Recursion in C (http://www.go4expert.com/forums/recursion-in-c-t17913/)

shirrrrrrrrrrr 9Jun2009 02:51

Recursion in C
 
I need help to solve this problem:

The function gets two
sorted arrays (together with their sizes), and fills the
third array using the contents of the two arrays, so that
the third array is also sorted

what is the stopping condiation? I am stuck and dont know how to start...

xpi0t0s 9Jun2009 03:28

Re: Recursion in C
 
Why do you need recursion for that simple task?
Let's say the arrays contain { 1,2,4,6,10} and {3,7,8,12}.
The result will be {1,2,3,4,6,7,8,10,12}.
Try doing it on paper a few times, you'll see how to do it then. That's how I solve programming puzzles.

shirrrrrrrrrrr 9Jun2009 11:27

Re: Recursion in C
 
Quote:

Originally Posted by xpi0t0s (Post 49213)
Why do you need recursion for that simple task?
Let's say the arrays contain { 1,2,4,6,10} and {3,7,8,12}.
The result will be {1,2,3,4,6,7,8,10,12}.
Try doing it on paper a few times, you'll see how to do it then. That's how I solve programming puzzles.


yes, i solve it in a simple way but now i need to solve it with recursion...i am a beginer in C progrming and I don't know how to start writing it...what should bee my stoping condition? or how do i tell the function what to to? any way thank you fot your help

SaswatPadhi 9Jun2009 11:42

Re: Recursion in C
 
If you ask how to fix a clock with a sledgehammer, probably nobody will be interested to help you (though it might be possible).

Try to fix a clock with screwdriver. ;)

xpi0t0s 9Jun2009 12:54

Re: Recursion in C
 
Why do you need to solve it with recursion? Is this an assignment/homework/coursework?
Anyway, all loops can be replaced with tail recursion, for a description of this see http://en.wikipedia.org/wiki/Tail_recursion

mayjune 15Jun2009 21:14

Re: Recursion in C
 
as far as i can think, stopping condition whould be
if (index of 3rd array is equal to the sum of the first two array)
return final_array;

if i get the code, will upload it....

SaswatPadhi 16Jun2009 13:30

Re: Recursion in C
 
Hi man !

If you are still struggling to fix the clock with sledgehammer [;)], here is something that I wrote today :

Code: C

void RecursiveCombine(int A[], int SizeA, int B[], int SizeB, int C[], bool InitialCall=true)
{
    static int IndexA, IndexB, IndexC;

    if( InitialCall )   IndexA = IndexB = IndexC = 0;

    if( IndexA < SizeA )
        if( IndexB < SizeB )
            if( A[IndexA] < B[IndexB] )     C[IndexC++] = A[IndexA++];
            else                            C[IndexC++] = B[IndexB++];
        else
            C[IndexC++] = A[IndexA++];
    else
        if( IndexB < SizeB )                C[IndexC++] = B[IndexB++];
        else                                return;

    RecursiveCombine(A, SizeA, B, SizeB, C, false);
}


You can should call it this way :
Code: C

RecursiveCombine(X, sX, Y, sY, Z);

or this way :
Code: C

RecursiveCombine(X, sX, Y, sY, Z, true);

Hope that helps.


All times are GMT +5.5. The time now is 13:02.