Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to write a program in C++ that will allow me to add two or more binary numbers from the command line. My

I am trying to write a program in C++ that will allow me to add two or more binary numbers from the command line. My problem is that when I execute it gives me a strange output

00

01

on the same line before terminating. Code attached. Explanation needed as to why the code is wrong. Bold numerals are the output I am receiving.

#include #include using namespace std;

const int MAX_DIGITS = 36; //maximum number of digits allowed in sum

bool Badd(const char augend[], const char addend[], char sum[]); // IN IN OUT

int main(int argc, char *argv[]) // IN IN { char partialSum[MAX_DIGITS + 1]; //partial sum of the binary numbers char sum[MAX_DIGITS + 1]; //sum of the binary numbers bool noError; //no error flag //Exit if insufficient arguments passed to function if (argc < 3) { cout << "Insufficient arguments passed" << endl; return 1; } //Add the first two binary numbers on the command line. noError = Badd(argv[1], argv[2], sum); //Add additional binary numbers to the partial sum for (int counter = 3; noError && counter < argc; counter++) { strcpy(partialSum, sum); noError = Badd(partialSum, argv[counter], sum); } //Print the answer to standard output if (noError) { for (int counter = 0; counter < strlen(sum); counter++) { cout << sum[counter]; } cout << endl; } else cout << 'E' << endl; return 0; }// end main function

bool Badd(const char augend[], const char addend[], char sum[]) // IN IN OUT //PRE: augend and addend are strings representing valid binary numbers //POST: sum is a string representing augend + addend //returns true if addition is successful and false if otherwise { int carry = 0; //Carry after addition int lengthDif; //Difference in string lengths //Length of the longer string int longStringLength; if (strcmp(augend, addend) == 0) { longStringLength = strlen(augend); } else if (strcmp(augend, addend) < 0) { longStringLength = strlen(addend); lengthDif = strlen(addend) - strlen(augend); } else if (strcmp(augend, addend) > 0) { longStringLength = strlen(augend); lengthDif = strlen(augend) - strlen(addend); } for (int counter = longStringLength; counter <= 0; counter--) //Addition loop { if (carry == '0' && augend[counter] == '0' && addend[counter] == '0') sum[counter] = '0'; else if (carry == '0' && augend[counter] == '1' && addend[counter] == '0') sum[counter] = '1'; else if (carry == '0' && augend[counter] == '0' && addend[counter] == '1') sum[counter] = '1'; else if (carry == '1' && augend[counter] == '0' && addend[counter] == '0') sum[counter] = '1'; else if (carry == '1' && augend[counter] == '0' && addend[counter] == '1') { sum[counter] = '0'; carry = '1'; } else if (carry == '1' && augend[counter] == '1' && addend[counter] == '0') { sum[counter] = '0'; carry = '1'; } else if (carry == '0' && augend[counter] == '1' && addend[counter] == '1') { sum[counter] = '0'; carry = '1'; } else if(carry == '1' && augend[counter] == '1' && addend[counter] == '1') { sum[counter] = '1'; carry = '1'; } } if (strlen(sum) > MAX_DIGITS) return false; else return true; } //end of Badd function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Master The Art Of Data Storytelling With Visualizations

Authors: Alexander N Donovan

1st Edition

B0CNMD9QRD, 979-8867864248

More Books

Students also viewed these Databases questions