Code:
#include <stdio.h>
#define SIZE 15
/*
In algebra, the intersection of two sets is defined as a new set that contains all the values common in both sets.
For instance, consider the following two sets, A and B:
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {2, 4, 8, 12, 14, 20, 25, 28, 30, 32}
The only values common to both sets are 2, 4, and 8. The intersection of A and B, which is denoted as A n B, is
A n B = {2, 4, 8}
Write a simple c program that illustrates how array contents can be processes to perform an operation such as
finding the intersection of two sets. The program will ask the user to enter two sets of values, each stored
in an array. Then it will scan the two arrays looking for values common to both. The common values will be
stored in a third array, whose contents are displayed on the screen.
Use the following variables as needed in the program:
Set1 An array of integers to hold the first set
Set2 An array of integers to hold the second set
Intersection An array of integers to hold the intersection of the set1 and set2
numIntValue An integer holding the number of intersecting values.
*/
int getArray(int set1[SIZE], int set2[SIZE]);
int findIntersection(int set1[SIZE], int set2[SIZE], int intersection[SIZE], int numIntValue);
void displayIntValue(int intersection[SIZE], int numIntValue);
int main()
{
int set1[SIZE];
int set2[SIZE];
int intersection[SIZE];
int numIntValue = 0;
getArray(set1, set2);
findIntersection(set1, set2, intersection, numIntValue);
displayIntValue(intersection, numIntValue);
}
/*
getArray()
set1 and set2 are passed into the function. It prompts the user to enter values for each array.
*/
int getArray(int set1[SIZE], int set2[SIZE])
{
int count;
int pass;
int i;
int hold;
printf("Enter 15 numbers for the first set: ");
for (count=0; count < SIZE; count++ )
{
scanf( "%i", (set1 + count));
}
printf("Enter 15 numbers for the second set: ");
for (count = 0; count < SIZE; count++ )
{
scanf( "%i", (set2 + count));
}
/*
Using bubble sort to sort the first array
*/
for ( pass = 1; pass < SIZE; pass++ )
{
for ( i = 0; i < SIZE - 1; i++ )
{
if ( set1[ i ] > set1[ i + 1 ] )
{
hold = set1[ i ];
set1[ i ] = set1[ i + 1 ];
set1[ i + 1 ] = hold;
}
}
}
/*
Using bubble sort to sort the second array
*/
for ( pass = 1; pass < SIZE; pass++ )
{
for ( i = 0; i < SIZE - 1; i++ )
{
if ( set2[ i ] > set2[ i + 1 ] )
{
hold = set2[ i ];
set2[ i ] = set2[ i + 1 ];
set2[ i + 1 ] = hold;
}
}
}
return 0; /* returns 0 for succesful completetion */
}
/*
findIntersection()
set1, set2, and intersection are passed into the function. It scans the arrays for values that appear in both.
The intersection values are stored intersection. This function returns the number of intersecting values found.
*/
int findIntersection(int set1[SIZE], int set2[SIZE], int intersection[SIZE], int numIntValue)
{
int x = 0;
int i = 0;
int y = 0;
int a = 0;
int b;
for (b = 0; b <= SIZE; b++ )
{
if (set1[x] == set2[i])
{
intersection[y] = set1[x];
y++;
i++;
x++;
a++;
numIntValue ++;
}
else
i++;
}
return numIntValue;
}
/*
displayIntValue()
The intersection array and the numIntvalues variable are passed into the function.
If numIntValues contains a number greater than zero, the function displays that many elements in the intersection
array. If there are no intersecting values, the function displays a message indicating so.
*/
void displayIntValue(int intersection[SIZE], int numIntValue)
{
int x;
if (numIntValue == 0)
{
printf("There are no intesecting values !");
}
else
{
for (x = 0; x <= 1; x++ )
{
printf("These values did intersect: ");
printf("%i\n", intersection[x]);
}
}
}