I'm a beginner to C and programming in general, so I have a question concerning an issue with my code. I want to be able to add binary numbers (represented as strings) taken from the command line. Currently, if I add the strings '1010' to '1101', my program returns '0111' which is wrong obviously, it should return '10111'. My issue is when sum[0]='0' and carry='1'. When this is the case, sum[0] should become '1' and then all the other elements should change. Any insight on this? Thanks.

#include <stdio.h>
#include <string.h>

#define bool  int
#define false 0
#define true  1

#define MAX_DIGITS 24       /* Maximum digits in (output) 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                     */
	int counter;                       /* Loop counter                      */

	/* Exit if insufficient arguments were supplied. */
	if (argc < 3)
	{
		printf("Error: insufficient arguments.\n");
		return 1;
	}

	/* Add together the first two binary numbers on the command-line. */
	noError = Badd( argv[1], argv[2], sum );
		
	/* Add any additional binary numbers to the partial sum. */
	for (counter = 3; noError && counter < argc; counter++)
	{
		strcpy( partialSum, sum );
		noError = Badd( partialSum, argv[counter], sum );
	}

	/* Print answer on standard output. */
	if (noError)
		printf("%s\n", sum);
	else
		printf("E\n");

	return 0;
}


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 the sum of augend + addend.         */
/* Returns true if successful in addition, false otherwise.               */
{
	int i;
	int maxLength = 0;
	char carry = '0';
	int auglen = strlen(augend);
	int addlen = strlen(addend);
	
	
	if (auglen >= addlen)
		maxLength = auglen;
	else
		maxLength = addlen;
	
	
	for (i=maxLength; i >= 0; i--)
	{
		if (augend[i]=='1' && addend[i]=='0' && carry=='0')
		{
			sum[i]='1';
			carry='0';
		}
		else if (augend[i]=='0' && addend[i]=='1' && carry=='0')
		{
			sum[i]='1';
			carry='0';
		}
		else if (augend[i]=='1' && addend[i]=='1' && carry=='0')
		{
			sum[i]='0';
			carry='1';
		}
		else if (augend[i]=='0' && addend[i]=='0' && carry=='0')
		{
			sum[i]='0';
			carry='0';
		}
		else if (augend[i]=='1' && addend[i]=='0' && carry=='1')
		{
			sum[i]='0';
			carry='1';
		}
		else if (augend[i]=='0' && addend[i]=='1' && carry=='1')
		{
			sum[i]='0';
			carry='1';
		}
		else if (augend[i]=='1' && addend[i]=='1' && carry=='1')
		{
			sum[i]='1';
			carry='1';
		}
		else if (augend[i]=='0' && addend[i]=='0' && carry=='1')
		{
			sum[i]='1';
			carry='0';
		}
	}
	
	return true;
}

Recommended Answers

All 4 Replies

You have a carry you're not taking care of after you parse everything. Try putting in leading zeros and see what happens (it should work). However, that's not how you want to do it. You'd need to insert the carry bit in the front and push the rest of the string back.

My recommendation is to read in 2 binary streams, convert them to integers, add them, then write them back out again as binary.

Happy coding!

I'm wanting to keep them as strings. I don't want to convert them to integers or decimal. Ahhh I'm stuck :(

Hm... I seem to have over-looked a few things.

First, you need to get both strings the same length.

Look at your code here.

int auglen = strlen(augend);
	int addlen = strlen(addend);
	
	
	if (auglen >= addlen)
		maxLength = auglen;
	else
		maxLength = addlen;

This won't work. You'll want to left-side pad the shorter one with zeros until their the same length. Then, after you do your addition, if the carry is still one, slide the 1 in on the side.
However, you'll need to do some messy string manipulation to do this. You'd need to do a string copy that starts at the end and works toward the beginning. Then insert the '1'. Memmove should do that for you. But then again, ONLY if the final string will fit in the size limits.

Of course, I'm dancing around the elephant in the room. That is, you could use a double ended queue and solve a lot of your problems. But that just creates more problems if you've never written one before.

So 'add' the values rather than test the values.

If you add '1' (31h) and '0' (30h) together you get 'a' (61h). Subtract out the extra 30h and you have your character:

'0' = 30h
'1' = 31h
'2' = 32h
therefore:
'0'+'0' - 30h = '0'
'0'+'1' - 31h = '1'
'1'+'1' - 32h = '2'

The only IF you need is testing for '2' and convert that into '0' + carry Or you can add all 3 values and subtract the extra 60h. Then you have to deal with a possible '3', just like above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.