Thread: How to merge 2 arrays?

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    How to merge 2 arrays?

    I want to merge array 1 into 2 how can i do that?
    AbHHinaay

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Array A: The array that will receive array B
    Array B: Array that we'll add to array A.

    Be sure that A have enough space to merge with the B.
    Loop until the last element of A and from there, start looping B too and copying from B to A, until the last element of B.

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Make an array that has enough space, then start inserting an element from array 1 and array 2 into the new array, and the way you will insert the elements depends on what do you mean by merge, and that's acheived by using loops, and counters to controll the insertion.
    none...

  4. #4
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Code:
     
    #include<stdio.h>
    
    #define ARRAY_LENGTH                       7
    #define ARRAY_B_LENGTH                   5
    #define ARRAY_A_LENGTH                 (ARRAY_B_LENGTH + ARRAY_LENGTH)
    
    
    int main()
    {
    int array_a[ARRAY_A_LENGTH];
    int array_b[ARRAY_B_LENGTH];
    int i, j;
    
    /* initialize or put value in arrays*/
    for(i = 0, j = 47; i < ARRAY_LENGTH; i++)
    array_a[i] = i + j;
    
    for(i = 0, j = 53; i < ARRAY_B_LENGTH; i++)
    array_b[i] = i + j;
    
    
    /* Display all value of */
    for(i = 0; i < ARRAY_LENGTH; i++)
    printf("array_a[%d]=%d\n", i, array_a[i]);
    
    for(i = 0; i < ARRAY_B_LENGTH; i++)
    printf("array_b[%d]%d\n", i, array_b[i]);
    
    
    
    /*Marge small one with the big*/
    for(i = ARRAY_LENGTH, j = 0; i < ARRAY_A_LENGTH; i++, j++)
    array_a[i] = array_b[j];
    
    
    /* Display marged array */
    for(i = 0; i < ARRAY_A_LENGTH; i++)
    printf("array_a[%d] = %d\n",i, array_a[i]);
    
    return 0;
    
    }
    May get an idea .. .. instead of using two arrays, you may try with third array.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to merge two arrays recursevly..
    By transgalactic2 in forum C Programming
    Replies: 117
    Last Post: 01-11-2009, 04:47 PM
  2. Merge Module Problems
    By mercury529 in forum Windows Programming
    Replies: 0
    Last Post: 11-29-2006, 03:30 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM