This program will accept input for two sets of integers, print these sets, find the intersection, union, set difference, and symmetric difference.
This program constists of 3 modules:
PROJ1 Contains function calls to:
void read_set(int[],int)
void get_intersect(int[],int[],int,int)
PROJ2 contains function definitions for functions:
void read_set(int[],int) Receives a array, and reads values into it
PROJ3 contains function definitions for functions:
void print_elements(int[],int))
Receives an array, and last index . Prints all elements of array.
INTER contains function definitions for functions:
void get_intersect(int[],int[],int,int) Receives two arrays, and last indexes
.
Finds intersection of arrays.
/**********************************************************************************/ /* module PROJ1 */ /**********************************************************************************/ #include <stdio.h> main() { extern void read_set(int[],int); /*reads in elements of an array*/ extern void get_intersect(int[],int[],int,int); /*finds intersection oftwo arrays*/ int size_a, /*size 1st of array*/ set_a[99], /*1st array*/ set_b[99], /*second array*/ size_b; /*size of second array*/ printf("\nEnter number of elements (less than 100) for set A\n"); /*prompt for input*/ scanf("\n%d",&size_a); /*gets size of 1st array*/ while(size_a<=0) /*checks for valid input*/ { if(size_a<=0) printf("\nInput must be a positive integer\n"); scanf("\n%d",&size_a); } read_set(set_a,size_a);/*reads elements a of first set*/ printf("\nEnter number of elements (less than 100) for set B\n"); /*prompt for input*/ scanf("\n%d",&size_b); /*gets size of second array*/ while(size_b<=0) /*checks for valid input*/ { if(size_b<=0) printf("\nInput must be a positive integer\n"); scanf("\n%d",&size_b); } read_set(set_b,size_b); /*reads elements of second array*/ get_intersect(set_a,set_b,size_a,size_b);/*gets intersection of both arrays*/ }Back to top
/**********************************************************************************/ /* MODULE: PROJ2 */ /* this module contains a function to read numbers entered into an array */ /**********************************************************************************/ void print_elements(int [],int); /*function to print elements of an array*/ /*-------------------function void read_set()-------------------------------------*/ /*function to read numbers entered into an array*/ void read_set(int array[], int size) { int n;/*increment counter*/ printf("\nEnter elements in a set, each element must be 0 or greater"); printf("\n"); for(n=0;n<size;++n) /*numbers will be accepted until the end of array is reached*/ { scanf("%d",&array[n]);/*gets an array element*/ } print_elements(array,size);/*prints elements entered*/ return; }Back to top
/**********************************************************************************/ /* MODULE PROJ3 */ /* contains a function to print array of elements */ /**********************************************************************************/ /*----------------------function void print_elements------------------------------*/ void print_elements(int array[100], int size) /*receives an array, and it's size*/ { int n;/*increment counter*/ printf("\n\n"); for(n=0;n<size;++n) { printf(" %d",array[n]);/*prints an element[n] until last index is reached*/ } return; }Back to top
/**********************************************************************************/ /* module: INTER */ /* finds the intersection of two sets(arrays) */ /**********************************************************************************/ extern void print_elements(int[],int); void get_intersect(int array1[], int array2[], int size1, int size2) { int intersect[100], /*array to receive intersected elements*/ n=0, /*increment counter*/ size=0, /*size of array to hold intersections*/ flag=1, /*flag value to be changed to exit loop*/ m; /*another increment counter*/ for(m=0;m<size2;++m) /*this will increment value of element in array2*/ { flag=1;/*sets flag back to one*/ for(;(n>size1)&&flag;++n) /*compares one element of first to all of second*/ { if (array1[n]==array2[m]) /*if matching values are found*/ { intersect[size]=array1[n]; /*the value is put into the intersectn array*/ ++size; /*value is increased so next value found will go into next elm */ flag=0; /*flag is set to 0, so loop will stop*/ } } n=size-n; /*this will keep elements that have been searched from being compared*/ /*over again*/ } /*size=size;*/ printf("\nIntersection of set A and B ="); print_elements(intersect,size); return; }Back to top