Question
help will be highly appreciated!! please dont post the answer which is already here because thats a wrong answer.
Font Paragraph Styles Instructions: Write a C++ program that will make use of a function that will implement a Full Adder as
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Quick Launch (Ctrl+Q) - x deepthi.onganattu - D File Test Analyze - Window Help Local Windows Debugger E 1 . FullAdderlnCpp -Program

//Header files
#include <iostream>
#include<vector>
#include<string>
using namespace std;

//Function prototypes
bool checkValidInput(string);
vector<bool> changeToVector(string);
vector<bool> fullAdder(vector<bool>, vector<bool>);
void display(vector<bool>);

int main()
{
    //Variables to read binary input
   string binary1, binary2;
   char ch;
   //Loop until user prefer
   while (true)
   {
       //Prompt for first binary
       cout << "Enter first binary number: ";
       cin >> binary1;
       while (!checkValidInput(binary1)) {
           cout << "Invalid input!!!" << endl;
           cout << "Enter first binary number: ";
           cin >> binary1;
       }
       //Prompt for second binary
       cout << "Enter second binary number: ";
       cin >> binary2;
       while (!checkValidInput(binary2)) {
           cout << "Invalid input!!!" << endl;
           cout << "Enter second binary number: ";
           cin >> binary2;
       }
       //Create bool vector
       vector<bool> bin1 = changeToVector(binary1);
       vector<bool> bin2 = changeToVector(binary2);
       //Call functions to add
       vector<bool> result = fullAdder(bin1, bin2);
       //Display result
        cout << "Sum of two binary's (" << binary1 << "+" << binary2 << ") in boolean form :-" << endl;
       display(result);
       //Repetition part
       cout << "\nDo you want to enter another inputs of full adder: ";
       cin >> ch;
       if (ch != 'Y'&& ch != 'y') {
           break;
       }
   }
}
//Function to check whether the string is valid or not
bool checkValidInput(string str) {
   for (int i = 0; i < str.length(); i++) {
       if (str[i] != '1' && str[i] != '0') {
           return false;
       }
   }
   return true;
}
//Function to convert string to binary
vector<bool> changeToVector(string str) {
   vector<bool> bin;
   for (int i = 0; i < str.length(); i++) {
       if (str[i] == '1') {
           bin.push_back(true);
       }
       else {
           bin.push_back(false);
       }
   }
   return bin;
}
//Function to generate full adder function
vector<bool> fullAdder(vector<bool> bin1, vector<bool>bin2) {
   //String to store result
   string resultStr = "";
   //To get sum
   int sum = 0;
   //Get last index of vectors
   int i = bin1.size() - 1;
   int j = bin2.size() - 1;
   //Loop to check the end
   while (i >= 0 || j >= 0 || sum == 1)
   {
       //First vector value add
       if (i>=0 && bin1.at(i)==true) {
           sum += 1;
       }
       else if(i>=0) {
           sum += 0;
       }
       //second vector value add
       if (j >= 0 &&bin2.at(j)==true) {
           sum += 1;
       }
       else if(j>=0) {
           sum += 0;
       }
        //result prepend in string
       resultStr = char(sum % 2 + '0') + resultStr;
       //if any carry finding
       sum /= 2;

       // Move to next digits
       i--; j--;
   }
   //Return vector bool
   return changeToVector(resultStr);
}
//Function to display result
void display(vector<bool> bin) {
   cout << '[';
   for (int i = 0; i < bin.size(); i++) {
       if(i < bin.size() - 1) {
           if (bin[i]) {
               cout << "True" << ",";
           }
           else {
               cout << "False" << ",";
           }
        }
       else {
           if (bin[i]) {
               cout << "True" << "]";
           }
           else {
               cout << "False" << "]";
           }
       }
   }
   cout << endl;
}

---------------------------------------------------------------------

Output

Enter first binary number: 1
Enter second binary number: 0
Sum of two binary's (1+0) in boolean form :-
[True]

Do you want to enter another inputs of full adder: y
Enter first binary number: 11
Enter second binary number: 0
Sum of two binary's (11+0) in boolean form :-
[True,True]

Do you want to enter another inputs of full adder: y
Enter first binary number: 1101
Enter second binary number: 100
Sum of two binary's (1101+100) in boolean form :-
[True,False,False,False,True]

Do you want to enter another inputs of full adder: y
Enter first binary number: 0
Enter second binary number: 1
Sum of two binary's (0+1) in boolean form :-
[True]

Do you want to enter another inputs of full adder: y
Enter first binary number: 00
Enter second binary number: 1
Sum of two binary's (00+1) in boolean form :-
[False,True]

Do you want to enter another inputs of full adder: n

Add a comment
Know the answer?
Add Answer to:
help will be highly appreciated!! please dont post the answer which is already here because thats...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT