Answer to Question #182611 in C++ for ZAIN

Question #182611

Write a program Given two arrays A[n] & B[n] containing bit strings representing sets A & B respectively, write a code fragment to construct bit string representing the set

𝐴 − 𝐵̅.

 


1
Expert's answer
2021-04-18T05:31:45-0400
#include <iostream>
#include <string>
using namespace std;
int main()
{
    std::string a;
    std::string b;
    cout << "Enter first bit string A : ";
    cin >> a;
    int n = a.length();
    std::cout << "Enter the second bit string B of length " << n<<" ";
    cin >> b;
    std::string result{ "" };
    int delta = 0;
    char mn;
   
    for (int i = n - 1; i >-1; i--) 
    {
        mn = a[i];
        if (delta == 1 && a[i] == '1') 
        {
            mn = '0';
            delta = 0;
        }
        if (delta == 1 && a[i] == '0') 
        {
            mn = '1';
            delta = 1;
        }
        
        if (mn =='0' && b[i]=='0') 
        {
            result = "0" + result;
        }
        if (mn == '1' && b[i] == '0')
        {
            result = "1" + result;
        }
        if (mn == '1' && b[i] == '1')
        {
            result = "0" + result;
        }
        if (mn == '0'&& b[i] == '1')
        {
            result = "1" + result;
            delta = 1;
        }
       
     }
    if (delta == 1) 
    {
        cout << "Negative binary number in two's complement format ";
    }
    cout << "A-B = "<<result<<endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS