Question

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program-1:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
   string line;
   char set[2][1000];
   int index[2] = { 0, 0 };
   ifstream inputFile1("prog1input.txt");
   int sset = 0;
   while (getline(inputFile1, line)) {
       for (int i = 0; i < line.length(); i++) {
           if (line[i] != ' ') {
               set[sset][index[sset]++] = line[i];
           }
       }
       sset++;
   }
   inputFile1.close();
   ofstream outputFile1;
   outputFile1.open("prog1output.txt", ios::trunc);
   //Union
   outputFile1 << "1. Union\n";
   {
       bool vis[256] = { 0 };
       for (int i = 0; i < index[0]; i++) {
           if (!vis[set[0][i]]) {
               vis[set[0][i]] = !vis[set[0][i]];
               outputFile1 << set[0][i] << " ";
           }
       }
       for (int i = 0; i < index[1]; i++) {
           if (!vis[set[1][i]]) {
               vis[set[1][i]] = !vis[set[1][i]];
               outputFile1 << set[1][i] << " ";
           }
       }
   }
   outputFile1 << "\n";
   outputFile1 << "2. Intersection\n";
   //Intersection
   {
       bool vis[256] = { 0 };
       for (int i = 0; i < index[0]; i++) {
           vis[set[0][i]] = 1;
       }
       for (int i = 0; i < index[1]; i++) {
           if (vis[set[1][i]]) {
               outputFile1 << set[1][i] << " ";
           }
       }
   }
   outputFile1 << "\n";
   outputFile1 << "3. A-B\n";
   //A-B
   {
       bool vis[256] = { 0 };
       for (int i = 0; i < index[1]; i++) {
           vis[set[1][i]] = 1;
       }
       for (int i = 0; i < index[0]; i++) {
           if (!vis[set[0][i]]) {
               outputFile1 << set[0][i] << " ";
           }
       }
   }
   outputFile1 << "\n";
   outputFile1 << "4. Decide if (A is subset of B)\n";
   //A is subset of B
   {
       bool vis[256] = { 0 };
       for (int i = 0; i < index[0]; i++) {
           vis[set[0][i]] = 1;
       }
       int cnt = 0;
       for (int i = 0; i < index[1]; i++) {
           if (vis[set[1][i]]) {
               cnt++;
           }
       }
       if (cnt == index[0]) {
           outputFile1 << "Yes, A is a subset B";
       }
       else {
           outputFile1 << "No, A is not a subset of B";
       }
   }
   outputFile1.close();
   return 0;
}

Input:

Output:

Program 2:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
   string line;
   char set[3][1000];
   int index[3] = { 0, 0, 0 };
   ifstream inputFile1("prog2input.txt");
   int sset = 0;
   while (getline(inputFile1, line)) {
       for (int i = 0; i < line.length(); i++) {
           if (line[i] != ' ') {
               set[sset][index[sset]++] = line[i];
           }
       }
       sset++;
   }
   inputFile1.close();
   ofstream outputFile1;
   outputFile1.open("prog2output.txt", ios::trunc);
   //Union (A u B) u C
   outputFile1 << "1. (A u B) u C\n";
   {
       bool vis[256] = { 0 };
       for (int i = 0; i < index[0]; i++) {
           if (!vis[set[0][i]]) {
               vis[set[0][i]] = !vis[set[0][i]];
               outputFile1 << set[0][i] << " ";
           }
       }
       for (int i = 0; i < index[1]; i++) {
           if (!vis[set[1][i]]) {
               vis[set[1][i]] = !vis[set[1][i]];
               outputFile1 << set[1][i] << " ";
           }
       }
       for (int i = 0; i < index[2]; i++) {
           if (!vis[set[2][i]]) {
               vis[set[2][i]] = !vis[set[2][i]];
               outputFile1 << set[2][i] << " ";
           }
       }
   }
   outputFile1 << "\n";
   outputFile1 << "2. A u (B n C)\n";
   //A u (B n C)
   {
       bool vis[256] = { 0 };
       bool vis1[256] = { 0 };
       for (int i = 0; i < index[1]; i++) {
           vis[set[1][i]] = 1;
       }
       for (int i = 0; i < index[2]; i++) {
           if (vis[set[2][i]]) {
               outputFile1 << set[2][i] << " ";
               vis1[set[2][i]] = 1;
           }
       }
       for (int i = 0; i < index[0]; i++) {
           if (!vis1[set[0][i]]) {
               outputFile1 << set[0][i] << " ";
           }
       }
   }
   outputFile1 << "\n";
   outputFile1 << "3. (A u B) n (A u C)\n";
   //(A u B) n (A u C)
   {
       bool vis1[256] = { 0 };
       char aub[2000];
       int iaub = 0;
       for (int i = 0; i < index[0]; i++) {
           if (!vis1[set[0][i]]) {
               vis1[set[0][i]] = !vis1[set[0][i]];
               //outputFile1 << set[0][i] << " ";
               aub[iaub++] = set[0][i];
           }
       }
       for (int i = 0; i < index[1]; i++) {
           if (!vis1[set[1][i]]) {
               vis1[set[1][i]] = !vis1[set[1][i]];
               //outputFile1 << set[1][i] << " ";
               aub[iaub++] = set[1][i];
           }
       }
       bool vis2[256] = { 0 };
       char buc[2000];
       int ibuc = 0;
       for (int i = 0; i < index[1]; i++) {
           if (!vis2[set[1][i]]) {
               vis2[set[1][i]] = !vis2[set[1][i]];
               //outputFile1 << set[0][i] << " ";
               buc[ibuc++] = set[1][i];
           }
       }
       for (int i = 0; i < index[2]; i++) {
           if (!vis2[set[2][i]]) {
               vis2[set[2][i]] = !vis2[set[2][i]];
               //outputFile1 << set[1][i] << " ";
               buc[ibuc++] = set[2][i];
           }
       }
       bool vis[256] = { 0 };
       for (int i = 0; i < iaub; i++) {
           vis[aub[i]] = 1;
       }
       for (int i = 0; i < ibuc; i++) {
           if (vis[buc[i]]) {
               outputFile1 << buc[i] << " ";
           }
       }
   }
   outputFile1.close();
   return 0;
}

Input:

Output:

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...

    Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line arguments that represent file names. The first and second files contain four sorted integer numbers that represent set elements. The main process creates a child process that shares the three files. The child process determines the intersection set of two sets and saves the line: "Child process PID: xxxx Intersection of (x, x, x, x) and (y, y, y, y) (z, z, z, z)"...

  • Program has to be in C# language Write a computer program that takes two sets (let...

    Program has to be in C# language Write a computer program that takes two sets (let the user determine the cardinality of each set and enter them manually) and calculates the following operations: Union Intersection Difference (set1 - set2) Cartesian product (set2 X set1) Check whether set2 is a subset of set1 or set1 is a subset of set2 Find the powerset of set1 and print out its elements and cardinality

  • u are to write a program in Java that takes two sets of up to 5...

    u are to write a program in Java that takes two sets of up to 5 symbols(letters, numbers or any characters) and prints the cartesian product of the two sets. The program should allow the user to enter the characters for each set and the output should, consist of the name of each set followed by the characters and on the following line print the cartesian product. Example: {1,3,*} X {3,?,D} = {(1,3),(1,?),(1,D),(3,3),(3,?),(3,D,(*,3),(*,?),(*,D)} or {2,3,5,7} X (5,6} = {(2,5),(2,6),(3,5),(3,6),(5,5),(5,6),(7,5),(7,6)}

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • write a program in c ++ that reads the length and width of a rectangle from...

    write a program in c ++ that reads the length and width of a rectangle from a file named input.txt and write the area and perimeter of the rectactangle to file named output.txt

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • C++ Write a program based on 8_lb that reads in the integer data from "input.txt" into...

    C++ Write a program based on 8_lb that reads in the integer data from "input.txt" into a vector. Please prompt for the file name and append the "txt". Then create another vector where each element is the original vector times 10. Create a variable total and sum up the numbers from the second vector. The total should be 17510. Then write the data from the original vector and the 10 times vector to a file. See output.txt for the format....

  • write a program in c++ Read an infix expression from an input file and convert to...

    write a program in c++ Read an infix expression from an input file and convert to postfix. Instead of displaying directly on the screen, first place in a queue, and then display the contents of the queue on the screen. Precondition: The expression will be read from a file (input.txt) that contains a single line. There will be no spaces between the operands and the operators. The following operators are allowed: ( ) + - * / The normal rules...

  • Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your...

    Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your program will reverse every line in the input. This program does not have an output file, as it modifies the input file directly. You may assume that your input file has 1000 lines or less, if this helps. sample input: Hello World! I study C++ following output: !dlroW olleH ++C yduts I

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT