I have this program I have to write about adding binary numbers. Here is the write up:

"Defining a binary number as int binNum[8]; write a C++ function
void binaryAdd(int* sum, int& cBit, const int* bin1, const int* bin2)
to compute sum as the sum of the two binary numbers, bin1 and bin2. cBit should be the value of the carry bit after the addition. sum should be the result in binary. For example, if the two binary numbers are 01111111 and 00111111, then sum will be 10111110 and carry will be 0. Test your function with interactive input."

The function cannot be changed. I'm having a hard time with this problem.

What have you tried so far? This is actually a simple enough project because you can easily figure out the steps on paper and then translate those steps directly to C++. So your first order of business should be to work out the exact steps of adding a binary number on paper.

What have you tried so far? This is actually a simple enough project because you can easily figure out the steps on paper and then translate those steps directly to C++. So your first order of business should be to work out the exact steps of adding a binary number on paper.

I'll take your advise and get back to you. Thanks for the reply.

What have you tried so far? This is actually a simple enough project because you can easily figure out the steps on paper and then translate those steps directly to C++. So your first order of business should be to work out the exact steps of adding a binary number on paper.

#include <iostream>


using namespace std;


void binaryAddint* result, int&cBit, const int* bin1, const int* bin2 );


int main()
{
int result[8];
int carry[9];
const int a[8] = { 0, 1, 1, 1, 1, 1, 1, 1 };
const int b[8] = { 0, 0, 1, 1, 1, 1, 1, 1 };
int i = 0;



for (i=0; i<8; i++ );
{
result = a + b + carry;
}


cout << "The addition of the two binary numbers is: " << result << endl;
cout << "The carry bit is: " << carry << endl;
}


void binaryAdd( int* result, int carry, int a, int b )
{
int i = 0;


if ( carry == 1 && a == 1 && b == 1 )
{
result = 1;
carry = 1;
}
if ( carry == 0 && a == 1 && b == 1 )
{
result = 0;
carry = 1;
}
if ( carry == 0 && a == 0 && b == 0 )
{
result =  1;
carry = 0;
}
if ( carry == 1 && a == 0 && b == 1 )
{
result = 0;
carry = 1;
}
if ( carry == 0 && a == 1 && b == 0 )
{
result = 1;
carry = 0;
}
if ( carry == 1 && a == 1 && b == 0 )
{
result = 0;
carry = 1;
}
if ( carry == 0 && a == 0 && b == 0 )
{
result = 0;
carry = 0;
}
if ( carry == 1 && a == 0 && b == 0 )
{
result = 1;
carry = 0;
}
}

I'm having some real trouble with this problem. Please help.

Edited 3 Years Ago by happygeek: fixed formatting

1) void binaryAddint* result, int&cBit, const int* bin1, const int* bin2 );

You are missing a ( in there somewhere, probably between Add and int.

2) define carry to be an int, not an int array, and initialize it to zero

3) You are defining binaryAdd() within main() rather than calling binaryAdd() within main. It should be defined outside of main(). If it is defined after main() then there must be a prototype before main() and the return type, function name and the list of parameters must be the same in both the prototype and definition. The list of parameters must be equal in number, type and appear in the same sequence in both the prototype and the definition.

You should call binaryAdd() from within main(), using the function call in place of this line:

result = a + b + carry;

4) Maybe you'll want to change the prototype of binaryAdd() to:

void binaryAdd(int & result, int & carry, const int bin1, const int bin2 );

make the function call like this:

binaryAdd(result, carry, a, b);

and change the definition accordingly.

5) Move this line:

result = a + b + carry;

to be the first line of the body of binaryAdd()s definition, except maybe do it like this

int r = bin1 + bin2 + carry;

to match up with the new definition variables. Then you could do something like you did in binaryAdd() already:

if(r == 0)
 result = 0
 carry = 0
else if(r == 1)
 result = 1
 carry = 0
else if(r == 2)
 result = 0
 carry = 1
else
 result = 1
 carry = 1

or maybe one of the variants below:

if(r % 2 == 0)
  result[i] = 0
else 
  result[i] = 1

if(r < 2)
  carry = 0;
else 
   carry = 1;

or:

r % 2 == 1 ? result[i] = 1 : result[i] = 0;
r < 2 ? carry = 0 : carry = 1;

5) you have to decide whether the least significant digit of the binary number has index zero or whether it has the largest index used in the array (in this case it would be index 7 for both addends, though that need not be the case for all binary additions).

6) You will need to decide what to do if one binary number contains more digits than another. There are several options available.

7) Maybe you'll realize that I haven't completely rewritten your program, compiled it, and ran it to see if my recommendations are all valid. They are suggestions that I believe will work.

Good luck.

BTW: when posting code with indentation in it you can keep your formatting by wrapping it in code tags that are described in the watermarks of the message box or in the announcements at the top of the board.

Comments
Good post

first think about the math behind it all

0+0=0 C=0
1+0=1 C=0
0+1=1 C=0
1+1=0 C=1

Your function is

void binaryAdd(int* sum, int& cBit, const int* bin1, const int* bin2)

sum=sum of two digits
cBit=Carry bit
bin1=binary number 1
bin2=binary number 2

to implement your funcion you need

to call binaryAdd(sum,cBit,bin1,bin2) in the main file

but first you must populate the data members like

int i,
cout<<"this program adds 8 digit binary numbers"<<endl;
for (i=0;i<8;i++)
{
      cout<<"Please enter "<<i<<" binary number digit of first binary number"<<endl;
      cin>>bin1[i]
}
for (i=0;i<8;i++)
{
      cout<<"Please enter "<<i<<" binary number digit of second binary number"<<endl;
      cin>>bin2[i]
}

thats all i'm adding to help

This is what I came up with. Still can't get it to run.

#include <iostream>

using namespace std;

void binaryAdd( int* sum, int&cBit, const int* bin1, const int* bin2 )

int result[8];
int carry = 0;

write_8bin(int* a)
{
       for( int i = 7; i>=0; --i ) 
	   {
               cout << a[i];
       }
}

int main()
{
       int i;
       int bin1[8];
       int bin2[8];

       cout << "This program adds two 8 digit binary numbers" << endl;

       for( i = 0; i<8; i++ )
       {
               cout << "Please enter "<< i <<" binary numbers" << endl;
               cin >> bin1[i];
       }
       for( i = 0; i<8; i++ )
       {
               cout <<
                       "Please enter "<< i <<" binary numbers" << endl;
               cin >> bin2[i];
       }
       binaryAdd( result, carry, bin1, bin2 );

       write_8bin( bin1 );
       cout << " + ";
       write_8bin( bin2 );
       cout << " = ";
       write_8bin( result );
       cout << endl << "carry:" << carry << endl;

       return 0;
}
void binaryAdd( int* sum, int&cBit, const int* bin1, const int* bin2 )
{
       cBit = 0;
       int r;

       for ( int i = 0; i < 8; i++ )
       {
               r = bin1[i] + bin2[i] + cBit;
               if ( r % 2 == 0 ) sum[i] = 0;
               else sum[i] = 1;

               if ( r < 2 )
                       cBit = 0;

               else
                       cBit = 1;
       }
}

Hi buddy1,

here is some code what might meet your assignment.

In your main() you write:

// Binary addition
const int a[8] = { 0, 1, 1, 1, 1, 1, 1, 1 };
const int b[8] = { 0, 0, 1, 1, 1, 1, 1, 1 };
int nb = 8, carry = 0, sum[8];

binaryAddint(nb, carry, sum, a, b);

cout << "\nOperand a:   "; for (int i = 0; i < nb; cout << a[i++]);
cout << "\nOperand b:   "; for (int i = 0; i < nb; cout << b[i++]);
cout << "\na plus b:    "; for (int i = 0; i < nb; cout << sum[i++]);
cout << "\nLast carray: " << carry << endl;

/* Result:
   Operand a:   01111111
   Operand b:   00111111
   a plus b:    10111110
   Last carray: 0
*/

Function to do the additions:

void binaryAddint( int nb, int& carry, int* sum, const int* bin1, const int* bin2 ){
  for ( int s, i = nb-1; i >= 0; i--){
    s = bin1[i] + bin2[i] + carry;
    sum[i] = s % 2; carry = s / 2;
  }
}

First, both binary figures and the carry will be added. That gives the sum s. sum s remainder 2 gives the new figure (e.g. 1 + 1 + 1 = 3 % 2 = 1). There is only a new carry if the sum is 2 or 3. So dividing the sum s by 2 gives carry (e.g. 0 + 1 + 1 = 2 / 2 = 1).

I hope this clarifies the problem.

krs,
tesu

I cannot change the function. Can anyone help me out.

I cannot change the function. Can anyone help me out.

Which function can't you change? What can you change? What input did you give the program? What should the output be? What is the actual output? You need to be more specific.

The c++ function void binaryAdd(int* sum, int& cBit, const int* bin1, const int* bin2 ) cannot be altered. I would be thankful for any help solving this problem.

The c++ function void binaryAdd(int* sum, int& cBit, const int* bin1, const int* bin2 ) cannot be altered. I would be thankful for any help solving this problem.

You haven't described what the problem is. You've said you can't get it to run. See my former post for pertinent questions. Also, the prompts you use to ask for user input are not very descriptive. If you mean "Enter the ith binary digit", you should say that rather than "Enter i binary numbers". You aren't asking for numbers, you are asking for a single number, or rather a digit. Also, specify whether you want the user to enter the high order digits first or the low order bits first. You can't expect good output if the user doesn't know what to enter and in what order.

The c++ function void binaryAdd(int* sum, int& cBit, const int* bin1, const int* bin2 ) cannot be altered. I would be thankful for any help solving this problem.

I am not that sure whether your question deals with my post #9, does it? Are there problems like how-to-change formal parameters of the function declaration or don't fit your actual parameters when calling this function? Are there any error messages?

This is what I came up with. Still can't get it to run.

. . .
void binaryAdd( int* sum, int&cBit, const int* bin1, const int* bin2 )
{
       cBit = 0;
       int r;

       for ( int i = 0; i < 8; i++ )
       {
               r = bin1[i] + bin2[i] + cBit;
               if ( r % 2 == 0 ) sum[i] = 0;
               else sum[i] = 1;

               if ( r < 2 )
                       cBit = 0;

               else
                       cBit = 1;
       }
}

Possibly, you don't know what you should change in your function to get it function correctly. These are your problems in binaryAdd of posting #7:

1. Serious problem (where you begin when you are doing addition manually, left or right side?):
for ( int i = 0; i < 8; i++ ) --> for ( int i = 7; i >=0; i-- ) // see posting #9

2. better, for allowing continuous additions:
cBit = 0 --> should be set in main()

3. more beautiful:
if ( r % 2 == 0 ) sum = 0;
else sum = 1; --> sum = r % 2;


krs,
tesu

Comments
Very helpful

Thank for your help. You have been the most helpful person thus far. I will mark as solved

Kevin

This question has already been answered. Start a new discussion instead.