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 1:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main() {

   string line;

   char sets[2][1000];

   int index[2] = { 0, 0 };

   ifstream ifs("prog1 input.txt");

   int ssets = 0;

   while (getline(ifs, line)) {

       for (int i = 0; i < line.length(); i++) {

           if (line[i] != ' ') {

               sets[ssets][index[ssets]++] = line[i];

           }

       }

       ssets++;

   }

   ifs.close();

   ofstream ofs;

   ofs.open("prog1 output.txt", ios::trunc);

   //Union

   ofs << "1. Union\n";

   {

       bool temp[256] = { 0 };

       for (int i = 0; i < index[0]; i++) {

           if (!temp[sets[0][i]]) {

               temp[sets[0][i]] = !temp[sets[0][i]];

               ofs << sets[0][i] << " ";

           }

       }

       for (int i = 0; i < index[1]; i++) {

           if (!temp[sets[1][i]]) {

               temp[sets[1][i]] = !temp[sets[1][i]];

               ofs << sets[1][i] << " ";

           }

       }

   }

   ofs << "\n";

   ofs << "2. Intersection\n";

   //Intersection

   {

       bool temp[256] = { 0 };

       for (int i = 0; i < index[0]; i++) {

           temp[sets[0][i]] = 1;

       }

       for (int i = 0; i < index[1]; i++) {

           if (temp[sets[1][i]]) {

               ofs << sets[1][i] << " ";

           }

       }

   }

   ofs << "\n";

   ofs << "3. A-B\n";

   //A-B

   {

       bool temp[256] = { 0 };

       for (int i = 0; i < index[1]; i++) {

           temp[sets[1][i]] = 1;

       }

       for (int i = 0; i < index[0]; i++) {

           if (!temp[sets[0][i]]) {

               ofs << sets[0][i] << " ";

           }

       }

   }

   ofs << "\n";

   ofs << "4. Decide if (A is subsets of B)\n";

   //A is subsets of B

   {

       bool temp[256] = { 0 };

       for (int i = 0; i < index[0]; i++) {

           temp[sets[0][i]] = 1;

       }

       int cnt = 0;

       for (int i = 0; i < index[1]; i++) {

           if (temp[sets[1][i]]) {

               cnt++;

           }

       }

       if (cnt == index[0]) {

           ofs << "Yes, A is a subsets B";

       }

       else {

           ofs << "No, A is not a subsets of B";

       }

   }

   ofs.close();

   return 0;

}

Program 2:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main() {

   string line;

   char sets[3][1000];

   int index[3] = { 0, 0, 0 };

   ifstream ifs("prog2 input.txt");

   int ssets = 0;

   while (getline(ifs, line)) {

       for (int i = 0; i < line.length(); i++) {

           if (line[i] != ' ') {

               sets[ssets][index[ssets]++] = line[i];

           }

       }

       ssets++;

   }

   ifs.close();

   ofstream ofs;

   ofs.open("prog2 output.txt", ios::trunc);

   //Union (A u B) u C

   ofs << "1. (A u B) u C\n";

   {

       bool temp[256] = { 0 };

       for (int i = 0; i < index[0]; i++) {

           if (!temp[sets[0][i]]) {

               temp[sets[0][i]] = !temp[sets[0][i]];

               ofs << sets[0][i] << " ";

           }

       }

       for (int i = 0; i < index[1]; i++) {

           if (!temp[sets[1][i]]) {

               temp[sets[1][i]] = !temp[sets[1][i]];

               ofs << sets[1][i] << " ";

           }

       }

       for (int i = 0; i < index[2]; i++) {

           if (!temp[sets[2][i]]) {

               temp[sets[2][i]] = !temp[sets[2][i]];

               ofs << sets[2][i] << " ";

           }

       }

   }

   ofs << "\n";

   ofs << "2. A u (B n C)\n";

   //A u (B n C)

   {

       bool temp[256] = { 0 };

       bool temp1[256] = { 0 };

       for (int i = 0; i < index[1]; i++) {

           temp[sets[1][i]] = 1;

       }

       for (int i = 0; i < index[2]; i++) {

           if (temp[sets[2][i]]) {

               ofs << sets[2][i] << " ";

               temp1[sets[2][i]] = 1;

           }

       }

       for (int i = 0; i < index[0]; i++) {

           if (!temp1[sets[0][i]]) {

               ofs << sets[0][i] << " ";

           }

       }

   }

   ofs << "\n";

   ofs << "3. (A u B) n (A u C)\n";

   //(A u B) n (A u C)

   {

       bool temp1[256] = { 0 };

       char aUb[2000];

       int iaUb = 0;

       for (int i = 0; i < index[0]; i++) {

           if (!temp1[sets[0][i]]) {

               temp1[sets[0][i]] = !temp1[sets[0][i]];

               //ofs << sets[0][i] << " ";

               aUb[iaUb++] = sets[0][i];

           }

       }

       for (int i = 0; i < index[1]; i++) {

           if (!temp1[sets[1][i]]) {

               temp1[sets[1][i]] = !temp1[sets[1][i]];

               //ofs << sets[1][i] << " ";

               aUb[iaUb++] = sets[1][i];

           }

       }

       bool temp2[256] = { 0 };

       char bUc[2000];

       int ibUc = 0;

       for (int i = 0; i < index[1]; i++) {

           if (!temp2[sets[1][i]]) {

               temp2[sets[1][i]] = !temp2[sets[1][i]];

               //ofs << sets[0][i] << " ";

               bUc[ibUc++] = sets[1][i];

           }

       }

       for (int i = 0; i < index[2]; i++) {

           if (!temp2[sets[2][i]]) {

               temp2[sets[2][i]] = !temp2[sets[2][i]];

               //ofs << sets[1][i] << " ";

               bUc[ibUc++] = sets[2][i];

           }

       }

       bool temp[256] = { 0 };

       for (int i = 0; i < iaUb; i++) {

           temp[aUb[i]] = 1;

       }

       for (int i = 0; i < ibUc; i++) {

           if (temp[bUc[i]]) {

               ofs << bUc[i] << " ";

           }

       }

   }

   ofs.close();

   return 0;

}

Output and input files:


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 Python program that takes as input two numbers, the height, and width of a...
Write a Python program that takes as input two numbers, the height, and width of a rectangle. It then prints a rectangle with height lines and width characters in each line. The rectangle should contain information regarding its dimensions and area inside (centered vertically but not horizontally). If any part of the information (including two stars on each end and a space before and after the line) does not fit in the rectangle, then print the complete information after the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT