sets of letters

Pages: 12
I have to write a program that deals with sets of letters. The program should include the following
functions:
unions(A,B,C), that computes AUB and puts the result in C. /* Do not use the word ‘union’
since it’s a key word in C */
intersection(A,B,C), that computes AIB and puts the result in C.
contain(A,B), A boolean function that returns TRUE if B 􀀀 A and FALSE otherwise.
init(set), that initializes the set. (This procedure will be called for each of the sets.) i need some tips. please help
Last edited on
oh and i think i need to use the typedef function
typedef is not a function, it is a keyword.

What kind of tips do you need?
tips on what i need to do.
Last edited on
Very funny.
You know what a union is in set theory right? It's basically an or function. Any object in one, the other or both of the sets is part of the union of the sets. The intersection of the sets is the objects that are in both. Etc.
So what are your limitations in this homework assignment? Vectors, arrays, anything goes?
Do you have any useful information pertaining to your task?
Last edited on
I was not told of any limitations and I was given some information

• Use the typedef to define a new type named ‘letters’ that includes all the alphabetic letters.
Define a boolean array of size 26 to represent the set. So, for example, the set, {‘a’, ‘b’ , ‘c’}
will be represented as follows,
letterArray,
T T T F F F F F F F F F F F F F F F F F F F F F F F
a b c d e f g h i j k l m n o p q r s t u v w x y z
For each letter, if letterArray[letter]==TRUE then letter belongs to the group and if its
FALSE it means that letter does not belong to the group.
• Note that typedef’s when used in input and output operations are considered as integers and
not as characters (letters). Thus, if you want to write a procedure disp that displays the letters
of the set to the output, the following version would not work –
void disp (bool set[size]) /* This would NOT work!!! */
{
letters l;
for (l=a; l<=z; l++)
{
if (set[l]) printf (“\t%c”,l);
}
}
Instead you should “convert” the integer that represents each letter of the letters typedef, to the
corresponding character, using the ASCII table shown earlier.
So the correct version of disp will look like this,
void disp (bool set[size]) /* The correct version */
{
letters l;
for (l=a; l<=z; l++)
{
if (set[l]) printf (“\t%c”,l+97);
}
}
/* The value of the variable l is in the range of 0-25 since it is defined as an instance of the letters
typedef. Adding 97 to l will return the ASCII code of the corresponding lower case letter). */
This is an interesting assignment. I particularly like how the arrays of booleans will make finding unions and intersections a snap! Well, I suppose it would be a snap regardless...

Just to contrast approaches, if this was not an assignment I'd be all over the STL set_union and set_intersection algorithms for this one.

Does this have to be in C, like the examples, or is C++ allowed?
Last edited on
OK, what's the point of the typedef if you just have primitive char?
Anyway.... I'd prefer to create an enumeration of the letters if you really must.
The letter array should be an array of bools obviously. 26 also obviously.
As I read down I see that the typedef should be represented as an integer and that's just more reason to enumerate it instead.
Encoding that ascii table from int to letter will be your problem but of course an enum may be able to get around that. Seeing as I've recommended it so many times (but of course, my experience is limited), you can check it out in the tutorial on this site.
OK. So here's, I suppose, how it should roll.
For your union, go for (as in the loop) from 0 to 25. If a value is true, you turn the value of the corresponding element in C to true. To save time try something like this:
1
2
if (A[x] == true || B[x] == true)
    C[x] == true;

Assuming x is your counter.
Do the same thing for intersection except instead, change that || to an &&.
For your contain function, I assume that asks if B is a subset of A? In that case, cycle through B. If at any time B contains a value A does not, return false. At the end return true. That way if you cycle through the whole thing without having returned you know all of B's elements are in A, so you can return true.
1
2
3
4
5
6
for (int x = 0; x < 26; x++)
{
    if (B[x] == true && A[x] != true)
        return false;
}
return true;

You see the idea right?
I'm not sure what to do for initializing the set. Set everything to false? What is expected?
This is a clever assignment, I agree. It's better than a lot I've had.
Last edited on
moorecm the name of the compiler i am using is Dev-C++ 4.9.9.2 and tummychow i have told you all i know so i dont know what im supposed to do with init(set) i was just told to initialize the sets A and B accordingly within the program. and i think i do see the idea.
Last edited on
oh i think the init(set) is supposed to give values to A and B. but how would i do this? Please help.
Last edited on
Please help I don't know what to do.
We already explained it to you. What still is unclear?
And how are we supposed to know what init does better than you do?
oh i think the init(set) is supposed to give values to A and B

Pray tell, what are those values? If you can't tell us how can we tell you?
@coolPorgramer5: I think what we are asking is that you make some sort of attempt at the
problem and post your code. Otherwise we aren't sure how to help you short of writing the
entire assignment for you, and we won't do that.

