two static arrays size 10 need to be copied to an empty target array size 20.
like this: you copy from the first array the nums till arr1 = 0 then
you switch to the second array and copy to the nums till arr2 = 0.
then you switch back, and continue to copy the nums, without copying the '0's.
example :
arr1[10] = {1,1,1,0,2,2,2,2,0,0)
arr2[10] = {3,3,3,3,0,4,4,4,0,5}
then the result array will be:
result[20] = {1,1,1,3,3,3,3,2,2,2,2,4,4,4,5,0,...0}
the thingy i wrote prints all the nums allright except the last ones in the second array. any ideas why?
thanx in advance.
#include <stdio.h>
#define size 10
//int Get_Num ();
//void Fill_Arr(int a[]);
void Print_Arr(int arr[],int num);
void Switch (int *a, int *b, int *res);
void main()
{
int arr1[size]={9,9,0,8,8,8,8,0,7,7};
int arr2[size]={1,1,1,0,2,2,0,3,3,3};
int i;
int arr3[20]={0};
//Fill_Arr(arr1);
//Fill_Arr(arr2);
Switch(arr1,arr2,arr3);
Print_Arr(arr3,20);
}
void Switch (int *a, int *b, int *res)
{
int *ptr = res;
int i;
int *temp1 = a;
int *temp2 = b;
while(ptr-res<20)
{
if (*a)
{
while(*a)
{
*ptr = *a;
a++;
ptr++;
}
}
else if(*b)
{
if(a-temp1<10)
a++;
while(*b)
{
*ptr = *b;
b++;
ptr++;
}
if(b-temp2<10) //till the end of arr2[]
b++;
}
if(!(*b)&&!(*a))
b++;
}
}