Thread: recursion array

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    5

    recursion array

    hi guys!!!
    this is my first post and I am fairly new programming in C,
    I wanted to ask about a problem. I have 2 array of numbers and I have to merge them with each other recursively orderly, not how to do it, thanks for a good guide, help me to think about, thanks

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    24
    can you give a sample input and output so it'll be easy to understand?

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    arrayA[4,44,54,1,65,65] arrayB[8,87,4,23,12,433] and together arrayC[1,4,4,12,23,44,54,65,65,87,433] with a loop or recursion, the name that you like

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Easy.

    Make sure arrayC is large enough to hold both arrayA and arrayB (i.e. length is at least the sums of the lengths of arrayA and arrayB).

    Copy arrayA to arrayC, starting at the beginning. Starting one past the last element in ArrayC that was copied from ArrayA, copy arrayB to arrayC.

    Sort the array in ascending order. Remove any duplicate values (if that is required - your example suggests it isn't).

    And, no, I'm not going to provide code. The description above is a big enough hint on what you need to do.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    24
    Quote Originally Posted by Gordo Miguel View Post
    arrayA[4,44,54,1,65,65] arrayB[8,87,4,23,12,433] and together arrayC[1,4,4,12,23,44,54,65,65,87,433] with a loop or recursion, the name that you like
    with loop it'll be like this:
    Code:
     int A[5]={1,3,5,7,9}, B[]={2,4,6,8,10}, C[10];
    
    int count=0;
    
    for(int i=0;i<5;i++)
    
    	{
    
    		C[i]=A[i];
    
    		count++;
    
    	}
    
    for(int i=0;i<5;i++)
    
    	{
    
    		C[count]=B[i];
    
    		count++;
    
    	}
    
    
    for(int i=0;i<10;i++)
    
       {
    
    	   printf("%d ",C[i]);
    
       } 
    After merging you can do the sorting using any sorting algorithm, hopefully you'll be able to do that.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    thanx guys, i dont want the code, but the ideas help me a lot, now doing with a recursion it heavier
    Last edited by Gordo Miguel; 04-28-2013 at 08:02 AM.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    well, finish the code.
    I can do it with recursion?? the juntarArreglos function??

    Code:
    #include <stdio.h>
    #define MAX 5
    #define MAX2 10
    
    
    void inicializo(int *a, int *);
    void cargarArreglos(int *a, int *b);
    void juntarArreglos(int *a, int *b);
    void mostrar(int *c);
    
    
    int main(){
    int a[MAX];
    int b[MAX];
    int c[MAX2];
    int i=0;
    
        inicializo(a,b);
        cargarArreglos(a,b);
        juntarArreglos(a,b);
    
    
    
    return 0;
    }
    
    /*Inicializo el arreglo */
    void inicializo(int *valores, int *valoresb)
    {
    int i;
    int valorinicial=-1;
    
        for(i=0; i<MAX; i++)
        {
            valores[i]=valorinicial;
            valoresb[i]=valorinicial;
            }
    }
    
    void cargarArreglos(int *A, int *B){
    int i,j;
    
            printf("Cargue el primer arreglo");
                for (i=0;i<MAX ; i++ )
                scanf("%d", &A[i]);
    
            printf("cargue el segundo arreglo");
                for (j=0;j<MAX;j++ )
                scanf("%d",&B[j]);
    
    }
    
    void mostrar(int *c){
    
    int i=0;
        for (i=0;i<MAX2 ;i++ ){
          printf("valor = %d\n", c[i]);
        }
    
    }
    
    
    
    void juntarArreglos(int *A, int *B){
    
    int i1=0;
    int i2=0;
    int cont=0;
    int C[MAX2];
    
        for(i1=0;i1<MAX && i2<MAX;cont++){
            if(A[i1]<B[i2]){
                C[cont]=A[i1];
                i1++;
                }else{
                C[cont]=B[i2];
                i2++;
                }
            }
    
    
        for(i1=0 ;i1<MAX;i1++)
                C[cont]=A[i1];
                cont++;
        for(i2=0 ;i2<MAX;i2++)
                C[cont]=B[i2];
                cont++;
    
        mostrar(C);
    
    }

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by dotnet13 View Post
    with loop it'll be like this:
    Code:
     int A[5]={1,3,5,7,9}, B[]={2,4,6,8,10}, C[10];
    
    int count=0;
    
    for(int i=0;i<5;i++)
    
        {
    
            C[i]=A[i];
    
            count++;
    
        }
    
    for(int i=0;i<5;i++)
    
        {
    
            C[count]=B[i];
    
            count++;
    
        }
    
    
    for(int i=0;i<10;i++)
    
       {
    
           printf("%d ",C[i]);
    
       } 
    After merging you can do the sorting using any sorting algorithm, hopefully you'll be able to do that.
    You shouldn't give away answers like that.

    You can use recursion if you want to, but using a loop would be a better solution.
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Gordo Miguel View Post
    well, finish the code.
    How did you test it?

    Code:
    $ ./foo
    Cargue el primer arreglo1 2 3 4 5
    cargue el segundo arreglo6 7 8 9 10
    valor = 1
    valor = 2
    valor = 3
    valor = 4
    valor = 5
    valor = 5
    valor = 10
    valor = 0
    valor = 3882996
    valor = -1080235202
    You have a buffer overflow in juntarArreglos():
    Code:
    void juntarArreglos(int *A, int *B){
    
    int i1=0;
    int i2=0;
    int cont=0;
    int C[MAX2];
    
        for(i1=0;i1<MAX && i2<MAX;cont++){
            if(A[i1]<B[i2]){
                C[cont]=A[i1];
                i1++;
                }else{
                C[cont]=B[i2];
                i2++;
                }
            }
    
    
        for(i1=0 ;i1<MAX;i1++)
                C[cont]=A[i1];
                cont++;
        for(i2=0 ;i2<MAX;i2++)
                C[cont]=B[i2];
                cont++;
    There are two problems with your algorithm.
    1) You assume A and B are already sorted.
    2) After you filled up the new array C until one of either A or B is exhausted, you again add all elements from both A and B to C which causes the buffer overflow. (Hint: should you set both "i1" and "i2" back to 0?)

    I suggest you reread grumpy's post and try to implement that algorithm (i.e don't mix the merging with the sorting).

    Quote Originally Posted by Gordo Miguel View Post
    I can do it with recursion?? the juntarArreglos function??
    I'm not sure what you mean but there are several sorting algorithms like mergesort and quicksort which use recursion.

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Click_here i test that answer, but doesnt work like i want.

    AndiPersti in my pc works fine, but i recognize the mistake, i will study a little more

    Thanx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing an array using recursion
    By danishzaidi in forum C Programming
    Replies: 2
    Last Post: 12-31-2011, 01:52 AM
  2. finding max in array using RECURSION
    By wonderwall in forum C Programming
    Replies: 5
    Last Post: 10-27-2011, 09:46 AM
  3. A recursion array scanner
    By steelsoul in forum C Programming
    Replies: 4
    Last Post: 12-12-2010, 06:12 PM
  4. Replies: 8
    Last Post: 06-14-2009, 01:39 PM
  5. creating combinations of array values - recursion??
    By johndirect in forum C Programming
    Replies: 2
    Last Post: 11-20-2008, 12:49 PM