0

How to write a C code to copy the contents of 2 arrays into a third one in a zigzag order given that array sizes are not known?

I tried this, but didn't work, any help? #include <stdio.h> void zig_order(int arr1_size, int *arr1, int arr2_size, int *arr2) { int arr_zig_size = arr1_size + arr2_size; int arr_zig[arr_zig_size]; int i, j, k; for(k=0;k<=arr_zig_size;k++) { for(i=0 ; i<=arr1_size; i++) { for(j=0; j<=arr2_size; j++) { arr_zig[k]=arr1[i]; arr_zig[k+1]=arr2[j]; } } printf("arr_zig=%d \n", *arr_zig); } void main() { int arr1[]={1,3,1,5,3}, arr2[]={4,2,6}; zig_order(5, arr1, 3, arr2); return 0; }

14th Oct 2018, 8:17 AM
Aisha Ali
Aisha Ali - avatar
5 odpowiedzi
+ 5
Yes you can do that but the variable should be integer then
14th Oct 2018, 9:08 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
when you say zigzag you mean arr1 = 1,2,3,4 arr2 = 5,6,7,8 newarr = 1,5,2,6,3,7,4,8 right, this is what you want as output?
14th Oct 2018, 8:23 AM
Tanay
Tanay - avatar
+ 3
Yes, you can give array size like that if you are using C99 standards to Compile your code this type of arrays are called variable length arrays. and the size must be unsigned int.
14th Oct 2018, 10:04 AM
Tanay
Tanay - avatar
+ 2
Check this out i have modified your code. https://code.sololearn.com/cWkwCVT2lLac
14th Oct 2018, 8:44 AM
Tanay
Tanay - avatar
0
yes that,s what I mean! thank you so much, but is it right to add variable as array size like that? int arr_zig[arr_zig_size];
14th Oct 2018, 8:56 AM
Aisha Ali
Aisha Ali - avatar