Question

In: Computer Science

Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file prog1 output.txt. Write a title before the output of each operation.
1. Union
2. Intersection
3. A - B
4. Decide if (A ⊂ B).
Program 2 – 50 points
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file prog2 output.txt and the standard output. Write a title before the output of each operation.
1. (A∪B)∪C.
2. A∪(B∩C).
3. (A∪B)∩(A∪C). HINTS
•You may want to use arrays.
•Pay special attention to the parentheses because they indicate the order. For example: (A ∪ B) ∪ C means to compute (A ∪ B) first, and then ∪C.
•Consider reusing the algorithms for union and intersection that you have defined already. For example, to compute (A ∪ B) ∪ C:
First, compute A ∪ B using the function that computes the union that you already defined. Let’s say that you store that result in an array R.
Then, compute R ∪ C using the function that computes the union that you already defined.

Solutions

Expert Solution

Program

#include <iostream>
#include <fstream>
using namespace std;

string unionOf(int a[], int b[]){
string s="Union of SET A and SET B is ";
for(int i=0; i<128; i++){
if(a[i]==1 || b[i]==1){
char c=i;
s+=c;
s+=" ";
}
}
return s;
}

string intersection(int a[], int b[]){
string s="intersection of SET A and SET B is ";
for(int i=0; i<128; i++){
if(a[i]==1 && b[i]==1){
char c=i;
s+=c;
s+=" ";
}
}
return s;
}

string difference(int a[], int b[]){
string s="Difference of SET A and SET B is ";
for(int i=0; i<128; i++){
if(a[i]-b[i]==1){
char c=i;
s+=c;
s+=" ";
}
}
return s;
}

string properSubset(int a[], int b[]){
string s;
int m=0;
int n=0;
for(int i=0; i<128; i++){
if(b[i]-a[i]==-1){
return "SET A is not a proper subset of SET B";
}
if(a[i]==1){
m++;
}
if(b[i]==1){
n++;
}
}
if(m==n){
return "SET A is not a proper subset of SET B";
}
return "SET A is a proper subset of SET B";
}

int main(){
int setA[128];
int setB[128];

for(int i=0; i<128; i++){
setA[i]=0;
setB[i]=0;
}

string str;
int j;

ifstream file("Read.txt");

getline(file, str);
cout<<str<<endl;
for(int i=9; i<str.length(); i++){
j=(int)str.at(i);
setA[j]=1;
}

getline(file,str);
cout<<str<<endl;
for(int i=0; i<str.length(); i++){
j=(int)str.at(i);
setB[j]=1;
}

ofstream myfile;
myfile.open ("output.txt");
string result=unionOf(setA,setB);
cout<<result<<endl;
myfile<<result<<endl;

result=intersection(setA,setB);
cout<<result<<endl;
myfile<<result<<endl;

result=difference(setA,setB);
cout<<result<<endl;
myfile<<result<<endl;

cout<<properSubset(setA,setB)<<endl;
myfile<<result<<endl;
myfile.close();

}

Output


Related Solutions

Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
Program this using C please The program will read from standard input two things - a...
Program this using C please The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1 and string str2 such that any lower-case alphabet character (a-z) will...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT