-2

I'm new to the C world. I'm using Visual 2010. I need to create an array from 2 other arrays, or a function to merge them; I come from PHP so I'm sorry if this is stupid. I tested some loop without success..

a real example could be helpful:

int arrayA[5] = {3,2,1,4,5} 
int arrayB[5] = {6,3,1,2,9}

And the printed expected output of the third arrayC should be :

arrayC {
[3][6]
[2][3]
[2][1]
[4][2]
[5][9]
}
jess
  • 337
  • 3
  • 13
  • 1
    What is the code in Php? C code is remarkable similar – Ed Heal Jun 13 '15 at 21:53
  • 3
    Where is the loop you tested without success? – Weather Vane Jun 13 '15 at 21:55
  • 2
    It is unclear whether you want to create a 2-D array programmatically, or just print the two input arrays side by side. – Weather Vane Jun 13 '15 at 21:57
  • 1
    This is not a website for personalised introductory training of the basics; It is a Q&A for the ages! Time to read your C book... – Lightness Races in Orbit Jun 13 '15 at 22:03
  • im sorry always can be a beginer one day no ?! – jess Jun 13 '15 at 22:14
  • @user4406273 When you write that want to merge the inputs, do you mean that the order is in a 1-D array as in the number of items in `arrayC` is equal to the sum of the number of items in each of `arrayA` and `arrayB`? Or do you mean that you want a 2-D array, where `arrayC[0][0]` is the same as `arrayA[0]` and `arrayC[0][1]` is the same as `arrayB[0]`? Or perhaps you want a 1-D array that merely combines them as in `int arrayC[] = {3,2,2,4,5, 6,3,1,2,9};`, and you just want to the output to appear as if it's `{3,6,2,3,2,1,4,2,5,9}`? –  Jun 13 '15 at 22:51
  • _(continued)_ Obviously there are a few ways of doing this in C, so please explain how you want the array structured, not just how you want the output to look. –  Jun 13 '15 at 22:51

4 Answers4

2

A straightforward approach can look the following way

#include <stdio.h>

#define N   5

int main( void )
{
    int a[N] = { 3, 2, 2, 4, 5 }; 
    int b[N] = { 6, 3, 1, 2, 9 };
    int c[N][2];

    for ( size_t i = 0; i < N; i++ )
    {
        c[i][0] = a[i]; c[i][1] = b[i];
    }

    for ( size_t i = 0; i < N; i++ ) printf( "%d, %d\n", c[i][0], c[i][1] ); 

    return 0;
} 

The program output is

3, 6
2, 3
2, 1
4, 2
5, 9

If you want to write a function that will merge arrays of any size then it can look like

#include <stdio.h>
#include <stdlib.h>

#define N   5

int ** merge( int *a, int *b, size_t n )
{
    int **c = malloc( n * sizeof( int * ) );

    if ( c != NULL )
    {
        size_t i = 0;
        for ( ; i < n && ( c[i] = malloc( 2 * sizeof( int ) ) ); i++ )
        {
            c[i][0] = a[i]; c[i][1] = b[i];
        }

        if ( i != n )
        {
            while ( i-- ) free( c[i] );
            free( c );
            c = NULL;
        }
    }

    return c;
}   

int main( void )
{
    int a[N] = { 3, 2, 2, 4, 5 }; 
    int b[N] = { 6, 3, 1, 2, 9 };
    int **c;

    c = merge( a, b, N );

    if ( c != NULL )
    {
        for ( size_t i = 0; i < N; i++ ) printf( "%d, %d\n", c[i][0], c[i][1] ); 
        for ( size_t i = 0; i < N; i++ ) free( c[i] );
        free( c );
    }

    return 0;
} 

The program output will be the same as shown above.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Really It's unclear to all. I had understood like this.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int arrayA[5] = {3,2,2,4,5};
    int arrayB[5] = {6,3,1,2,9};

    int arrayC[5][5];
    int i,j;

    for(i=0; i<5; i++)
    {
        int a = arrayA[i]*10 + arrayB[i];
        arrayC[i][0] = a;
    }

    for(i=0; i<5; i++)
    {
        printf("%d ", arrayC[i][0]);
        printf("\n");
    }

    return 0;
}

After your comment:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int arrayA[5] = {3,2,2,4,5};
    int arrayB[5] = {6,3,1,2,9};

    int arrayC[5];
    int i,j;

    for(i=0; i<5; i++)
    {
        arrayC[arrayA[i]] = arrayB[i];
    }

    for(i=0; i<5; i++)
    {
        printf("[%d %d]",arrayA[i], arrayC[arrayA[i]]);
        printf("\n");
    }

    return 0;
}
Mr. Perfectionist
  • 2,605
  • 2
  • 24
  • 35
  • im sorry , i don't want to add values , i need to take each values in arrayA to make key of arrayC and putt arrayB value in this arrayC [3] will the key and [6] the value , [2]will the second key and [3] the second values – jess Jun 13 '15 at 22:38
0

Please edit your question and specify it (you can read https://stackoverflow.com/help/how-to-ask ).

If you know size of the array, you can create 2D array in that way:

int array[2][5] = { {2, 3, 4, 5}, {6, 3, 1, 2, 9} };

Also take a look to malloc function. This is how to create dynamic 2D array

# create array of two pointers
int **tab = (int**) malloc(sizeof(int*) * 2);
# create pointer to array
tab[0] = (int*) malloc(sizeof(int) * 5);
tab[1] = (int*) malloc(sizeof(int) * 5);

tab[0][0] = 3;
tab[0][1] = 2;
// ...

tab[1][0] = 6;
tab[1][1] = 3;
tab[1][2] = 1;
// ...

// remember to call free
free(tab[0]);
free(tab[1]);
free(tab);

Of course you should use for loop. I show you only how to create array. Please also take a look at this thread Using malloc for allocation of multi-dimensional arrays with different row lengths

Adam
  • 639
  • 9
  • 22
0

you can do this in c++ if i get what you mean

#include <iostream>
using namespace std;

int main()
{
int arrayA[5] = {3,2,2,4,5};
int arrayB[5] = {6,3,1,2,9};
int arrayC[10];
int a=0;
int b=0;
bool use_a= true;
bool use_b = false;

for ( int i =0  ; i <10 ; i++ )
{
if(use_a){
arrayC[i]=arrayA[a];
use_a=false;
use_b= true;

a++;

}else if(use_b){
arrayC[i]= arrayB[b];
use_b=false;
use_a= true;
b++;
}
}

for(int i =0 ; i <10 ; i++)
cout<<arrayC[i];
return 0;
}