forked from hermanwongkm/CS1010
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set containment.c
70 lines (56 loc) · 1.79 KB
/
Set containment.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// setContainment.c
// Program to check if a set is contained in another.
// This is an incomplete program given to students.
#include <stdio.h>
void scanArray(int [], int);
void printArray(int [], int);
int isSubset(int [], int, int [], int);
int main(void) {
int arrayA[4], sizeA = 4, arrayB[7], sizeB = 7;
printf("1st array:\n");
scanArray(arrayA, sizeA);
printf("2nd array:\n");
scanArray(arrayB, sizeB);
if (isSubset(arrayA, 4, arrayB, 7))
printf("arrayA[0..3] is a subset of arrayB[0..6]\n");
else
printf("arrayA[0..3] is not a subset of arrayB[0..6]\n");
if (isSubset(arrayA, 4, arrayB, 6))
printf("arrayA[0..3] is a subset of arrayB[0..5]\n");
else
printf("arrayA[0..3] is not a subset of arrayB[0..5]\n");
return 0;
}
// To read values into arr
void scanArray(int arr[], int size) {
int i;
printf("Enter %d values: ", size);
for (i=0; i<size; i++) {
scanf("%d", &arr[i]);
}
}
// To print values of arr
void printArray(int arr[], int size)
{
int i;
// To print all values on one line
for (i=0; i<size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// This function is a stub. Complete it.
int isSubset(int arrA[], int sizeA, int arrB[], int sizeB) {
int i,j;
for(i = 0; i < sizeA; i++){ //i will become 7. Imagine i++ at the end of it.
// printf("Value of I: %d", i);
for(j = 0; j <sizeB; j++){
//printf("Value of J: %d\n", j);
if(arrA[i] == arrB[j] )
break;}
if(j == sizeB){ //means that it has reached the end of the arrary for j and [i] is not inside
// printf("Value of J: %d Value of SizeB: %d\n",j,sizeB);
return 0;
}
}
return 1;
}