# Thread: Help with a problem Array

1. ## Help with a problem Array

I have 2 constant integer arrays. I am computing their union, difference, and intersection. I cannot figure out why it is outputting 1 in the intersection and not in the difference. 1 Should be in the difference, not the intersection.

Further, at the end of the program I have it run Element_set for set2[0] and it returns 0, like it should, and to make sure set2[0] is 2, I have it print that as well. Yet, in my output above, it still has the same problem.

My out put should look like this:

Set1: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Set2: 2, 4, 7, 9, 11
Union of 1&2: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
Difference of 1&2: 1, 3, 5, 6, 8, 10
Intersection of 1&2: 2, 4, 7, 9
0 2

Any help appreciated

Code:
```#include <stdio.h>

void Set_print(int Set[], int n);			/* Declares function Set_print with parameters int Set[] and int n[] */
void Set_print(int Set[], int n)
{
int i=n+1;				/* prints all the n values of the array Set[n] */
for( n = 0; n < i ; n++)
printf(" %d", Set[n]);
}

int Element_set(int Set[], int n, int value);		/* Declares function Element_set with parameters int Set[], int n, */
int Element_set(int Set[], int n, int value)		/* and int value 						   */
{
int i;
i=n+1;					/* Checks n-elements of Set[n] for value and if value is in Set[]  */
for(n=0; n<i; n++)			/* it returns 1, if value is not in Set[] it returns 0		   */
{
if(value == Set[n])
return 1;
}
if(value != Set[i-1])
return 0;

}

int  Set_union ( int Set1[], int Set2[], int Set3[], int n1, int n2 );	/* Declares function Set_union with parameters */
int  Set_union ( int Set1[], int Set2[], int Set3[], int n1, int n2 )	/* int Set1[], int Set2[], int Set3[], int n1, */
{								/* and int n2, n1 and n2 the elements of Set1  */
int value, set1, set2;					/* and Set2, respectively		       */
int n3=0;
for(value=1; value<12; value++)
{							/* Checks Set1[n1] and Set2[n2] to see if they */
set1 = Element_set(Set1, n1, value);		/* contain value.  If set1 or set2 contains    */
set2 = Element_set(Set2, n2, value);		/* value, then element n3 of Set3 is set to    */
if(set1 == 1 || set2 == 1)			/* value, and the element number of set3 is    */
{					/* returned				       */
Set3[n3] = value;
n3++;
}
}
return n3;
}

int Set_differ ( int Set1[], int Set2[], int Set3[], int n1, int n2);	/* Declares function Set_differ with parameters */
int Set_differ ( int Set1[], int Set2[], int Set3[], int n1, int n2)	/* int Set1[], int Set2[], int Set3[], int n1,  */
{								/* int n2, n1 and n2 the elements of Set1 and   */
int value, set1, set2=0;					/* Set2, respectively				*/
int n3;
for(value=1; value<12; value++)
{							/* Checks Set1[n1] and Set2[n2] to see if they */
set1 = Element_set(Set1, n1, value);		/* contain value.  If set1 contains value AND  */
set2 = Element_set(Set2, n2, value);		/* set2 does NOT contain value, then element   */
if(set1 == 1 && set2 == 0)			/* n3 of Set3 is set to value, and the element */
{					/* number of set3 is returned 		       */
Set3[n3] = value;
n3++;
}
}
return n3;
}

int Set_inter ( int Set1[], int Set2[], int Set3[], int n1, int n2);	/* Declares function Set_inter with parameters */
int Set_inter ( int Set1[], int Set2[], int Set3[], int n1, int n2)	/* int Set1[], int Set2[], int Set3[], int n1  */
{								/* and int n2.  n1 and n2 the elements of Set1 */
int value, set1, set2;						/* and Set2, respectively		       */
int n3=0;
for(value=1; value<12; value++)					/* Checks Set1[n1] and Set2[n2] to see if they */
{							/* contain value.  If set1 AND set2 BOTH       */
set1 = Element_set(Set1, n1, value);			/* contain value, then element n3 of set3 is   */
set2 = Element_set(Set2, n2, value);			/* set to value, and the element number of set3*/
if(set1 == 1 && set2 == 1 )				/* is returned.				       */
{
Set3[n3] = value;
n3++;
}
}
return n3;
}

main()

{
int n1, n2,n3,sz3, l;
int set1[10] = {1,2,3,4,5,6,7,8,9,10};
int set2[5] = {2,4,7,9,11};
int set3[11]={0};

printf("\nSet1\t");
Set_print(set1, 9);
printf("\n\nSet2\t");
Set_print(set2, 4);

for(n1=0, n2=0; n1<10 || n2<5; n1++, n2++)
{
sz3 = Set_union(set1, set2, set3, n1, n2);
}
printf("\n\nSet3\tUnion of 1&2\t", &Set_union);
Set_print(set3, sz3-1);

for(n1=0, n2=0; n1<10 || n2<5 ; n1++, n2++)
{
sz3 = Set_differ(set1, set2, set3, n1, n2);
}
printf("\n\nSet3\tDifference of 1&2\t");
Set_print(set3, sz3-1);

for(n1=0, n2=0; n1<10 || n2<5 ; n1++, n2++)
{
sz3 = Set_inter(set1, set2, set3, n1, n2);
}
printf("\n\nSet3\tIntersection of 1&2\t", &Set_inter);
Set_print(set3, sz3-1);
printf("\n");

printf("\n%d", Element_set(set2, 4, 1));
printf(" %d", set2[0]);

}```

2. You don't need the for loops in main (which returns an int by the way).
Code:
`n3 = Set_union(set1, set2, set3, 10, 5, 11);`
This can do just fine by itself. There are also some unused parameters to printfs in main.

I find the following to be a little easier to read.
Code:
```int Element_set(const int Set[], int n, int value)
{
int i;
for ( i = 0; i < n; ++i )
{
if ( value == Set[i] )
{
return 1;
}
}
return 0;
}```
I recommend rewriting the other loops that begin at one in this manner as well.

Set_differ does not initialize n3.

I think I'm leaving some things out, but my working copy has a number of my preferences here and there. I'll leave you to take another pass at it. After the loops in main are gone, the incorrect output was a little easier to debug.

3. Hmm, this looks a little like the post