/*
* File: sets.cpp
* ------------------
* This program deals with sets of lower case letters.
* This program computes the union and intersection of two sets. This program
* also checks if set B is a subset of set A and returns true if set B is a subset
* of set A and returns false if not.
*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

#define size 26

void init(bool set[]);
void initC(bool setC[]);
void displaySet(bool set[]);
bool unions(bool A[],bool B[],bool C[]);
bool intersection(bool A[],bool B[],bool C[]);
bool contain(bool A[],bool B[]);

typedef enum
{
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
}
letters;
int main()
{
bool A[size];
bool B[size];
bool C[size];
printf("This program deals with sets of lower case letters. This program computes the\n"); 
printf("union and intersection of two sets. This program also checks if set B is a\n");
printf("subset of set A and returns true if set B is a subset of set A and returns\n");
printf("false if not.\n");
printf("\n");
printf("Enter a list of lowercase letters as elements for set A.\n");
init(A);
printf("\n");
printf("Enter a list of lowercase letters as elements for set B.\n");
init(B);
initC(C);
unions(A,B,C);
displaySet(C);
initC(C);
intersection(A,B,C);
initC(C);
if(contain(A,B))
printf("B is a proper subset of A: true\n");
else
printf("B is a proper subset of A: false\n");
}

/*************************************...
/* This function initializes the values for each set. */

void init(bool set[])
{
char element;
letters l;
printf("Press enter after each element.\n");
printf("Signal the end of the list by pressing 'control' 'z' and 'enter'.\n");
printf("Enter the first element:");
for(l=a; l<=z;l=(letters)(l+1))
{
set[l]=false;
}
while(element=getchar()!=EOF)
{
element=element-97;
l=(letters)element;
if(l>=a&&l<=z)
set[l]=true;
}
}

/*************************************...
/* This function displays the values of a set to the output. */


void displaySet(bool set[])
{
letters l;
for(l=a; l<=z; l=(letters)(l+1))
{
if(set[l])
printf("%c", (l+97));
}
}

/*************************************...
/*This function computes the union of sets A and B and puts the value in set C.*/

bool unions(bool A[],bool B[],bool C[])
{
letters letter;
printf("The union of set A and set B = { ");
for(letter=a; letter<=z; letter=(letters)(letter+1))
{
if((A[letter])||(B[letter]))
C[letter]=true;

}
return(C);
}

/***********************************in...
/* This function computes the intersection of set A and set B and puts the result
* in set C. */

bool intersection(bool A[],bool B[],bool C[])
{
letters letter;
for(letter=a; letter<=z; letter=(letters)(letter+1))
{
if(A[letter]&&B[letter])
C[letter]=true;
}
return(C);
}

/************************************c...
/* This function returns true if B is a proper subset of A and returns false otherwise. */

bool contain(bool A[],bool B[])
{
bool isSubset;
letters letter;
isSubset=true;
for(letter=a; letter<=z; letter=(letters)(letter+1))
{
if(B[letter]&&A[letter])
{
isSubset=true;
A[letter]=false;
B[letter]=false;
}
if(B[letter]&&!A[letter])
isSubset=false; break;
}
if(isSubset)
{
isSubset=false;
for(letter=a; letter<=z; letter=(letters)(letter+1))
{
if(A[letter]&&!B[letter])
isSubset=true;
}
}
return(isSubset);
}

/*************************************...
/* This function re-initializes the values of set C to false. */

void initC(bool setC[])
{
letters l;
for(l=a; l<=z; l=(letters)(l+1))
{
setC[l]=false;
}
}

I am not able to get the sets to print on the screen. Can someone please help me with a solution to this.

Recommended Answers

All 4 Replies

Step 1, learn to indent code. Even programs as short as this are close to unreadable without good indentation.

