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++;	
}
}

Recommended Answers

All 7 Replies

thanks for the advice.
can you say anything constructive about the code itself as well?

Nope - if you can't be bothered to make a decent job of presentation, then I can't be bothered to read it.

Like the sig says, post crap and be ignored.

If this were a CV, it would have gone in the bin in a hot second.

I want to see your print function . If it is some thing of this sort

for(i=0;i<20;i++)
                printf("%d\n",arr3[i]);

Then you will get junk characters in the ending. The reason for this is that there are less than 20 numbers in arr 3.

I want to see your print function . If it is some thing of this sort

for(i=0;i<20;i++)
                printf("%d\n",arr3[i]);

Then you will get junk characters in the ending. The reason for this is that there are less than 20 numbers in arr 3.

well...you're right. my print is:

void Print_Arr(int arr[], int num)
{
	int i;
for(i=0 ; i<num ; i++)
printf("%d",arr[i]);
printf("\n");
}

but i thought that the

int arr3[20]={0};

in the main()
took care of the junk values. my main problem is that the program won't print the last nums of the second source array and nothing i try helps :(

Can you post the output that you are getting. This is the output that I am getting when I run your code

timberlake {~} > ./test1
9 9 1 1 1 8 8 8 8 2 2 7 7 3 3 3 4196247 9 9 8

timberlake {~} >

The last 4 values are junk. But the main point is I think no values from array B have been skipped

Can you post the output that you are getting. This is the output that I am getting when I run your code

timberlake {~} > ./test1
9 9 1 1 1 8 8 8 8 2 2 7 7 3 3 3 4196247 9 9 8

timberlake {~} >

The last 4 values are junk. But the main point is I think no values from array B have been skipped

that's funny, my compiler didn't print the last values 3 3 3 putting junk instead of them. but you were right. one of my problems was that i didn't took care of the junk values by setting a counter and sending it to a print function as to print the arr till it.
anyho, your answer was very helpful, thanks! :)
in the end i decided on a bit different approach and re-wrote the whole damn thing. :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.