| | |
beginner c++ programmer
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 0
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.
"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.
New members chased away this month: 3
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
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:
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.
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:
C++ Syntax (Toggle Plain Text)
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.
Last edited by Lerner; May 8th, 2008 at 1:06 am.
•
•
Join Date: Apr 2007
Posts: 14
Reputation:
Solved Threads: 1
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
thats all i'm adding to help
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]
} Last edited by scarface3288; May 8th, 2008 at 1:22 am.
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
Hi buddy1,
here is some code what might meet your assignment.
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
here is some code what might meet your assignment.
c++ Syntax (Toggle Plain Text)
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; } }
I hope this clarifies the problem.
krs,
tesu
Last edited by tesuji; May 20th, 2008 at 12:28 am.
![]() |
Similar Threads
- declaration syntax error? (C++)
- Need help setting up graphics (C++)
- Beginner programmer (C++)
- Can't Get Going with eVC++4.0 (C++)
Other Threads in the C++ Forum
- Previous Thread: Elementary Class/const Question
- Next Thread: Compile error for RTTI
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