/*
* File: sets.cpp
* ------------------
* This program deals with sets of lower case letters.
* This program computes the union and intersection of two sets. This program
* also checks if set B is a subset of set A and returns true if set B is a subset
* of set A and returns false if not.
*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

#define size 26

void init(bool set[]);
void initC(bool setC[]);
void displaySet(bool set[]);
bool unions(bool A[],bool B[],bool C[]);
bool intersection(bool A[],bool B[],bool C[]);
bool contain(bool A[],bool B[]);

typedef enum
{
    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
}
letters;
int main()
{
    bool A[size];
    bool B[size];
    bool C[size];
    printf("This program deals with sets of lower case letters. This program computes the\n"); 
    printf("union and intersection of two sets. This program also checks if set B is a\n");
    printf("subset of set A and returns true if set B is a subset of set A and returns\n");
    printf("false if not.\n");
    printf("\n");
    printf("Enter a list of lowercase letters as elements for set A.\n");
    init(A);
    printf("\n");
    printf("Enter a list of lowercase letters as elements for set B.\n");
    init(B);
    initC(C);
    unions(A,B,C);
    displaySet(C);
    initC(C);
    intersection(A,B,C);
    initC(C);
    if(contain(A,B))
    printf("B is a proper subset of A: true\n");
    else
    printf("B is a proper subset of A: false\n");
}

/*************************************...
/* This function initializes the values for each set. */

void init(bool set[])
{
    char element;
    letters l;
    printf("Press enter after each element.\n");
    printf("Signal the end of the list by pressing 'control' 'z' and 'enter'.\n");
    printf("Enter the first element:");
    for(l=a; l<=z;l=(letters)(l+1))
    {
        set[l]=false;
    }
    while(element=getchar()!=EOF)
    {
        element=element-97;
        l=(letters)element;
        if(l>=a&&l<=z)
        set[l]=true;
    }
}

/*************************************...
/* This function displays the values of a set to the output. */


void displaySet(bool set[])
{
    letters l;
    for(l=a; l<=z; l=(letters)(l+1))
    {
        if(set[l])
        printf("%c", (l+97));
    }
}

/*************************************...
/*This function computes the union of sets A and B and puts the value in set C.*/

bool unions(bool A[],bool B[],bool C[])
{
    letters letter;
    printf("The union of set A and set B = { ");
    for(letter=a; letter<=z; letter=(letters)(letter+1))
    {
        if((A[letter])||(B[letter]))
        C[letter]=true;

    }
    return(C);
}

/***********************************in...
/* This function computes the intersection of set A and set B and puts the result
* in set C. */

bool intersection(bool A[],bool B[],bool C[])
{
    letters letter;
    for(letter=a; letter<=z; letter=(letters)(letter+1))
    {
        if(A[letter]&&B[letter])
        C[letter]=true;
    }
    return(C);
}

/************************************c...
/* This function returns true if B is a proper subset of A and returns false otherwise. */

bool contain(bool A[],bool B[])
{
    bool isSubset;
    letters letter;
    isSubset=true;
    for(letter=a; letter<=z; letter=(letters)(letter+1))
    {
        if(B[letter]&&A[letter])
        {
            isSubset=true;
            A[letter]=false;
            B[letter]=false;
        }
        if(B[letter]&&!A[letter])
        isSubset=false; break;
    }
    if(isSubset)
    {
        isSubset=false;
        for(letter=a; letter<=z; letter=(letters)(letter+1))
        {
            if(A[letter]&&!B[letter])
            isSubset=true;
        }
    }
    return(isSubset);
}

/*************************************...
/* This function re-initializes the values of set C to false. */

void initC(bool setC[])
{
    letters l;
    for(l=a; l<=z; l=(letters)(l+1))
    {
        setC[l]=false;
    }
}

> while(element=getchar()!=EOF)
You've written this while(element=(getchar()!=EOF)) You want this while((element=getchar())!=EOF) Note the positioning of the extra ( ), and also look up operator precedence in your C book.

Further, element should be an int, not a char

Also, when the loop exits, because you did EOF, you need to do clearerr( stdin ); Otherwise on the next input, EOF will still be true and you have no further input.

OK here's my revised code but it still doesn't work. Whats wrong?

/*
* File: sets.cpp
* ------------------
* This program deals with sets of lower case letters.
* This program computes the union and intersection of two sets. This program
* also checks if set B is a subset of set A and returns true if set B is a subset
* of set A and returns false if not.
*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

#define size 26

void init(bool set[]);
void initC(bool setC[]);
void displaySet(bool set[]);
bool unions(bool A[],bool B[],bool C[]);
bool intersection(bool A[],bool B[],bool C[]);
bool contain(bool A[],bool B[]);

typedef enum
{
	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
}
letters;
int main()
{
	bool A[size];
	bool B[size];
	bool C[size];
	printf("This program deals with sets of lower case letters. This program computes the\n"); 
	printf("union and intersection of two sets. This program also checks if set B is a\n");
	printf("subset of set A and returns true if set B is a subset of set A and returns\n");
	printf("false if not.\n");
	printf("\n");
	printf("Enter a list of lowercase letters as elements for set A.\n");
	init(A);
	printf("\n");
	printf("Enter a list of lowercase letters as elements for set B.\n");
	displaySet(A);
	init(B);
	displaySet(B);
	initC(C);
	unions(A,B,C);
	displaySet(C);
	initC(C);
	intersection(A,B,C);
	initC(C);
	if(contain(A,B))
		printf("B is a proper subset of A: true\n");
	else
		printf("B is a proper subset of A: false\n");
}

/****************************************init***************************************/
/* This function initializes the values for each set. */

void init(bool set[])
{
	int element;
	letters l;
	printf("Press enter after each element.\n");
	printf("Signal the end of the list by pressing 'control' 'z' and 'enter'.\n");
	printf("Enter the first element:");
	for(l=a; l<=z;l=(letters)(l+1))
	{
		set[l]=false;
	}
	while((element=(getchar())!=EOF))
	{
		element=element-97;
		l=(letters)element;
		set[l]=true;
	}
}

/*************************************displaySet**************************************/
/* This function displays the values of a set to the output. */


void displaySet(bool set[])
{
	letters l;
	for(l=a; l<=z; l=(letters)(l+1))
	{
		if(set[l])
			printf("%g", (l+97));
	}
}

/*************************************unions****************************************/
/*This function computes the union of sets A and B and puts the value in set C.*/

bool unions(bool A[],bool B[],bool C[])
{
	letters letter;
	printf("The union of set A and set B = { ");
	for(letter=a; letter<=z; letter=(letters)(letter+1))
	{
		if((A[letter])||(B[letter]))
		{
			C[letter]=true;
		}

	}
	return(C);
}

/***********************************intersection*********************************/
/* This function computes the intersection of set A and set B and puts the result
*  in set C. */

bool intersection(bool A[],bool B[],bool C[])
{
	letters letter;
	for(letter=a; letter<=z; letter=(letters)(letter+1))
	{
		if(A[letter]&&B[letter])
			C[letter]=true;
	}
	return(C);
}

/************************************contain***********************************/
/* This function returns true if B is a proper subset of A and returns false otherwise. */

bool contain(bool A[],bool B[])
{
	bool isSubset;
	letters letter;
	isSubset=true;
	for(letter=a; letter<=z; letter=(letters)(letter+1))
	{
		if(B[letter]&&A[letter])
		{
			isSubset=true;
			A[letter]=false;
			B[letter]=false;
		}
		if(B[letter]&&!A[letter])
			isSubset=false; break;
	}
	if(isSubset)
	{
		isSubset=false;
		for(letter=a; letter<=z; letter=(letters)(letter+1))
		{
			if(A[letter]&&!B[letter])
				isSubset=true;
		}
	}
	return(isSubset);
}

/****************************************initC***********************************/
/* This function re-initializes the values of set C to false. */

void initC(bool setC[])
{
	letters l;
	for(l=a; l<=z; l=(letters)(l+1))
	{
		setC[l]=false;
	}
}

What's wrong is you can't read.

> while((element=(getchar())!=EOF))
Is exactly the same as your original post, with more parentheses.
Go read my reply again, and study where I placed them.
No wait, that won't work this time, any more than it did last time.

Here, the critical ( ) emphasised while([B]([/B]element=getchar()[B])[/B]!=EOF) Plus you ignored the whole clearerr thing I mentioned.

Here, paste this as your loop.

while(element=getchar()!=EOF)
    {
[B]        printf( "Interesting, element is decimal %d\n", element );
[/B]        element=element-97;
[B]        if ( element < 0 ) printf( "OMG, WTF, it's now %d\n", element );
[/B]        l=(letters)element;
        if(l>=a&&l<=z)
        set[l]=true;
    }

Putting temporary printf statements all over the place is the first step to debugging your own code.

When you get fed up of continually editing the code with new printf statements, and taking out ones you no longer need, then come back and we'll explain debuggers to you.

Why does the first printf statement execute twice in the same loop?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.