Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Please read the entire question and answer all parts!! Read the \'HINTS\' gi

ID: 3787646 • Letter: C

Question

C++

Please read the entire question and answer all parts!! Read the 'HINTS' given at the end to help answer.

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' and second line is the set 'B'. Every element of each set is a character. 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. Write a title before the output of each operation.
1. (A B) C.
2. A (B C).
3. (A B) (A C).

HINTS:
Pay special attention to the parenthesis 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 dened 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.

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;
//For the length of A, B and C
int al, bl, cl;
//To read data from file
void Read(char a[], char b[], char c[])
{
//Declares an object of ifstream for reading
ifstream rfile;
//Opens the file input.txt in read mode
rfile.open("input.txt");
//Initializes the length to 0
al = bl = cl = 0;
char ch;
//Do while '.' is encountered which is the indicator for the end of array A
do
{
//Reads a character
rfile>>ch;
//If the character is '.' then stop
if(ch == '.')
break;
else
//Otherwise store the character in the array A and increase the counter
a[al++] = ch;
}while(!rfile.eof());
//Stores null at the end of the array
a[al] = '';
//Do while '.' is encountered which is the indicator for the end of array B
do
{
//Reads a character
rfile>>ch;
//If the character is '.' then stop
if(ch == '.')
break;
else
//Otherwise store the character in the array B and increase the counter
b[bl++] = ch;
}while(!rfile.eof());
//Stores null at the end of the array
b[bl] = '';
//Do while '.' is encountered which is the indicator for the end of array C
do
{
//Reads a character
rfile>>ch;
//If the character is '.' then stop
if(ch == '.')
break;
else
//Otherwise store the character in the array C and increase the counter
c[cl++] = ch;
}while(!rfile.eof());
//Stores null at the end of the array
c[cl]= '';
//Closer the file
rfile.close();
}
//Union operation
void Union(char a[], char b[], char r[])
{
//Initializes the flag to zero
int f = 0;
//Initializes the result length counter to zero
int rl = 0;
//Loops till end of the array A and stores it in the Result array
for(int d = 0; a[d] != ''; d++)
r[rl++] = a[d];
//Stores null at the end of result array
r[rl] = '';
//Loops till end of array B
for(int d = 0; b[d] != ''; d++)
{
//Loops till end of the array Result
for(int e = 0; r[e] != ''; e++)
{
//Compares each element of array B with the each elements of array Result
if(b[d] == r[e])
{
//If match found set the flag to 1 and come out of the loop
f = 1;
break;
}
}
//If flag is zero then store the character in array B in the array Result and increase the counter
if(f == 0)
r[rl++] = b[d];
else
//Otherwise reset the flag to zero
f = 0;
}
//Store null in the array result
r[rl] = '';
}
//Intersection operation
void Intersection(char a[], char b[], char r[])
{
//Initializes the flag to zero
int f = 0;
//Initializes the result length counter to zero
int rl = 0;
//Loops till end of array A
for(int d = 0; a[d] != ''; d++)
{
//Loops till end of array B
for(int e = 0; b[e] != ''; e++)
{
//Compares each element of array A with each element of array B
if(a[d] == b[e])
//If match fount store the character in the array Result and increase the counter
r[rl++] = a[d];
}
}
//Store null at the end of array Result
r[rl] = '';
}

int main()
{
//Creates an object of ofstream for writing
ofstream wfile;
//Opens the file output.txt in write mode
wfile.open("output.txt");
//Declares array
char A[50], B[50], C[50], T[50], T1[50], R[50];
//Reads the data from file and store it in the array A, B, and C
Read(A, B, C);

Intersection(A, B, T);
Union(T, C, R);
//Writes the result into the file with message
wfile<<"(A Intersection B) Union C"<<endl;
for(int d = 0; R[d] != ''; d++)
wfile<<R[d]<<" ";

Intersection(B, C, T);
Union(A, T, R);
//Writes the result into the file with message
wfile<<endl<<"A Union (B Intersection C)"<<endl;
for(int d = 0; R[d] != ''; d++)
wfile<<R[d]<<" ";

Intersection(A, B, T);
Intersection(A, C, T1);
Union(T, T1, R);
//Writes the result into the file with message
wfile<<endl<<"(A Intersection B) Union (A Intersection C)"<<endl;
for(int d = 0; R[d] != ''; d++)
wfile<<R[d]<<" ";
//Close the file
wfile.close();
return 0;
}

Input File (input.txt)

a b c d.
m a b n.
p a m n.

Output File (output.txt)

(A Intersection B) Union C
a b p m n
A Union (B Intersection C)
a b c d m n
(A Intersection B) Union (A Intersection C)
a b

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote