# Thread: Help with Array

1. ## Help with Array

I have 2 constant arrays
int set1[10] = {1,2,3,4,5,6,7,8,9,10};
int set2[5] = {2,4,7,9,11};

I need to compute their union, difference, and intersection.
The union works fine. For some reason, its including 1 in the intersection and not in the difference, when it should be in the difference and not in the intersection.

Current output:
set1: 1,2,3,4,5,6,7,8,9,10
set2: 2,4,7,9,11
set3: union of 1&2 1,2,3,4,5,6,7,8,9,10,11
set3: diff of 1&2 3,5,6,8,10
set3: inter of 1&2 1,2,4,7,9

Should output:
set1: 1,2,3,4,5,6,7,8,9,10
set2: 2,4,7,9,11
set3: union of 1&2 1,2,3,4,5,6,7,8,9,10,11
set3: diff of 1&2 1,3,5,6,8,10
set3: inter of 1&2 2,4,7,9

anyone know why the 1 is showing in inter 1&2 and not in diff 1&2 with the current code?

Thanks

Code:
```/****************************************/
/* This program initializes 2 sets	*/
/* and computes their union, difference,*/
/* and intersection.			*/
/****************************************/

#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;					/* 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 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, 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++)
{
Set_union(set1, set2, set3, n1, n2);
}
printf("\n\nSet3\tUnion of 1&2\t", &Set_union);
Set_print(set3, 10);

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

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

}```

2. ## Re: Help with Array

Originally posted by webbizdesign
I have 2 constant arrays
int set1[10] = {1,2,3,4,5,6,7,8,9,10};
int set2[5] = {2,4,7,9,11};

I need to compute their union, difference, and intersection.
The union works fine. For some reason, its including 1 in the intersection and not in the difference, when it should be in the difference and not in the intersection.

Current output:
set1: 1,2,3,4,5,6,7,8,9,10
set2: 2,4,7,9,11
set3: union of 1&2 1,2,3,4,5,6,7,8,9,10,11
set3: diff of 1&2 3,5,6,8,10
set3: inter of 1&2 1,2,4,7,9

Should output:
set1: 1,2,3,4,5,6,7,8,9,10
set2: 2,4,7,9,11
set3: union of 1&2 1,2,3,4,5,6,7,8,9,10,11
set3: diff of 1&2 1,3,5,6,8,10
set3: inter of 1&2 2,4,7,9

anyone know why the 1 is showing in inter 1&2 and not in diff 1&2 with the current code?

Thanks

Code:
```/****************************************/
/* This program initializes 2 sets  */
/* and computes their union, difference,*/
/* and intersection.            */
/****************************************/

#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]);
Why not simply use
for( i = 0; i < n; i++)
printf(" %d", Set[ i]);

}
The function prototypes are not needed when you define the functions before they are used.
If you move all functions after main(), then the prototypes are needed
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    */
a useless comment. define the function's reason for
{
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;
Loop thru one set looking for each element in the other instead of looping thru values
The way you're code is designed, this works, but you are of course limited to sets containing only
the values 1 thru 12
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;                  /* 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 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 */
You are only checking if the value is IN set1 and NOT IN set2.
Switch to if (set1 != set2) to test if it is in one set but not the other
{                   /* 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.                    */
This if looks fine.
Add printf() statements to see what the values being tested are
and the return values from Element_set()
{
Set3[n3] = value;
n3++;
}
}
return n3;
}

main()

{
int n1, n2,n3, 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++)
{
Set_union(set1, set2, set3, n1, n2);
}
printf("\n\nSet3\tUnion of 1&2\t", &Set_union);
Set_print(set3, 10);

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_differ);
Set_print(set3, 5);     Since you are returning the size of the set3 array from
each function, why hard code the number of values to print? Use
Set_print(set3, sz3);

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

}```

3. Originally posted by WaltP
Loop thru one set looking for each element in the other instead of looping thru values The way you're code is designed, this works, but you are of course limited to sets containing only
the values 1 thru 12

how would I do that?

4. Code:
```/* To add all the elements in Set1 that ARE NOT in Set2 to Set3 */
for(n1 = 1; n1 < 12; n1++)
{
set2 = Element_set(Set2, n2, Set1[n1]);
if (!set2)
{
Set3[n3] = Set1[n1];
n3++;
}
}

/*=========================*/

/* To add all the elements in Set1 that ARE in Set2 to Set3 */
for(n1 = 1; n1 < 12; n1++)
{
set2 = Element_set(Set2, n2, Set1[n1]);
if (set2)
{
Set3[n3] = Set1[n1];
n3++;
}
}```
The problem with this will be the added values once you go thru both sets is that Set3 will not be in a sorted order.

Popular pages Recent additions