Thread: How to merge two array

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    32

    How to merge two array

    I want to merge the values of two arrays

    For example
    Code:
    x = { 1, 2, 3, 4 }
    y = { 3, 4, 5, 6, 7 }
    z= {1, 2, 3, 4,3,4,5,6,7}

    Code:
    #include<stdio.h>
    
    int main ()
    {
        int i; 
        
        int    x[] = { 1, 2, 3, 4 };
        int y[] = { 3, 4, 5, 6, 7 };
        int z[9];
        
        for (i = 0; i <4; i++)
        {
            &z[i];
        }
    
    
           for (i = 4; i <9; i++)
        {
            &z[i];
        }
            
        for (i = 0; i <9; i++)
        {
           printf("z[%d] = %d\n", i, z[i]);
     
        }
        
        return 0;
    }
    z[0] = 4199136
    z[1] = 4199136
    z[2] = 0
    z[3] = 4200912
    z[4] = 6422240
    z[5] = 6422296
    z[6] = 6422476
    z[7] = 1979435552
    z[8] = -1794567444

    What's wrong with code ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,552
    What do you think &z[i] is really doing? Hint it's not copying an array element from one array into another array.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,162
    Turn up the warning level.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:13:9: warning: statement with no effect [-Wunused-value]
             &z[i];
             ^
    foo.c:19:9: warning: statement with no effect [-Wunused-value]
             &z[i];
             ^
    foo.c:8:9: warning: unused variable ‘y’ [-Wunused-variable]
         int y[] = { 3, 4, 5, 6, 7 };
             ^
    foo.c:7:12: warning: unused variable ‘x’ [-Wunused-variable]
         int    x[] = { 1, 2, 3, 4 };
                ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    32
    Quote Originally Posted by Salem View Post
    Turn up the warning level.
    [code]
    $ gcc -Wall foo.c
    I don't understand what i'm doing wrong
    Code:
    #include<stdio.h> 
    int main ()
    {
        int i; 
         
        int  x[] = { 1, 2, 3, 4 };
        int y[] = { 3, 4, 5, 6, 7 };
        int z[9];
         
        for (i = 0; i <4; i++)
        {
            z[i] = x[i];
        }
     
     
           for (i = 4; i <9; i++)
        {
            z[i] = y[i];
        }
             
        for (i = 0; i <9; i++)
        {
           printf("z[%d] = %d\n", i, z[i]);
      
        }
         
        return 0;
    }
    z[0] = 4199136
    z[1] = 4199136
    z[2] = 0
    z[3] = 4200912
    z[4] = 6422240
    z[5] = 6422296
    z[6] = 6422476
    z[7] = 1979435552
    z[8] = 2069808346

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,910
    The problem is that when you append the second array, you need to start from index 0 for the second array, while starting from index 4 for the result array. Since you start from index 4 for both, you end up accessing the second array out of bounds.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,877
    Code:
    z[i] = y[i];
    likely you want

    Code:
    z[i] = y[i-4];
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    32
    Quote Originally Posted by laserlight View Post
    The problem is that when you append the second array, you need to start from index 0 for the second array, while starting from index 4 for the result array. Since you start from index 4 for both, you end up accessing the second array out of bounds.
    I set index of second array to 0
    Code:
    #include<stdio.h> 
    int main ()
    {
        int i; 
         
        int  x[] = { 1, 2, 3, 4 };
        int y[] = { 3, 4, 5, 6, 7 };
        int z[9];
         
        for (i = 0; i <4; i++)
        {
            z[i] = x[i];
        }
     
     
           for (i = 0; i <9; i++)
        {
            z[i] = y[i-4];
        }
             
        for (i = 0; i <9; i++)
        {
           printf("z[%d] = %d\n", i, z[i]);
      
        }
         
        return 0;
    }
    z[0] = 4199136
    z[1] = 4199136
    z[2] = 0
    z[3] = 4200912
    z[4] = 6422240
    z[5] = 6422296
    z[6] = 6422476
    z[7] = 1979435552
    z[8] = -957751498

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,877
    0-4 equals -4 and -4 is not a valid array index.
    Last edited by stahta01; 03-03-2020 at 09:29 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    714
    Quote Originally Posted by stahta01 View Post
    0-4 equals -4 and -4 is not a valid array index.
    Well... it depends...
    Code:
    int a[4], *p = &a[2];
    p[-2] = 0; // valid!. same as a[0].

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    714
    I prefer to deal with pointers:

    Code:
    #define ARRAY_TEMS(a) (sizeof a / sizeof a[0])
    int x[] = { 1, 2, 3, 4 };
    int y[] = { 6, 7, 8, 9 10 };
    int z[9]; // must have enough space to both x[] and y[] arrays.
    int i;
    
    int *p, *q;
    
    q = z;
    
    p = x;
    for ( i = 0; i < ARRAY_ITEMS(x); i++)
      *q++ = *p++;
    
    p = y;
    for ( i = 0; i < ARRAY_ITEMS(y); i++)
      *q++ = *p++;
    
    for ( i = 0; i < ARRAY_ITEMS(z); i++)
      printf( "z[%d] = %d\n", i, z[i] );
    Last edited by flp1969; 03-04-2020 at 06:55 AM.

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Here is a solution that does not use pointers and gets to the heart of the issue, element positions are not the same in the second loop


    Code:
    /*Merge2Arraystoa3rd.c*/
    
    #include<stdio.h>
    
    int main ()
    {
        int i;
    
        int x[] = { 1, 9, 3, 4 };
        int y[] = { 7, 2, 5, 8, 6 };
    //    int z[9];
        int z[10]; // a C string must have one extra element for the "\0" //See Strings in C - GeeksforGeeks
        for (i = 0; i <4; i++)
        {
            //&z[i];
            z[i] = x[i]; // each element in array x will copy into the same array element position in z
        }                // but this is not the same in the next iteration
    
        for (i = 0; i <5; i++) // Element position 0 in y is equal to element position 4 in z
        {
            //&z[i];
            z[i+4] = y[i];
        }
    
        for (i = 0; i <9; i++)
        {
           printf("z[%d] = %d\n", i, z[i]);
        }
        return 0;
    }[

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merge - array sort on linked list
    By rcgldr in forum C Programming
    Replies: 6
    Last Post: 10-18-2014, 05:33 PM
  2. Merge and sort array.
    By Alexie in forum C Programming
    Replies: 17
    Last Post: 01-30-2013, 10:57 AM
  3. Runtime Hangup with Merge Sort of 2D array
    By orlileithian in forum C Programming
    Replies: 12
    Last Post: 10-28-2012, 12:25 PM
  4. Merge Sort (Array)
    By Jack Hammer in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2011, 02:37 PM
  5. sorting - merge -
    By dayknight in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2006, 09:11 PM

Tags for this Thread