简体   繁体   中英

Merge two 2d arrays into one 1d array in C

I have taken two 2d arrays but the output is very different from expected.It should merge two 2d arrays into one 1d array.I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.

    #include <stdio.h>

    void print(int a[][3],int m);


    int main()
    {
    int array1[3][3];
    int array2[3][3];

    int arraySum[6][3];

    int k = 0;  //put into array;
    int l = 10;

    for(int i = 0; i < 3; i++)
    {
       for(int j = 0; j < 3; j++)
       {
           array1[i][j] = ++k;     //fill from 1 to 10
           array2[i][j] = ++l;     //fill from 11 - 19
       }
    }
/*merge arrays*/
    for(int i = 0; i < 6; i++)
    {
       for(int j = 0; j < 3; j++)
       {
           (i < 3) ? arraySum[i][j] = array2[i][j] : arraySum[i][j] =  array1[i-3][j];
           //fill arraySum with array2 and append with array1.
           //just so that arraySum does not have any order
       }
    }

     printf("Arrays before sorting");
     printf("Array 1: ");
      print(array1,3);
     printf("Array2: ");
      print(array2,3);
     printf("arraySum");
      print(arraySum,6);

    /* bubble sort*/
    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 3; j++)
        {
           for(int k = i+1; k < 6; k++)
           {
               for(int m = 0; m < 3; m++)
               {
                   if(arraySum[i][j] > arraySum[k][m])
                   {
                    //swap
                       int temp = arraySum[i][j];
                       arraySum[i][j] = arraySum[k][m];
                       arraySum[k][m] = temp;
                   }
               }
           }
        }
    }
     printf("\n\n Merged Array after sorting");
     print(arraySum,6);
     return 0;
    }

void print(int a[][3],int m)
{
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            printf("%d" , a[i][j]);
        }
    }
}

I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.

Please try this code,To Merge two 2d arrays into one 1d array in C

    #include <stdio.h>

    void print(int a[][3],int m);


    int main()
    {
    int array1[3][3];
    int array2[3][3];

    int arraySum[6][3];

    int k = 0;  //put into array;
    int l = 10;

    for(int i = 0; i < 3; i++)
    {
       for(int j = 0; j < 3; j++)
       {
           array1[i][j] = ++k;     //fill from 1 to 10
           array2[i][j] = ++l;     //fill from 11 - 19
       }
    }
/*merge arrays*/
    for(int i = 0; i < 6; i++)
    {
       for(int j = 0; j < 3; j++)
       {
           (i < 3) ? (arraySum[i][j] = array2[i][j]) : (arraySum[i][j] = array1[i-3][j]);
           //fill arraySum with array2 and append with array1.
           //just so that arraySum does not have any order
       }
    }

     printf("Arrays before sorting");
     printf("Array 1: ");
      print(array1,3);
     printf("Array2: ");
      print(array2,3);
     printf("arraySum");
      print(arraySum,6);

    /* bubble sort*/
    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 3; j++)
        {
           for(int k = i+1; k < 6; k++)
           {
               for(int m = 0; m < 3; m++)
               {
                   if(arraySum[i][j] > arraySum[k][m])
                   {
                    //swap
                       int temp = arraySum[i][j];
                       arraySum[i][j] = arraySum[k][m];
                       arraySum[k][m] = temp;
                   }
               }
           }
        }
    }
     printf("\n\n Merged Array after sorting");
     print(arraySum,6);
     return 0;
    }

void print(int a[][3],int m)
{
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            printf("%d" , a[i][j]);
        }
    }
}

I hope this code will be usefull. Thank you.

I believe you're trying to be too smart with ternary operator, you can do it simpler way:

if (i < 3)
    arraySum[i][j] = array2[i][j];
else
    arraySum[i][j] = array1[i-3][j];

Listen to your compiler it would've told you what was wrong if you've compiled with -Wall -Wextra .

And if you insist on using ternary then this would probably be clearer:

arraySum[i][j] = (i < 3) ? array2[i][j] : array1[i-3][j];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM