-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerset.c
110 lines (82 loc) · 2.8 KB
/
powerset.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
//2^input Method
unsigned int twoPow(unsigned int input) {
unsigned int i;
unsigned int output = 1;
for (i = 0; i < input; i++) {
output *= 2;
}
return output;
}
// Checking input for integer method
short isInteger(char s[]) {
for (short i = 0; s[i] != '\0'; i++) {
if (isdigit(s[i]) == 0)
return 0;
}
return 1;
}
int main() {
// Declaration of data for input
char setSize[2];
unsigned int arraySize;
unsigned int i;
do {
printf("Please enter the cardinality of your set S. Please note that cardinality cannot be bigger than 20 inclusive. Insertion of invalid characters will prompt the software to ask for cardinality again. \n");
scanf("%s", setSize);
arraySize = strtol(setSize, NULL, 0); /* conversion of string to int */
} while (isInteger(setSize) == 0 || arraySize >= 20);
// If it empty set, the output is immediate
if (arraySize == 0) {
printf("The only subset of your entered set S is : \n");
printf("{} \n");
return 0;
}
// Declaration of data to process the input
char *S[arraySize];
char *powerSetS[twoPow(arraySize)];
char *powerSetSStar[twoPow(arraySize-1) - 1];
size_t malloc_size = 100;
// Input of elements of the set S by the user
for (i = 0; i < arraySize; i++) {
S[i] = malloc(malloc_size * sizeof(char)); /* allocates char byte size */
printf("Enter element: ");
scanf("%99s", S[i]); /* set as 99 to avoid overflow */
}
// Allocation of memory to pointer array for strings
for (i = 0; i < twoPow(arraySize-1); i++) {
powerSetSStar[i] = malloc(malloc_size * sizeof(char)); /* allocates char byte size */
}
for (i = 0; i < twoPow(arraySize); i++) {
powerSetS[i] = malloc(malloc_size * sizeof(char)); /* allocates char byte size */
}
// The previously explained mathematical algorithm in action
for (i = 0; i < arraySize; i++) {
// Declaration of variables for loops
unsigned int k;
unsigned int n;
if (i == 0) {
powerSetS[i] = S[i]; /* Adding a singleton to initiate the algorithm */
}
else {
for (k = 0; k < twoPow(i)-1; k++) {
strcpy(powerSetSStar[k], powerSetS[k]); /* copy each element */
strcat(powerSetSStar[k], ", "); /* comma element separator for each subset */
strcat(powerSetSStar[k], S[i]); /* add new element to each previously defined element */
strcpy(powerSetS[twoPow(i) - 1 + k], powerSetSStar[k]); /* set the new elements back to the power set */
n = k;
}
strcpy(powerSetS[twoPow(i) + n], S[i]); /* add the singleton element */
}
}
powerSetS[twoPow(arraySize)-1] = ""; /* defining the empty set */
// Printing power set
printf("The subsets for your set S are (each separated by a new line): \n");
for (i = 0; i < twoPow(arraySize); i++) {
printf("{%s} \n", powerSetS[i]);
}
return 0;
}