It appears you understand the mathematical definition of a set and what the union and
intersection operations do. The first step is to figure out what C or C++ data structure you
will use to represent the set. Then, as a good next step, you can write a function that
takes the intersection of two sets. To do that, you'll need to write another function that
searches a set for a given element.
If you're wondering what data structure should represent the set, you'd be best off with an enumeration for your character (if you can't use char) and an array of 26 booleans.
I gave you the core code for your union/intersection functions. It's not as difficult as it sounds, surely you have enough to get started.
sorry I've been busy all day. And i'm really sorry but i don't understand what you mean by enumerating a character.
Last edited on
K, then check the documentation tutorial on enumerations. I still don't see why you can't just use char, and check if it's a character using cctype.
EDIT: Here's the link: http://www.cplusplus.com/doc/tutorial/other_data_types/
Scroll down a bit.
Last edited on
I tried to make the program but didn't get the results i wanted.

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
#include <stdio.h>
#include <ctype.h>
#include "simpio.h"

main()
{
      int x;
      char A[x];
      char B[x];
      char C[x];
      printf("Enter characters for set A \n");
      printf("\n\n");
      printf("A=");
     for (int x = 0; x < 26; x++)
     {
            C[x]=getchar();
            if (isalpha(C[x]) == true)
            {
            A[x]=C[x];
            printf("%d, ", A[x]);
            }
     }
      printf("Enter characters for set B \n");
      printf("\n\n");
     printf("B=");
     for (x = 0; x < 26; x++)
     {
         C[x]=getchar();
         if (isalpha(C[x]) == true)
            {
            B[x]=C[x];
            printf("%d, ", B[x]);
            }
     }
     if (A[x] == true || B[x] == true)
     {
    C[x] == true;
     }
     for (int x = 0; x < 26; x++)
{
    if (B[x] == true && A[x] != true)
        return false;
}
return true;
}
That indenting is disturbing. You realize that you missed int in int main, and you return 0 and not true, right?
And you seem to have a bad habit of interpreting people exactly. When I gave you that example code, I meant that you should put it in the functions you were going to create - union(), intersection() and contains(). Not slap them into main. There's no way in hell that would work.
Last edited on
Ok i rewrote my program but i'm getting a million errors like:
too few arguments to function `char unions(char, char, char)'


obviously im doing a lot of things wrong.

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
#include <stdio.h>
#include <ctype.h>
#include "simpio.h"

char unions(char A, char B, char C);
bool intersections(char A, char B, char C);
bool contain(char A, char B);
int main()
{
    typedef char* letters;
      
      char A;
      char B;
      char C;
      unions(A, B);
      intersection(A, B);
      contain(A[x], B[x]);
      return 0
}
char unions(char A, char B, char C)
{
     printf("AUB=");
     int x;
     typedef enum { /*these are set A's values you may change if you wish but you have to change in the rest of the program*/
             a,b,c,d,e,f,g,h,i,j 
                  }A;
     typedef enum { /*these are set A's values you may change if you wish but you have to change in the rest of the program*/
             a,b,c,j
                  }B;
                  for (x=0; x < 10; x++)
                  { 
                    if (A[x] == true || B[x] == true)
                    {
                           C[x] == true; 
                           printf("%c, ", C[x]);
                    }
}
bool intersections(char A, char B, char C);
{
     int x, y;
     y=0;
     typedef enum { 
             a,b,c,d,e,f,g,h,i,j 
                  }A;
     typedef enum { 
             a,b,c,j
                  }B;
                  for (x=0; x<4; x++)
                  {
                      if (B[x] == A[y])
                      {
                               printf("%c, ", B[x]);
                      }
                      if (B[x] != A[y])
                      {
                               y+=1;
                      }
                  }
                  getchar();
}
bool contain(char A, char B)
{
     int x, y;
     x=0;
     typedef enum { 
             a,b,c,d,e,f,g,h,i,j 
                  }A;
     typedef enum { 
             a,b,c,j
                  }B;
                  while (x<4)
                  {
                        for (y=0; y<10; y++)
                        {
                            if (B[y] != A[x])
                            {
                                     printf("\nFALSE");
                                     break; 
                            }
                            if (B[y] == A[x])
                            {
                                     y+=1;
                            }
                        }
                  }
                  getchar();
}
Alright, overlooking some of the very strange indenting:
Enumerations should be defined at the very start of the program and, if you had read the tutorial I had pointed you to, you would know that an enumeration is not a typedef. It is a type.
1
2
enum letters { a,b,c,d }; // declares a type
typedef enum {a, b, c, d} // enum is a keyword, this is an error 

Secondly, you pass individual characters to all three of your functions and then you try to subscript them. This is also an obvious error because you should be passing char* for the arrays you intend you use. You created the typedef letters, use it. Typedefs should also, generally, be done globally.
Finally you forgot the semicolon in return 0.
EDIT: In addition, union and intersection probably do not need and should not have a return, and contain should have a return.
Last edited on
Pages: 12