Question in c++ Declare two arrays to store two binary numbers with maximum capa
ID: 3664983 • Letter: Q
Question
Question in c++
Declare two arrays to store two binary numbers with maximum capacity of 10 bits. Assign the values of ”0000000010” and ”0000000011” to the arrays. Write a computer program that sum these two binary numbers using the arrays. The result should be ”0000000101”.
Write a computer program that takes two binary numbers from the user as input and computes the ”addition” operation.
Sample input and output:
101 + 11 = 1000
111 + 111 = 1110
1010 + 1010 = 10100
11101 + 1010 = 100111
11111 + 11111 = 111110
Part 2
1. Declare two arrays to store set 1 and set 2, both with maximum capacity of 20 elements.
Assign the values of {”A”,”B”,”C”,”D”} and {”C”,”D”,”E”} to the arrays. Write a computer program to compute the union of the sets. The result should be {”A”,”B”,”C”,”D”,”E”}.
2. Implement an algorithm to compute the intersection of the sets. The result should be {”C”,”D”}
3. Solve the following exercises using your program. Let A = {0, 1, 2, 3, 4, 5, 6, 7}, B = {0, 2, 4, 6, 8, 10, 12}, and C = {0, 3, 5, 6, 9}.
Find
(a) (A B) C.
(b) (A B) C.
(c) A (B C) and (A B) (A C). Are the results equal?
(d) A' B'
(e) (A B)' B' 1
HINTS
• You may want to use three arrays of integers.
• 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 defined already. For example, to compute (A B) C: First, compute A B using the function that computes the union that you already defined. Lets 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
Q1 UNION
char set1[20],set2[20];
set1={"a","b","c","d"};
set2={"c","d","e"};
void union(char set1[],chjar set2[])
{ int i = 0, j = 0;
while (i < set1.length && j < set2.length)
{
if (set1[i] < set2[j])
cout<< set1[i++];
else if (set2[j] < set1[i])
cout<<set2[j++];
else
{
cout<< set2[j++];
i++;
}
}
//Now the remaining elements of the array
while(i < m)
cout<<set1[i++];
while(j < n)
cout<< set2[j++];
}
}
Q2 INTERSECTION
char set1[20],set2[20];
set1={"a","b","c","d"};
set2={"c","d","e"};
void intersection(char set1[],char set2[])
{ int i = 0, j = 0;
while (i < set1.length && j < set2.length)
{
if (set1[i] < set2[j])
i++;
else if (set2[j] < set1[i])
j++;
else
{
cout<<set2[j++];
i++;
} }
}
}
Q3. For third question, lets re-use the Q1 and Q2.
a) (A U B)U C
So, for this we shall call- union(A,B) and put it in X, and then call union(X,C)
Similarly, we can continue doing the same for the rest of the paths.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.