C++ Binary adder problem

Discussion in 'Computer Science & Culture' started by Truenemo1889, Jul 3, 2006.

Thread Status:
Not open for further replies.
  1. Truenemo1889 Registered Senior Member

    Messages:
    158
    Hello Everyone,

    I am trying to add two binary numbers in the form of two arrays. The code that i have been using compiles, but it does not give me the results i would expect. The problem that i am having is that it does not carry ones over to the next significant digit if i have a 1 + 1 = 10 case. Could anyone help me by giving an explanation in the form of a correction if possible. I would appreciate any feedback. Thank You !

    Here is the code

    Please Register or Log in to view the hidden image!



    // Binary number manipulation

    #include <stdio.h>
    #include <cmath>
    #include <iomanip>
    //#include <b_array.h>

    main()
    {

    int array_value1[ 4 ] = { 0, 0, 1, 1 }; //4 element array

    int array_value2[ 4 ] = { 0, 1, 0, 1 }; //4 element array

    int array_value3[4];

    int i;


    for( i = 0; i <4; i++)
    // printf("d[%i] = %i \n", i, array_value1 );

    for( i= 0; i <4; i++)
    // printf("d[%i] = %i \n", i, array_value2);

    for (i=0; i <4; i++)
    {
    array_value3 = array_value1 + array_value2;

    if ( (array_value1== 1) && (array_value2== 1))
    {

    array_value3 = 0;
    array_value3[i+1] = 1 + array_value1[i+1] + array_value2[i+1];

    }

    }
    for( i= 0; i <4; ++i)
    printf("d[%i] = %i \n", i, array_value3);

    return(0);
    }
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Zephyr Humans are ONE Registered Senior Member

    Messages:
    3,371
    You're putting the carry digit into array_value3, where it gets clobbered as soon as you calculate the next digit. Try initialising array_value3 to 0's and change the '=' in

    array_value3 = array_value1 + array_value2;

    to '+='.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. przyk squishy Valued Senior Member

    Messages:
    3,203
    In addition to what Zephyr said, this bit could also use some work:
    Code:
    if ( (array_value1[i]== 1) && (array_value2[i]== 1))
    	{
    
    		array_value3[i] = 0;
    		array_value3[i+1] = 1 + array_value1[i+1] + array_value2[i+1];
    
    	}
    To make things simpler, you only need to test if array_value3 > 1. If it is, just subtract 2 from array_value3 and store 1 in array_value3[i+1] (since array_value1[i+1] and array_value2[i+1] get added in the next loop anyway). Also, you need to define array_value3 to have 5 elements, not 4.

    So the code could look like this:
    Code:
    if (array_value3[i]>1)
    {
    	array_value3[i] -= 2;
    	array_value3[i+1] = 1;
    }
    PS: Use the VB
    Code:
     tag - it makes code easier to read and preserves your indents.
     
    Last edited: Jul 3, 2006
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Truenemo1889 Registered Senior Member

    Messages:
    158
    Thanks for the help everyone. I was able to figure it through the help i got from a lot of ppl. Thank you very much !

    Please Register or Log in to view the hidden image!



    Working code:

    // 4 bit binary adder

    #include <stdio.h>
    #include <iomanip>


    main()
    {

    int array_value1[ 4 ] = { 1, 0, 0, 0 }; //4 element array
    int array_value2[ 4 ] = { 1, 1, 1, 0 }; //4 element array
    int array_value3[4];
    int overflow[5];

    int i;

    for (i=0; i<4; i++) overflow = 0;
    for (i=0; i <4; i++)
    {
    array_value3 = array_value1 + array_value2 + overflow;

    if ( array_value3 > 1)
    {
    overflow[i+1] = 1;
    array_value3 %= 2;
    }
    }

    for( i= 0; i <4; ++i) printf("d[%i] = %i \n", i, array_value3);

    return(0);
    }


    P.S. The program adds from left to right starting with the least sig. digit. I am sorry i couldn't put the code in the proper format.
     
  8. przyk squishy Valued Senior Member

    Messages:
    3,203
    You just type CODE tags at the beginning and end of your code. They're described in the FAQ here (you can also try the PHP tag - it adds syntax highlighting). If you add spaces after every "<", (eg. #include < stdio.h>), the include filenames won't disappear (otherwise they get mistaken for HTML tags - I don't know if there's another workaround for this).

    The only point I'd make about your code now is that you don't really need the overflow array - just initialize array_value3 as {0, 0, 0, 0, 0} and you can use it instead (it'll save a whopping sixteen bytes of memory).

    By the way, was this a bit of hobbyist programming, or are you doing a course, or what?

    ***

    Just for the fun of it (coz I have way too much spare time on my hands and you put me in a coding mood):
    Code:
    #include < stdio.h>
    #include < string.h>
    #include < math.h>
    
    int main(int argc, char** argv)
    {
    	unsigned int c, i, j, n;
    
    	for (i=n=0; i< argc-1; i++)
    		for (	j=strlen(argv[i+1]);
    			j>=1&&(c=argv[i+1][j-1]-'0')>=0&&c<=1; 
    			n+=c*pow(2,strlen(argv[i+1])-j--)	);
    
    	for (	i=n>0?1+floor(log(n)/log(2)):1;
    		i-->=1;
    		putchar('0'+((n-=((c=n)>=pow(2,i))?pow(2,i):0)!=c))	);
    
    	putchar('\n');
    	
    	return 0;
    }
     
    Last edited: Jul 4, 2006
  9. sepidehs84 Registered Member

    Messages:
    1
    Hi ;
    Is it possible to change this program, 4 bit adder, in the way that we get inputs from memory instead of having them in program ?

    I am wondering if anybody can help me
     
  10. przyk squishy Valued Senior Member

    Messages:
    3,203
    Probably, but how do you mean? Where do you want to get the input from "in memory"?

    (By the way: everyone ignore the "for fun" code in post #5. I typed that when I was (re)learning C four years ago and I got a little carried away exploring the language's flexibility

    Please Register or Log in to view the hidden image!

    )
     
  11. Chipz Banned Banned

    Messages:
    838
    If I did it, I would do this. I'm not sure of the code in C++, here's a pseudo code

    Code:
    
    Ara1 = [3,2,1,0] 
    Ara2 = [0,3,2,3] 
    Ara_final = [0,0,0,0]
    
    // Add sums
    for (int i = 0; i < len(Ara1); i++)  {
        Ara_final[i] = Ara1[i] + Ara2[i]
    }
    
    for (int i =len(Ara_final); i >= 0; i--)  {
        if (Ara_final[i] > 3)  {
            Ara_final[i] = 0
            Ara_final[i+1]++
    
            if (i = 0) {
                //Do something to shift all of the array right one and add another digit at the beginning.
            }
        }
    }
    
    
    Or if you wanted to do it easier, you could hold the values in reverse order, cycle through them in reverse, and change the Ara_final[i+1]++ to Ara_final[i-1] And if the i=0 then you could run an append function.
     
Thread Status:
Not open for further replies.

Share This Page