Problem : Given 2 arrays such that second is the subset of the first, find the set difference.
That is all the elements in array1 that are not in array2.
Eg:
array1 - 6 7 4 3 9 1 2 5 8 0
array2 - 1 2 3 5 4
Set difference - 6 7 9 8 0
#include<stdio.h>
int main()
{
int s1, s2, i, j, k = 0, check, a1[50], a2[50], a3[50];
printf("\nEnter the size of array1:");
scanf("%d", &s1);
printf("\nEnter the elements\n");
for(i = 0; i < s1; i++)
scanf("%d", &a1[i]);
printf("\nEnter the size of array2 < size of array1:");
scanf("%d", &s2);
printf("\nEnter the elements (should be a subset of array1)\n");
for(i = 0; i < s2; i++)
scanf("%d", &a2[i]);
for(i = 0; i < s1; i++)
{
check = 1;
for(j = 0; (j < s2 && check == 1); j++)
{
if(a1[i] != a2[j])
continue;
else
check = 0;
}
if(check == 1)
a3[k++] = a1[i];
}
printf("\nSet difference:\n");
for(i = 0; i < k; i++)
printf("%d ", a3[i]);
return 0;
}
Written by Munia Balayil
Wednesday, July 25, 2012
Set difference
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment