It might make things a little cleaner to replace this:
Quote:
quote:
if (bin_one[count] == '0')
bin_one[count] = 0;
else
bin_one[count] = 1;
if (bin_two[count] == '0')
bin_two[count] = 0;
else
bin_two[count] = 1;
|
to:
binnum_one[count] = bin_one[count] - '0';
binnum_two[count] = bin_two[count] - '0';
This works because '0' and '1' are ascii characters who's (decimal) integer values are 48 and 49. So if you want to convert the character '0' to 0, you can use integer arithmetic. Just subtract '0'. This translates to 48 - 48, which of course is equal to zero. Similar for '1'. It translates to 49 - 48, which equals 1.
Cleans up all the if statements.
Next, let's look at your logic. You should notice that your "result" is zero for even sums (0 and 2), and one for odd sums (1 and 3). You should also notice that your carry bit is only set when your sum is greater than 1 (2 and 3).
You can rewrite your entire if/else chain in two lines:
result[count] = temp % 2; // even sums get 0, odd sums get 1.
carry = (temp > 1)? 1 : 0; // carry if sum > 1.
My other problem is with your function declaration. Why pass in the "result" variable at all? You never use it, not really. You pass in this array of SIZE integers to the function, set one index of that array per loop iteration, then output the item you just set in the array.
This also means you've got one major flaw: You process your addition right-to-left, but you output the current result in the loop, so that's left-to-right. This is why your output is always backwards.
I suggest changing the "result" variable from a parameter to a local variable, and making it a char array to make it easier to output. We perform a simple int-to-char conversion by adding back that '0' offset.
Here's a working version of your app. Let me know if it doesn't make sense.
#include <iostream>
#define SIZE 8
using namespace std;
void oneComplement(int binary_one[], int binary_two[])
{
char result[SIZE + 1];
int temp = 0;
int count = 0;
int carry = 0;
result[SIZE] = '\0';
for (count = SIZE - 1; count >= 0; count--)
{
temp = binary_one[count] + binary_two[count] + carry;
result[count] = (temp % 2) + '0'; // even sums get 0, odds get 1.
carry = (temp > 1)? 1 : 0; // carry if sum > 1.
} // end for
cout << result;
} // oneComplement()
int main()
{
char bin_one[SIZE + 1]; // +1 to hold NULL character
char bin_two[SIZE + 1]; // +1 to hold NULL character
int binnum_one[SIZE];
int binnum_two[SIZE];
int count;
cout << "Enter numbers to add> \n";
cin >> bin_one;
cin >> bin_two;
for (count = 0; count < SIZE; count++)
{
binnum_one[count] = bin_one[count] - '0';
binnum_two[count] = bin_two[count] - '0';
}
oneComplement(binnum_one, binnum_two);
cout << "\n";
return 0;
}
I should add that your question sounds a lot like a homework assignment. If this is the case, you should take care that you're not violating any of your school's or teacher's rules about where you can get help and/or answers to homework problems.
I don't like answering other people's homework problems...
Take care,
Nik
http://www.bigaction.org/