The version below adds validation to your integer input from the keyboard, so that your program will not crash on bad input. Also, each array is entered separately, and will accept data up to the maximum size permitted at compile time.
This is just one way to handle this problem. Another way, see below, might be to add all array 1 to array 3 ... then add after that, all of array 2 to array 3 ... then to transform array 3 into an array of unique elements, perhaps by way of first sorting it ...
/* mergeTwoArys2.c */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
#include <stdio.h>
#define MAX_SIZE 5 /* keep small while testing/debugging ... */
int more()/* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered ... */
{
int c, reply;
fputs( "More (y/n) ? ", stdout ); fflush( stdout );
reply = c = getchar();
while( c != '\n' ) c = getchar(); /* 'flush' stdin buffer ... */
if( reply == 'n' || reply == 'N' ) return 0;
/* else ... */
return 1;
}
/* returns the entered array ('by reference') and the size of the array entered,
size can be in range 0..max */
int enterAry( const char prompt[], int ary[], int max )
{
int i = 0, numGoodVals;
printf( "%s (max size %d)\n", prompt, max ); fflush( stdout );
do
{
printf( "enter integer %d: ", i+1 ); fflush( stdout );
numGoodVals = scanf( "%d", &ary[i] );
while( getchar() != '\n' ) ; /* flush stdin ... */
if( numGoodVals != 1 )
{
printf( "Please enter only integers ...\n" );
continue; /* jump to test at end of do..while loop right NOW! */
}
/* else ... */
++i;
if( i == max ) break;
}while( more() );
return i; /* the number of int's entered ... 'i' is in range 0..max */
}
void printAry( const int ary[], int num )
{
int i;
for( i = 0; i < num; ++i )
printf( "%d ", ary[i] );
}
int exist( const int ary[], int size, int val )
{
int i;
for( i = 0; i < size; ++i )
if( ary[i] == val ) return 1;
return 0;
}
int main()
{
int ary1[MAX_SIZE], ary2[MAX_SIZE], ary3[2*MAX_SIZE],
i, size1, size2, size3;
size1 = enterAry( "Enter ary1 ...", ary1, MAX_SIZE );
size2 = enterAry( "Enter ary2 ...", ary2, MAX_SIZE );
printf( "\nary1 is: " ); printAry( ary1, size1 );
printf( "\nary2 is: " ); printAry( ary2, size2 );
size3 = 0;
for( i = 0; i < size1; ++i )
{
if( !exist(ary3, size3, ary1[i]) )
{
ary3[size3] = ary1[i];
++ size3;
}
}
for( i = 0; i < size2; ++i )
{
if( !exist(ary3, size3, ary2[i]) )
{
ary3[size3] = ary2[i];
++ size3;
}
}
printf( "\nary3 is: " ); printAry( ary3, size3 );
fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
return 0;
}
Here is that other way that was suggested that you might proceed ... solving this problem by merging two arrays into a new array of sorted unique elements ...
/* mergeTwoArysStep2_a.c */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
#include <stdio.h>
#define SIZE 5
void printAry( const int ary[], int num )
{
int i;
for( i = 0; i < num; ++i )
printf( "%d ", ary[i] );
}
void bubblesort( int ary[], int size )
{
int j, swap, tmp;
do
{
swap = 0; // if array is sorted ... then DONE after the next pass
// only have to check, each loop, up to to size-1 places
for( j = 1; j < size; ++j )
{
if( ary[j-1] > ary[j] ) // swap value at j-1 with value at j
{
tmp = ary[j];
ary[j] = ary[j-1];
ary[j-1] = tmp;
swap = 1;
}
}
--size;
}while( swap );
}
int getUnique( int ary[], int size )
{
int i = 0, j =1;
for( ; j < size ; ++j )
{
if( ary[i] != ary[j] ) if( ++i != j ) ary[i] = ary[j];
}
return i+1; // return updated size, now of unique elements
}
int main()
{
int i, unique, ary1[SIZE], ary2[SIZE], ary3[2*SIZE];
for( i = 0; i < SIZE; ++i )
{
printf( "For ary1, enter integer %d: ", i+1 ); fflush( stdout );
scanf( "%d", &ary1[i] );
printf( "For ary2, enter integer %d: ", i+1 ); fflush( stdout );
scanf( "%d", &ary2[i] );
ary3[i] = ary1[i];
ary3[SIZE+i] = ary2[i];
}
while( getchar() != '\n' ) ; /* flush stdin ... */
printf( "\nary1 is: " );
printAry( ary1, SIZE );
printf( "\nary2 is: " );
printAry( ary2, SIZE );
printf( "\nary3 is: " );
printAry( ary3, 2*SIZE );
bubblesort( ary3, 2*SIZE );
printf( "\nary3 sorted is: " );
printAry( ary3, 2*SIZE );
unique = getUnique( ary3, 2*SIZE );
printf( "\nunique ary3 size is %d and ary3 is: ", unique );
printAry( ary3, unique );
fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
return 0;
}