beginner c++ programmer

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 17
Reputation: buddy1 is an unknown quantity at this point 
Solved Threads: 0
buddy1 buddy1 is offline Offline
Newbie Poster

beginner c++ programmer

 
0
  #1
Mar 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,839
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: beginner c++ programmer

 
0
  #2
Mar 20th, 2008
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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: buddy1 is an unknown quantity at this point 
Solved Threads: 0
buddy1 buddy1 is offline Offline
Newbie Poster

Re: beginner c++ programmer

 
0
  #3
Mar 20th, 2008
Originally Posted by Narue View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: buddy1 is an unknown quantity at this point 
Solved Threads: 0
buddy1 buddy1 is offline Offline
Newbie Poster

Re: beginner c++ programmer

 
0
  #4
May 7th, 2008
Originally Posted by Narue View Post
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[i] = a[i] + b[i] + carry[i];
}

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

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

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

I'm having some real trouble with this problem. Please help.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: beginner c++ programmer

 
1
  #5
May 8th, 2008
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[i] = a[i] + b[i] + carry[i];

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[i], carry, a[i], b[i]);

and change the definition accordingly.

5) Move this line:

result[i] = a[i] + b[i] + carry[i];

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:
  1. if(r == 0)
  2. result = 0
  3. carry = 0
  4. else if(r == 1)
  5. result = 1
  6. carry = 0
  7. else if(r == 2)
  8. result = 0
  9. carry = 1
  10. else
  11. result = 1
  12. carry = 1
  13.  
  14. or maybe one of the variants below:
  15.  
  16. if(r % 2 == 0)
  17. result[i] = 0
  18. else
  19. result[i] = 1
  20.  
  21. if(r < 2)
  22. carry = 0;
  23. else
  24. carry = 1;
  25.  
  26. or:
  27.  
  28. r % 2 == 1 ? result[i] = 1 : result[i] = 0;
  29. 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.
Last edited by Lerner; May 8th, 2008 at 1:06 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 14
Reputation: scarface3288 is an unknown quantity at this point 
Solved Threads: 1
scarface3288 scarface3288 is offline Offline
Newbie Poster

Re: beginner c++ programmer

 
0
  #6
May 8th, 2008
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
Last edited by scarface3288; May 8th, 2008 at 1:22 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: buddy1 is an unknown quantity at this point 
Solved Threads: 0
buddy1 buddy1 is offline Offline
Newbie Poster

Re: beginner c++ programmer

 
0
  #7
May 19th, 2008
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;
       }
}
Last edited by buddy1; May 19th, 2008 at 10:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: buddy1 is an unknown quantity at this point 
Solved Threads: 0
buddy1 buddy1 is offline Offline
Newbie Poster

Re: beginner c++ programmer

 
0
  #8
May 19th, 2008
0
Last edited by buddy1; May 19th, 2008 at 9:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: beginner c++ programmer

 
0
  #9
May 20th, 2008
Hi buddy1,

here is some code what might meet your assignment.
  1. In your main() you write:
  2.  
  3. // Binary addition
  4. const int a[8] = { 0, 1, 1, 1, 1, 1, 1, 1 };
  5. const int b[8] = { 0, 0, 1, 1, 1, 1, 1, 1 };
  6. int nb = 8, carry = 0, sum[8];
  7.  
  8. binaryAddint(nb, carry, sum, a, b);
  9.  
  10. cout << "\nOperand a: "; for (int i = 0; i < nb; cout << a[i++]);
  11. cout << "\nOperand b: "; for (int i = 0; i < nb; cout << b[i++]);
  12. cout << "\na plus b: "; for (int i = 0; i < nb; cout << sum[i++]);
  13. cout << "\nLast carray: " << carry << endl;
  14.  
  15. /* Result:
  16.   Operand a: 01111111
  17.   Operand b: 00111111
  18.   a plus b: 10111110
  19.   Last carray: 0
  20. */
  21.  
  22. Function to do the additions:
  23.  
  24. void binaryAddint( int nb, int& carry, int* sum, const int* bin1, const int* bin2 ){
  25. for ( int s, i = nb-1; i >= 0; i--){
  26. s = bin1[i] + bin2[i] + carry;
  27. sum[i] = s % 2; carry = s / 2;
  28. }
  29. }
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
Last edited by tesuji; May 20th, 2008 at 12:28 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: buddy1 is an unknown quantity at this point 
Solved Threads: 0
buddy1 buddy1 is offline Offline
Newbie Poster

Re: beginner c++ programmer

 
0
  #10
May 20th, 2008
I cannot change the function. Can anyone help me out.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC