Getting Sum of 3D Array elements by using pointer - c

I need to Sum the 3D Array by using other function 'int ADD'. I want to pass the Array by using pointer and adding with pointer increase but I'm stuck at passing the Array. Here is my codes.
int(*pA)[COL][HIT] = A;
printf("Sum Of Array A : %d",ADD((*pA)[COL][HIT]);
system("pause");
}
int ADD(int(*pA)[COL][HIT])
{
int sum = 0;
for ((*pA)[COL][HIT] = 0; (*pA)[COL][HIT] < 10 * 7 * 6; (*pA)[COL][HIT]++)
{
sum = sum + (*pA)[COL][HIT];
}
return sum;
}

It seems what you need is something like the following shown in the demonstrative program.
#include <stdio.h>
#define N1 2
#define N2 3
#define N3 4
long long int add( int ( *a )[N2][N3], size_t n )
{
long long int sum = 0;
for ( int ( *p1 )[N2][N3] = a; p1 != a + n; ++p1 )
{
for ( int ( *p2 )[N3] = *p1; p2 != *p1 + N2; ++p2 )
{
for ( int *p3 = *p2; p3 != *p2 + N3; ++p3 ) sum += *p3;
}
}
return sum;
}
int main(void)
{
int a[N1][N2][N3] =
{
{
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 }
},
{
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 }
}
};
printf("Sum Of Array a : %lld", add( a, N1 ) );
return 0;
}
The program output is
Sum Of Array a : 60
Another approach is to reinterpret the 3D array as a 1D array. For example
#include <stdio.h>
#define N1 2
#define N2 3
#define N3 4
long long int add( int *a, size_t n )
{
long long int sum = 0;
for ( int *p = a; p != a + n; ++p )
{
sum += *p;
}
return sum;
}
int main(void)
{
int a[N1][N2][N3] =
{
{
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 }
},
{
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 }
}
};
printf("Sum Of Array a : %lld", add( **a, N1 * N2 * N3 ) );
return 0;
}

Your variable names are confusing, since you are using a pointer to a 2D array (a pointer to a table) consider using row and col instead of COL and HIT
You need to pass both dimensions (rows and cols) to the function and also the number of tables.
Call the function using
ADD(tables, rows, cols, A);
And the prototype of the function should be something like
int ADD(int tables, int rows, int cols, int (*pA)[rows][cols])
Then, in a loop:
for (int i = 0; i < tables; i++)
for (int j = 0; j < rows; j++)
for (int k = 0; k < cols; k++)
sum = sum + pA[i][j][k];

Related

Im trying to find the max and min value and its respective index. However I cant get the indexmin

Find the minimum element of the array and its corresponding index.
I can't get the the minimum index to work. Do I add else statement under each if statement?
#include<stdio.h>
int main()
{
int array[10]={1,2,3,4,5,6,7,8,9,10} , i;
**//finding max and min, and its respective index**
int max = array[0] , min = array[0];
int indmin , indmax;
for( i = 0 ; i < 10 ; i++ )
{
if(array[i] > max)
{
max = array[i];
indmax = i;
}
if(array[i] < min)
{
min = array[i];
indmin = i;
}
}
//print the max and min value and its indexs
printf("\nMaximum element is %d\t index is %d", max , indmax);
printf("\nMinimum element is %d\t index is %d", min , indmin);
}
Initialize indmin and indmax. When defining the array leave out the size so it's derived from the data. When iterating over the array use sizeof(array) / sizeof(*array) to let compiler determine the size of the array instead of hard-coding it. Minimize scope of variable i. Use a function to print output for less duplication:
#include <stdio.h>
void print(const char *prompt, int value, int index) {
printf("%s element is %d\t index is %d\n", prompt, value, index);
}
int main() {
int array[]={1,2,3,4,5,6,7,8,9,10};
int min = array[0];
int indmin = 0;
int max = array[0];
int indmax = 0;
for(int i = 0; i < sizeof(array) / sizeof(*array); i++) {
if(array[i] > max) {
max = array[i];
indmax = i;
}
if(array[i] < min) {
min = array[i];
indmin = i;
}
}
print("Maximum", max, indmax);
print("Minimum", min, indmin);
}
You could refactor this by creating a struct to keep the value and index together:
#include <stdio.h>
struct value_index {
int value;
int index;
};
void print(const char *prompt, struct value_index *vi) {
printf("%s element is %d\t index is %d\n", prompt, vi->value, vi->index);
}
int main() {
int array[]={1,2,3,4,5,6,7,8,9,10};
struct value_index min = { array[0], 0 };
struct value_index max = { array[0], 0 };
for(int i = 0; i < sizeof(array) / sizeof(*array); i++) {
if(array[i] > max.value) {
max.value = array[i];
max.index = i;
}
if(array[i] < min.value) {
min.value = array[i];
min.index = i;
}
}
print("Maximum", &max);
print("Minimum", &min);
}
Or you could realize that you only need the original array along with the two indices. To make my version even better than #Fe2O3's answer, I used a macro to make mine smaller (and if bait works then I will claim mine is easier to read) :-)
#include <stdio.h>
void print(const char *prompt, int *arr, int index) {
printf("%s element is %d\t index is %d\n", prompt, arr[index], index);
}
int main() {
int array[]={1,2,3,4,5,6,7,8,9,10};
int indmin = 0;
int indmax = 0;
for(int i = 0; i < sizeof(array) / sizeof(*array); i++) {
#define CMP_AND_SET(OP, V) if(array[i] OP array[V]) V = i
CMP_AND_SET(<, indmin);
CMP_AND_SET(>, indmax);
#unset CMP_AND_SET
}
print("Maximum", array, indmax);
print("Minimum", array, indmin);
}
Building on #Fe2O3's branchless idea combined with an initialized array which I find to compact and quite readable:
indmin = (int[]) { indmin, i }[array[i] < array[indmin]];
indmax = (int[]) { indmax, i }[array[i] > array[indmax]];
By using (a < b) <=> -1 * (-a > -b) you can write the last one as (note: UB if array contains INT_MIN):
indmax = (int[]) { indmax, i }[-array[i] < -array[indmax]];
I would use a local macro to reduce code duplication by using macro to generate either the first version by passing in the operator (see above) or the 2nd version by passing in a factor F:
#define MINMAX(V, F) V = (int[]) { V, i }[F * array[i] < F * array[V]]
indmin = MINMAX(indmin, 1);
indmax = MINMAX(indmax, -1);
I am totally cheating but you can shuffle the min and max elements to fixed positions within the source array. No storage overhead. This would be the opposite of branchless.
#include <stdio.h>
void print(const char *prompt, int value) {
printf("%8s = %3d\n", prompt, value);
}
int swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
return 0;
}
int main(void) {
int arr[] = { 1, 2, 3, 4, 42, 5, -42, 6, 7, 8, 9, 10 };
const int min = 0;
const int max = sizeof arr/sizeof *arr - 1;
for(int i = 1; i < max + 1; i++ )
arr[i] < arr[min] && swap(arr + i, arr + min) ||
arr[i] > arr[max] && swap(arr + i, arr + max);
print("min", arr[min]);
print("max", arr[max]);
}
Leaving variables uninitialised is asking Demon of Hard-To-Find Bugs to co-author your code. Define variables close to where they are used to increase clarity. And, don't define more variables than you need. (Common beginner mistake to make another copy "just in case"...)
// use the spacebar to increase readability
#include <stdio.h>
int main() {
// let the compiler assign the size of an initialised array
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// use fewer variables
int indmin = 0, indmax = 0;
// don't compare an element (arr[0]) to itself
for( int i = 1 ; i < sizeof array/sizeof array[0]; i++ )
if( array[ i ] > array[ indmax ] )
indmax = i; // updated
else
if( array[ i ] < array[ indmin ] )
indmin = i; // updated
// don't add unnecessary braces (imho)
// this isn't the 17th century in need of needless filligree.
// use '\n' at the END of output. sometimes needed to 'flush' output buffer
printf("Maximum element is %d\t index is %d\n", array[ indmax ] , indmax);
printf("Minimum element is %d\t index is %d\n", array[ indmin ] , indmin);
return 0;
}
Maximum element is 10 index is 9
Minimum element is 1 index is 0
EDIT:
So, there's a friendly competition going on in this question... :-)
How's this:
#include <stdio.h>
int main() {
// let the compiler assign the size of an initialised array
// use shorter names to expose operations (not lengthy variable names)
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int iMin = 0, iMax = 0;
// don't compare an element to itself
for( int i = 1; i < sizeof arr/sizeof arr[0]; i++ ) {
// use "branchless" coding for speed.
int n = arr[i] > arr[iMax];
iMax = n*i + !n*iMax;
n = arr[i] < arr[iMin];
iMin = n*i + !n*iMin;
}
// reduce duplication of static data
char *fmt = "%s element is %d\t index is %d\n";
printf( fmt, "Maximum", arr[ iMax ], iMax );
printf( fmt, "Minimum", arr[ iMin ], iMin );
return 0;
}
Same output.
Ball's in your court #Allan :-)
EDIT:
There has been an advance on the last offering that needs to be addressed...
Here we go whole-hog, splurging-out with a third 'container' (mm[0]) to catch all those indexes that satisfy neither conditional ('<' & '>'). AND, a 4th 'container' (mm[3]) that doesn't change from being initialised to 0, the index of the 1st element. Besides being cryptic (not advised), this may-or-may-not be more expensive with its multiple array offset calculations... But, it's fun to look at...
#include <stdio.h>
int main() {
// added two elements to show 0 and nElem are not 'flukes'
// this really does find and report the min/max values
int arr[] = { 1, 2, 3, 4, 42, 5, -42, 6, 7, 8, 9, 10 };
int i, mm[1 + 2 + 1] = { 0 };
// assign 'i' to mm[ 0 or 1 or 2 ]. 0=latest, 1=max, 2=min, (3 unaffected)
for( i = 1; i < sizeof arr/sizeof arr[0]; i++ )
mm[ (arr[i] > arr[mm[1]]) + 2*(arr[i] < arr[mm[2]]) ] = i;
mm[ 0 ] = i-1; // always pick up the last index. Thanks #A Wind!
// now... this is getting silly!!
char *fmt = "%5s = %3d # arr[%d]\n";
char *type[] = { "last", "max", "min", "first" };
i = 3; do printf( fmt, type[i], arr[ mm[i] ], mm[i] ); while( --i >= 0 );
return 0;
}
first = 1 # arr[0]
min = -42 # arr[6]
max = 42 # arr[4]
last = 10 # arr[11]
Y'know... This might be interesting to try to apply to 3-way branching as is needed for binary searching; determining '<', '=' or '>'... Hmmm...
EDIT: (another variation on a theme at the suggestion of a worthy competitor :-)
#include <stdio.h>
int main() {
struct {
char *label;
int ind;
} mm[] = {
{ "last" },
{ "maximum" },
{ "minimum" },
{ "first" },
};
int i, arr[] = { 1, 2, 3, 4, 42, 5, -42, 6, 7, 8, 9, 10 };
for( i = 1; i < sizeof arr/sizeof arr[0]; i++ )
mm[ (arr[i] > arr[mm[1].ind]) + 2*(arr[i] < arr[mm[2].ind]) ].ind = i;
mm[ 0 ].ind = --i; // always pick up the last index. Thanks #A Wind!
for( i = sizeof mm/sizeof mm[0]; --i >= 0; /* space for rent */ )
printf( "%8s = %3d # arr[%d]\n", mm[i].label, arr[ mm[i].ind ], mm[i].ind );
return 0;
}
EDIT:
Trying to cover ALL the bases, here are three more ways to skin a cat
/* Minimalist */
#include <stdio.h>
int main() {
int mm[3] = { 0 },
arr[] = { 1, 2, 3, 4, 42, 5, 6, 7, 8, 9, 10 },
i = sizeof arr/sizeof arr[0];
while( --i )
mm[ 2*(arr[i] > arr[mm[2]]) + (arr[i] < arr[mm[1]]) ] = i;
char *fmt = "arr[%d] = %3d M%simum\n";
printf( fmt, mm[1], arr[mm[1]], "in" );
printf( fmt, mm[2], arr[mm[2]], "ax" );
return 0;
}
/* Recursive - for brevity, excluding the index; just reporting two values */
#include <stdio.h>
int amin( int a[], int i ) { // NB: "amin", not "main"
int j = --i ? amin( a, i ) : i;
return (a[j]<a[i])*j + (a[j] > a[i])*i;
}
int amax( int a[], int i ) {
int j = --i ? amax( a, i ) : i;
return (a[j]>a[i])*j + (a[j]<a[i])*i;
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, },
sz = sizeof arr/sizeof arr[0];
char *fmt = "M%simum: %3d\n";
printf( fmt, "in", arr[ amin(arr, sz) ] );
printf( fmt, "ax", arr[ amax(arr, sz) ] );
return 0;
}
/* And, simply brute force using a library function */
#include <stdio.h>
#include <stdlib.h>
int cmp( const void *a, const void *b ) { return *(int*)a - *(int*)b; }
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
sz = sizeof arr/sizeof arr[0];
qsort( arr, sz, sizeof arr[0], cmp );
char *fmt = "M%simum: %3d\n";
printf( fmt, "in", arr[ 0 ] );
printf( fmt, "ax", arr[ --sz ] ); // again, thanks to #A Wind
return 0;
}
Many ways to skin a cat.

In C, how to write a N-dimensional nested for loop where N is variable

I need to write the statement:
for ( int i1 = i1_min; i1 < width; ++i1 )
for ( int i2 = i2_min; i2 < i2_max; ++i2 )
for ( int i3 = i3_min; i3 < i3_max; ++i3 )
...
for ( int iN = iN_min; iN < iN_max; ++iN )
How to do it?
You could write a recursive function loopn() to do this:
void loopn_recurse (int *min, int *max, int *counters, size_t N, size_t n, void (*func)(int*, size_t)) {
for (int i = min[n]; i < max[n]; ++i) {
counters[n] = i;
if (N - n > 1) {
loopn_recurse(min, max, counters, N, n + 1, func);
} else {
// innermost algorithm
func(counters, N);
}
}
}
void loopn (int *min, int *max, int *counters, size_t N, void (*func)(int*, size_t)) {
assert(N > 0);
loopn_recurse(min, max, counters, N, 0, func);
}
// example usage
void test (int *counters, size_t N) {
for (size_t j = 0; j < N; ++j) {
printf("%d ", counters[j]);
}
putchar('\n');
}
loopn((int[]){ 1, 2, 3, 4, 5 }, (int[]){ 2, 4, 6, 8, 10 }, (int[5]){}, 5, test);
Try it online!
It is possible to represent N-dimensional space as 1-dim, with one index, from which you can calculate N sub-indexes if you need them.
Below I'm attaching example implementation of this kind of space flattening
#include <stdio.h>
int bonds[][2] = {
{1, 2},
{5, 10},
{0, 3},
{6, 8}
};
int get_index(int index_position, int current, int sizes[], int sizes_size) {
int mod = 1;
for(int i = index_position; i < sizes_size; i++) {
mod *= sizes[i];
}
int div = mod / sizes[index_position];
return (current % mod) / div;
}
int main(int argc, char** argv) {
int sizes[] = {
bonds[0][1] - bonds[0][0],
bonds[1][1] - bonds[1][0],
bonds[2][1] - bonds[2][0],
bonds[3][1] - bonds[3][0]
};
int big_size = sizes[0] * sizes[1] * sizes[2] * sizes[3];
for(int i = 0; i < big_size; i++) {
/* get indexes */
/*
int i0 = ((i % (sizes[0] * sizes[1] * sizes[2] * sizes[3])) / (sizes[1] * sizes[2] * sizes[3])) + bonds[0][0];
int i1 = ((i % ( sizes[1] * sizes[2] * sizes[3])) / ( sizes[2] * sizes[3])) + bonds[1][0];
int i2 = ((i % ( sizes[2] * sizes[3])) / ( sizes[3])) + bonds[2][0];
int i3 = ((i % ( sizes[3])) ) + bonds[3][0];
*/
int i0 = get_index(0, i, sizes, 4) + bonds[0][0];
int i1 = get_index(1, i, sizes, 4) + bonds[1][0];
int i2 = get_index(2, i, sizes, 4) + bonds[2][0];
int i3 = get_index(3, i, sizes, 4) + bonds[3][0];
printf("%d %d %d %d\n", i0, i1, i2, i3);
}
return 0;
}
Dynamic handling of bonds/sizes array is out scope here. The example shows how to calculate sub-indexes from flattened space.
If what you need to do is for really variable N, then recursive one by Patrick is a way to go.
If what you try to do is for fixed variable N and you are trying to avoid typing for loop repetitively, you can use macro to save some keystrokes:
#define LOOP(N) for (int i#N = i#N#_min; i#N < i#N#_max; ++i#N )
LOOP(1)
LOOP(2)
LOOP(3)
LOOP(4)
LOOP(5) {
val = i1+i2+i3+i4+i5; // example calculation
}

Output the indices of array in descending order

I use the following quicksort function to sort any given array in descending order:
int sorting (const void * a, const void * b)
{
return ( *(double*)a < *(double*)b );
}
int main(int argc, char *argv[]) {
int n;
double values[] = { 88.54, 56.65, 100.13, 2.091, 25.223 };
qsort(values, 5, sizeof(double), sorting);
for( n = 0 ; n < 5; n++ ) {
printf("%f ", values[n]);
}
return(0);
}
Besides outputting the values in descending order, I want to output their corresponding indices. For instance, for the given values[] array, I would get [2, 0, 1, 4, 3] which indicates the index 2 has the largest values, index 0 has the second largest values, and so on. How can I modify the code above ?
Thank you
Combine values with indexes in a struct, sort them, and print indexes along with values:
struct ind_val {
int index;
double value;
};
int sorting_ind_val (const void * a, const void * b) {
double lhs = ((struct ind_val*)a)->value;
double rhs = ((struct ind_val*)b)->value;
if (lhs < rhs)
return 1;
if (lhs > rhs)
return -1;
return 0;
}
...
double values[] = { 88.54, 56.65, 100.13, 2.091, 25.223 };
struct ind_val pair[5];
for (int i = 0 ; i != 5 ; i++) {
pair[i].index = i;
pair[i].value = values[i];
}
qsort(pair, 5, sizeof(struct ind_val), sorting_ind_val);
for (int i = 0 ; i != 5 ; i++) {
printf("%d: %f\n", pair[i].index, pair[i].value);
}
Demo.
2: 100.130000
0: 88.540000
1: 56.650000
4: 25.223000
3: 2.091000

