/***************************************************
 * Problem #1
 * CP-2 Warmup problem sheet (1999-2000)
 **************************************************/

/*
 * Question:
 * Write a complete C program to do the following task: 
 * (a) accept input for 2 integer arrays of size 10, 
 * (b) form a third array that will contain the elements of 
 *      both the arrays sorted in ascending order.  
 * The new array should not have duplicate elements.
 */


#include <stdio.h>
#define SIZE 10
main()
{
        int arr1[SIZE],arr2[SIZE];
        int result[2*SIZE];
        int i,n;
        
        printf("Give the first array of size: %d\n",SIZE);
        for(i=0;i<SIZE;i++)
        {
                printf("%d element of array-1: ",i+1);
                scanf("%d",&arr1[i]);
        }
        printf("Give the second array of size: %d\n",SIZE);
        for(i=0;i<SIZE;i++)
        {
                printf("%d element of array-2: ",i+1);
                scanf("%d",&arr2[i]);
        }
        n=sort(result,0,arr1,SIZE);
        n=sort(result,n,arr2,SIZE);
        for(i=0;i<n;i++)
                printf("%d\n",result[i]);
}

/*
 * This function assumes that the result is an array containing 
 * n elements sorted in ascending order, 
 * and the elements in arr1 of size 'size' should be inserted into result 
 * without
 * disturbing the order (Ascending order), eliminating duplication of
 * elements in the result.
 */

int sort(int *result,int n,int *arr1,int size)
{
        int i=0,j,k;
        
        if(n==0)
                {
                result[0]=arr1[0];
                i=1;
                }
        
        for(;i<size;i++)
        {
                printf("%d\t%d\n",i,n);
                for(j=0;j<n;j++)
                {
                        if(arr1[i]<result[j])
                        {
                                for(k=n;k>j;k--)
                                        result[k]=result[k-1];
                                result[j]=arr1[i];
                                n++;
                                break;
                        }
                        if(arr1[i]==result[j])
                                break;
                }
                if(j!=n)
                        continue;
                result[n++]=arr1[i];
                        
        }
        printf("Sorted array size: %d\n",n);
        return n;
}