Anda di halaman 1dari 4

Assignment No 01

TITLE : Perform set operations (Union, Intersection, Difference and Symmetric


Difference) using ‘C’.

AIM : To define and accept 2 arrays and perform set operations like union, intersection,
difference and symmetric difference using loops, conditional statements and array
indexes.

OBJECTIVE : Learn the handling of arrays using ‘C’ statements. Study single
dimensional array, see how to represent sets using single dimensional array and learn to
use loops and array indexes

THEORY:
1) Sets, set operations-Union, intersection, difference, symmetric difference of sets
2) 1 Dimentional array and its use to represent sets and its operations
Sets: A set is a collection of distinct objects, where the object may be atomic(single
item) or a set.
Operations on sets:
Union of sets S1 and S2 is :
S1={1,5,3,2,4}
S2={3,1,4,5,6}
S1 U S2 = {1,5,3,2,4,6}

Intersection of sets S1 and S2 is :


S1={1,5,3,2,4}
S2={3,1,4,5,6}
S1 ∩ S2 = {1,5,3,4}

Difference of sets S1 and S2 is :


S1={1,5,3,2,4}
S2={3,1,4,5,6}
S1-S2 = {2}
S2-S1= {6}

Symmetric difference of sets S1 and S2 is :


S1={1,5,3,2,4}
S2={3,1,4,5,6}
(S1-S2) U (S2-S1)= {2,6}

Array: An array is a collection of homogeneous data elements stored in contiguous


memory locataions.
Example :
10 24 35 23 60
Analogy: Books placed side by side on a shelf
Position of element in an array is called the index. Array elements can be accessed in a
sequential or random order using the index.
Array elements ->
10 24 35 23 60
0 1 2 3 4 -> INDEX
Array can be declared in ‘C’ as :
int array1[50];
The above declarations lead to reservation in memory of 50 contiguous locations which
can store integer data and can be accessed by names array1[0],array1[1],array1[2], … ,
array1[49]
Following are some more sample declarations:

int array2[]={10,29,36,44,51}
Integer array of 5 elements stored in array2[0],array2[1],array2[2], array2[3]and
array2[4]
float array3[10];
Float array of reserved size 10
char array4[]=”example string”;
Character array initialized to “example string”

Operations on an array: 1. Insert in the end, start or anywhere in between : Insertion in


the end of array can be performed by incrementing the index by 1 and assigning the value
to last array element.
Insertion anywhere else will require the remaining elements to be shifted down and the
element to be inserted in the given location.
2. Delete from start, end or anywhere in between: Deleting from the end will require to
decrement by 1 the variable storing the last array index.
3. Display of array elements.
INPUT:- Elements of 2 sets S1,S2.

OUTPUT( Expected ):-


The expected outputs are
• Display both sets
• Display union of the sets
• Display intersection of the sets
• Display Difference of the sets
• Display symmetric difference of the sets

Eg. Enter set elements of two set


A = { 2 5 6}
B = {1 2 7}
Union of A & B is {1 2 5 6 7}
Intersection is {2}
Difference (A-B) is { 5 6}
Symmetric difference (A-B) U ( B-A) is {5,6} U {1,7}={5,6,1,7}
Students are expected to write algo of program written according proper logic and
assumptions to display
1) Union
2) Intersection
3) Difference
4) Symmetric Difference
ALGORITHM
1. Algorithm UNION
1. Accept 2 integer arrays array1 and array2 of size say m, n respectively.
2. Initialize a third array array3 of at least the size m+n.
3. Copy first array elements into third array.
4. Compare first element of array2 with all elements of array1. If unique then add to
array3.
5. Repeat step 4 for all array elements of array2.
6. Print array3.

2. Algorithm INTERSECTION
1) Accept 2 integer arrays array1 and array2 of size say m, n respectively.
2) Initialize a third array array3 of at least the size m+n.
3) Compare first element of array1 with all elements of array2. If common then add to
array3.
5) Repeat step 4 for all array elements of array1.
6) Print array3.
3. Algorithm DIFFERENCE
1) Accept 2 integer arrays array1 and array2 of size say m, n respectively.
2) Initialize a third array array3 of at least the size m.
3) Compare first element of array1 with all elements of array2. If unique then add to
array3.
5) Repeat step 4 for all array elements of array1.
6) Print array3.
4. Algorithm SYMMETRIC_DIFFERENCE
1) Accept 2 integer arrays array1 and array2 of size say m, n respectively.
2) Initialize a third array array3 of at least the size m, array4 of size n and array5 of
size m+n.
3) Compare first element of array1 with all elements of array2. If unique then add to
array3.
4) Repeat step 3 for all array elements of array1.
5) Compare first element of array2 with all elements of array1. If unique then add to
array4.
6) Repeat step 6 for all array elements of array2.
7) Make union of array3 and array4 into array5.
8) Print array5
(Note: array3 and array 4 have unique elements so instead of making array4, the
elements can be added in the end of array3 itself and array3 printed in the end. Also,
the symmetric difference can be achieved by (A U B)-(A ∩B) )
INSTRUCTIONS:-

1)Proper indenting, coding styles, commenting, naming conventions, should


be followed.
2) Do not use goto. Try to avoid global variable declaration.
3) Use of functions is necessary.

Software used : Turbo C++

Anda mungkin juga menyukai