determine whether two arrays are permutation of each other by using recursion

I have some difficulties in writing a code which determines whether two unsorted arrays are permutation of each other , by using recursion.
I know how to determine it by non-recursive code, using sorts - but I dont know how to do it by using recursion.
So far, I cant get any real idea...
int CheckPermutation(int arr1[], int arr2[], int size) {
if (size == 0)
return 1;
if (size == 1)
return (arr1[0] > arr2[0]);
}
that's what I have tried, I find it difficult to continue from that point
Here is an implementation for comparing 2 unsorted arrays without modifying them that uses recursion:
#include <stdio.h>
// count occurrences of value in an array using recursion
int rcount(int value, const int *a, int size) {
return size == 0 ? 0 : (value == *a) + rcount(value, a + 1, size - 1);
}
// check if all entries in a have the same number of occurrences in a and b
int check_perm(const int *a, const int *b, int size) {
for (int i = 0; i < size; i++) {
if (rcount(a[i], a, size) != rcount(a[i], b, size))
return 0;
}
return 1;
}
int main(void) {
int a[] = { 1, 2, 3, 3, 4, 4, 4, 5, 6, };
int b[] = { 1, 3, 2, 4, 5, 4, 4, 6, 3, };
int c[] = { 1, 3, 2, 4, 5, 4, 4, 6, 6, };
if (check_perm(a, b, sizeof(a) / sizeof(*a)))
printf("arrays a and b match\n");
if (!check_perm(a, c, sizeof(a) / sizeof(*a)))
printf("arrays a and c do not match\n");
if (!check_perm(b, c, sizeof(b) / sizeof(*b)))
printf("arrays b and c do not match\n");
return 0;
}
EDIT:
Here is a solution with a single recursive function. Both arrays are potentially modified. If indeed check_perm() returns non zero, both arrays will have been sorted:
int check_perm(const int *a, const int *b, int size) {
if (size > 1) {
for (int i = 1; i < size; i++) {
if (a[0] > a[i]) {
int temp = a[0];
a[0] = a[i];
a[i] = temp;
}
if (b[0] > b[i]) {
int temp = b[0];
b[0] = b[i];
b[i] = temp;
}
}
return (a[0] == b[0]) && check_perm(a + 1, b + 1, size - 1);
}
return 1;
}

Combine or merge 2 array with loop or function

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]
}
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.
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;
}
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
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;
}

Resources