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

About the program: Compute the intersection and union of two sets A and B. TheUn

ID: 3617518 • Letter: A

Question

About the program:

Compute the intersection and union of two sets A and B. TheUniverse set has 128 elements numbered from 1 to 128. That is U ={1, 2, 3, 4,…, 128}. Your program must use NO more than 16memory locations to represent four sets A, B, C, and D. Theelements of each set are represented in bit format. An element in aset is marked by a 1. The missing elements are marked by a 0.For

example,

             unsigned a[0] = 0x80010001,

a[1] =0x80010001,

a[2] =0x80010001,

a[3] =0x80010001;

defines the elements of set A in bit-format. The elements arenumbered from left to right. For example, bit-pattern of a[0]indicates that elements 1, 16, and 32 are in set A, bit-pattern ofa[1] indicate that elements 33, 48, and 64 are also in set A, andso on.

Do the following:

1) Determine the elements of C = A B in bit-format.Hint: Use the bit-wise AND operator (&). Print set C inbit-format.

( | ). Print set D in bit-format.

3) Convert the bit-format of sets C and D to the list of actualelements. Specifically, write a function and call it twice oncepassing set C and second time passing set D. This function convertsthe bit-pattern to a list of actual elements.

4) Test your program with the following:

a)

unsigned a[4] = {0x80010001, 0x80010001, 0x80010001,0x80010001};

unsigned b[4] = {0x80000001, 0x80000001, 0x80000001,0x80000001};

Sample output for converting a bit-format to itscorresponding list: Shown for set A for

test data a.

A = {

1,

16,

32,

33,

48,

64,

65,

80,

96,

97,

112,

128,

}

I have posted below what I got so far, I need help with takingbit format values as input and then outputting the values asinteger (actual elements). The program as of right now worksif you enter int has values and it coverts it to bit-format andoutputs it. But I need it other way round. I have notprogrammed in C++ before , so I have hard time doingthis.

This is what I need it able to do:

Input should be in hex for example if I enter following

Enter Value in Hex for A: 80010001

Enter Value in Hex for B: 80000001

C: 80000001 (Intersection of A & B)

C: 1,32 (corresponding element-list

D: 80010001 (Union of A & B )

D: 1,16,32 ((corresponding element-list)

output for converting a bit-format to itscorresponding

A: 1,16,32

B: 1,32


=================================================

Code:


#include <iostream>
#include<math.h>
#include <string>
#include <sstream>
using namespace std;
void input(unsigned int[]);
void print(unsigned int[],string);
void unyon(unsigned int[],unsigned int[],unsigned int[]);
void intersect(unsigned int[],unsigned int[],unsigned int[]);

int main()
{unsigned int a[4]={0},b[4]={0},c[4]={0},d[4]={0};
int i,j;
input(a);
//print (a,"a");
input(b);
//print(b,"b");
unyon(a,b,c);
intersect(a,b,d);
print (a,"a");
print(b,"b");
print(c,"c");
print(d,"d");

system("pause");
return 0;
}

void input (unsigned int a[] )
{long i,word,bit;

cout<<"Enter the bits that are set a negative number toexit: ";
cout<<"number should be between 1 and 128 ";
cout<<"enter bit: ";
cin>>i;


while(i>0)
  
   {word=(i-1)/32;
   bit=((i-1)%32);
   a[word]+= (pow(2.0,32-bit-1));
   //cout<<hex<<a[word]<<""<<word<<endl;
   cout<<"enter bit: ";
cin>>i;
   }
}

void print(unsigned int a[],string mess)
{cout<<"array "<<mess<<": ";
for(int i=0;i<4;i++)
    cout<<hex<<a[i]<<" ";
cout<<endl;
}

void unyon(unsigned int a[],unsigned int b[],unsigned int c[])
{int i;
unsigned int mask=0x80000000,t1,t2;
for(i=0;i<4;i++)
   for(mask=0x80000000;mask>0;mask/=2)
        { t1=a[i]&mask;
         t2=b[i]&mask;
         c[i]+=t1|t2;
          }
}

void intersect(unsigned int a[],unsigned int b[],unsigned intc[])
{int i;
unsigned int mask=0x80000000,t1,t2;
for(i=0;i<4;i++)
   for(mask=0x80000000;mask>0;mask/=2)
        { t1=a[i]&mask;
         t2=b[i]&mask;
         c[i]+=t1&t2;
          }
}

Explanation / Answer

please rate - thanks #include #include #include #include using namespace std; void input(unsigned int[]); void print(unsigned int[],string); void unyon(unsigned int[],unsigned int[],unsigned int[]); void intersect(unsigned int[],unsigned int[],unsigned int[]); int main() {unsigned int a[4]={0},b[4]={0},c[4]={0},d[4]={0}; int i,j; cout