Question

In: Computer Science

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 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.

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

  • PLease put COMMENTS. I am new to C++

Solutions

Expert Solution

Program 1:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main() {

   string line;

   char sets[2][1000]; //for two sets

   int index[2] = { 0, 0 }; //to keep track of index of each set

   ifstream ifs("prog1 input.txt");

   int ssets = 0;

   //to read the elements from file

   while (getline(ifs, line)) {

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

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

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

           }

       }

       ssets++;

   }

   ifs.close(); //closing the input file

   ofstream ofs;

   ofs.open("prog1 output.txt", ios::trunc); //opening the output file

   //to do Union of the two sets

   ofs << "1. Union\n";

   {

       bool temp[256] = { 0 };

                   //printing elementsthat are present only in set 1

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

           if (!temp[sets[0][i]]) {     //checking if the element is flagged

               temp[sets[0][i]] = !temp[sets[0][i]];     //flagging element and printing it

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

           }

       }

                   //printing elementsthat are present only in set 2

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

           if (!temp[sets[1][i]]) {    //checking if the element is flagged

               temp[sets[1][i]] = !temp[sets[1][i]];    //flagging element and printing it

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

           }

       }

   }

   ofs << "\n";

   ofs << "2. Intersection\n";

   //to do Intersection of two sets

   {

                   //flaging the elementsthat are in set 1

       bool temp[256] = { 0 };

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

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

       }

                   //traversing set 2 and printing only those elementsthat are flagged

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

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

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

           }

       }

   }

   ofs << "\n";

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

   //to do A-B

   {

                   //flaging the elementsthat are in set 1

       bool temp[256] = { 0 };

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

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

       }

                   //traversing set 2 and printing only those elementsthat are flagged

       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";

   //to find if A is a subset of B

   {

                   //flaging the elementsthat are in set 1

       bool temp[256] = { 0 };

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

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

       }

                   //traversing the set 2 and increasing counter for every element that was flagged

       int cnt = 0;

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

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

               cnt++;

           }

       }

                   //compparing counter with number of elements in set A

       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];   //for 3 sets

   int index[3] = { 0, 0, 0 };    //to keep track of index of each set

   ifstream ifs("prog2 input.txt");

   int ssets = 0;

   //to read the elements from file

   while (getline(ifs, line)) {

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

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

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

           }

       }

       ssets++;

   }

   ifs.close();    //closing the input file

   ofstream ofs;

   ofs.open("prog2 output.txt", ios::trunc);    //opening the output file

   //Union (A u B) u C

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

   {

       bool temp[256] = { 0 };

                   //printing distinct elements of set A

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

           if (!temp[sets[0][i]]) {     //checking if the element is flagged

               temp[sets[0][i]] = !temp[sets[0][i]]; //flagging element and printing it

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

           }

       }

                   //printing distinct elements of set B

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

           if (!temp[sets[1][i]]) {    //checking if the element is flagged

               temp[sets[1][i]] = !temp[sets[1][i]];    //flagging element and printing it

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

           }

       }

                   //printing distinct elements of set C

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

           if (!temp[sets[2][i]]) {    //checking if the element is flagged

               temp[sets[2][i]] = !temp[sets[2][i]];    //flagging element and printing it

               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 };

                   //flaging all elements of set B in temp array

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

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

       }

                   //traversing set C and printing elementsthat are not flagged in temp

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

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

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

                                                   //flaging the printed elements in temp array

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

           }

       }

                   //printing the elements of set A that are not flagged in temp

       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;

                   //stroing distinct elements (not present in B) from A in aUb array

       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];

           }

       }

                   //stroing distinct elements (not present in A) from B in aUb array

       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 aUc[2000];

       int iaUc = 0;

                   //stroing distinct elements (not present in B) from A in aUb array

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

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

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

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

               aUc[iaUc++] = sets[0][i];

           }

       }

                   //stroing distinct elements (not present in A) from C in aUb array

       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] << " ";

               aUc[iaUc++] = sets[2][i];

           }

       }

                   //printing the common elementsfrom arrays aUb and aUc to get (AuB)n(AuC)

       bool temp[256] = { 0 };

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

           temp[aUb[i]] = 1;

       }

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

           if (temp[aUc[i]]) {

               ofs << aUc[i] << " ";

           }

       }

   }

   ofs.close();

   return 0;

}

Output and input files:


Related Solutions

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 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 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 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.
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT