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
  • Please answer this question in C++. Also, please write it with 'using namespace std' because the other ways some of you do it is confusing. Class Exercise Write a program that does the follow...

    Please answer this question in C++. Also, please write it with 'using namespace std' because the other ways some of you do it is confusing. Class Exercise Write a program that does the following: 1. Defines a class named Data that has the following members a. A vector of integers entitled numbers (Private) b. Methods (Functions) (Public): i. Display_Menu Prompts the user to choose one of the following 0. Quit 1. Input Numbers 2. Display Numbers 3. Search Numbers 4....

  • I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instruction...

    I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • Hi please help with c++ programming. This is my code. there's a syntax error. PLEASE FIX...

    Hi please help with c++ programming. This is my code. there's a syntax error. PLEASE FIX MY CODE instead of re-designing the program. Thanks /*    Description of problem: Introduction to Friends functions. Create objects in the stack (not on the heap) 3.1: Add a friend function, kilotopound, which will convert kilograms to pounds. 3.2: Add a copy constructor utilizing Lab 3.1 code. Copy the fist object using a copy constructor. Output the contents of both objects. */ #include <iostream>...

  • Please post screenshots only File Tools View CMSC 140 Common Projects Spring 2019(1) E - 3...

    Please post screenshots only File Tools View CMSC 140 Common Projects Spring 2019(1) E - 3 x Project Description The Powerball game consists of 5 white balls and a red Powerball. The white balls represent a number in the range of 1 to 69. The red Powerball represents a number in the range of 1 to 26. To play the game, you need to pick a number for each ball in the range mentioned earlier. The prize of winning the...

  • okay so here is my c++ code and the errors im really stuck on fixing what...

    okay so here is my c++ code and the errors im really stuck on fixing what i did wrong it seems to be the same repeated error our job is to write a menu driven program that can convert to display Morse Code ere is the menu the program should display Menu Alphabet Initials N-Numbers - Punctuations S = User Sentence Q- Quit Enter command the user chooses A your program should use a loop and your morse code printing...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

  • Overview: You will be writing classes that implement a playlist simulation for a music streaming app.The...

    Overview: You will be writing classes that implement a playlist simulation for a music streaming app.The Playlist class will contain a dynamic array of Song objects, and the Song class contains several pieces of information about a song. You will need to: 1) Finish the writing of two classes: Song and Playlist. The full header file for the Song class has been provided in a file called Song.h. 2) Write a main program (filename menu.cpp) that creates a single Playlist...

  • I need help as quick as possible, thanks beforehand. Please provide the test output The Lo...

    I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...

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