c++ : binary addition

This is a discussion on c++ : binary addition within the C++ Programming forums, part of the General Programming Boards category; hi i am biggnner to programming so i don't know exactly how to add binary numbers (5bit) i tried of ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    16

    c++ : binary addition

    hi i am biggnner to programming
    so i don't know exactly how to add binary numbers (5bit)
    i tried of course

    Code:
    #include <iostream>
    using namespace std;
     
    int main ()
    {
    
    	int arr[5],arr1[5];
    		int carry=0,sum,sum[6],i;
    	cout<<"number one : ";
    	for ( i=0;i<5;i++)
    	{
    		cin>>arr[i];
    	}
    	cout<<"\n number two: ";
    	for ( i=0;i<5;i++)
    	{
    		cin>>arr1[i];
    	}
    	for ( i=0;i<5;i++)
    	{
    		if (arr[i]==0 && arr1[i]== 0 && carry== 0)
    			sum=0;
    		else if (arr[i] == 0 && arr1[i]==1 && carry== 0)
    			sum=1;
    		else if (arr[i]== 1 && arr1[i] == 0 && carry == 0)
    			sum=1;
    		else if (arr[i] == 1 && arr1[i] == 1 && carry ==0)
    		{
    			sum=0;
    			carry=1;
    		}
    		else if (arr[i] == 0 && arr1[i] == 0 && carry == 1)
    		{
    			sum=0;
    			carry=0;
    		}
    		else if (arr[i] ==0 && arr1[i] == 1 && carry ==1 )
    			sum=0;
    		else if (arr[i] == 1 && arr1[i] == 0 && carry == 1)
    			sum=0;
    		else if (arr[i] == 1 && arr1[i] == 1 && carry ==1)
    			sum=1;
    		else
    			cout<<" WRONG!";
    	}
    	sum[i]=sum;
    }
    
    
    for(i=0;i<6;i++)
    {
    	cout<<" sum = "<<sum[i]<<endl;
    }
        return 0;
    }
    what's wrong with it ? can you explain it
    and if there is another way to add binary numbers light me plzzz

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    this algorithm should work
    Code:
    0 + 0 = 0
    1 + 0 = 1
    0 + 1 = 1
    1 + 1 = 10
    1 + 1 + 1 = 11
    from http://www.allaboutcircuits.com/vol_4/chpt_2/2.
    Last edited by pYro; 05-19-2011 at 07:27 AM.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    16
    ok thanks i will try it

    but how can make sure that the input in binary ?
    what should i write

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,688
    The main 8 part of the "if/else if" have to set the correct value for "carry".

    I suggest working on adding without using the input code till you get adding to partly work.

    Code:
        int arr[5] = {1,0,0,0,0}; // Binary 00001
        int arr1[5] = {1,1,0,0,0}; // Binary 00011
    Code:
        cout<<" arr = "<<arr[4]<<arr[3]<<arr[2]<<arr[1]<<arr[0]<<endl;
        cout<<" arr1 = "<<arr1[4]<<arr1[3]<<arr1[2]<<arr1[1]<<arr1[0]<<endl;
    Code:
        cout<<" sum = "<<sum[5]<<sum[4]<<sum[3]<<sum[2]<<sum[1]<<sum[0]<<endl;
    FYI: You last "for" loop is NOT inside the main function; I suugest trying to get code to compile without errors before posting it.
    Or, post the errors and warnings if it has them.
    Last edited by stahta01; 05-19-2011 at 08:25 AM.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    16
    i have exam soon & i need to know how to do it with input

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    16
    plzz help me ((

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    16
    ok i changed my code to this
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
    	int arr[5],arr1[5],carry=0,sum[6],i;
    	cout<<"number 1. ";
    	{
    		cin>>arr[i];
    	}
    	cout<<"\n number 2. ";
    	for (i=0;i<5;i++)
    	{
    		cin>>arr1[i];
    	}
    	for (i=0;i<5;i++)
    	{
    		if (arr[i] + arr1[i] + carry == 0)
    			sum[i]=0;
    		else if (arr[i] + arr1[i] + carry == 1)
    			sum[i]=1;
    		else if ( arr[i] + arr1[i] + carry == 2)
    			sum[i]=0;
    	     	carry=1;
    		else if (arr[i] + arr1[i] + carry == 3)
    			sum[i] =1;
    		    carry=1;
    		else
    			cout<<"wrong!";
    	}
    	for (i=0;i<6;i++)
    cout<<"sum= "<<sum[i];
    	return 0;
    }
    errors
    Code:
    *******************\nn.cpp(24) : error C2181: illegal else without matching if
    *******************\nn.cpp(27) : error C2181: illegal else without matching if

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So there's one little issue and two big issues here. Little issue: in case 5 I don't think you want to claim that 0+1=0. Big issue #1: you do know you have to add from right to left? Big issue #2: If you don't have your user type spaces between the digits you'll get the whole number in arr[0] rather than one digit in each. If you don't want the user to type spaces, then you're going to have to read character by character.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    16
    in case 5 I don't think you want to claim that 0+1=0.
    ok how can i correct it ?

    Big issue #1
    yes i know

    Big issue #2
    i know but i wasn't sure how to seperate them and adding them
    by using % / ?

    like i said i am biggnner to the programming
    c++ is my first language

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Soul View Post
    ok how can i correct it ?
    I would suspect claiming that 0+1=1 instead would be a good start.

    Quote Originally Posted by Soul View Post
    i know but i wasn't sure how to seperate them and adding them
    by using % / ?
    Just as reasonable as anything else, I suppose.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    2,688
    Until you learn to indent your code, you SHOULD always use "{" and "}" in your if statements!!

    And, to repeat my prior comment; YOU MUST set carry to a valid value in each part of the if statement; not just the part where it is 1!!!!!!!!!!!!!!!!!!!!

    Edit: Maybe you will understand this; you only set carry to 1 inside the loop; how will it every change back to 0 inside the loop if you do not change it.

    Tim S.
    Last edited by stahta01; 05-19-2011 at 02:47 PM.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    16
    ok how about this :
    Code:
    int main()
    {
    	int arr[5],arr1[5],sum[5],carry[6]={0};
    int i ,c;
    
    cout<<"number 1.:"<<endl;
    for(i=0;i<5;i++)
    {
    cin>>arr[i];
    }
    cout<<"number 2.:"<<endl;
    for(i=0;i<5;i++)
    {
    cin>>arr1[i];
    }
    
    for(i=0;i<5;i++)
    {
        c=arr[i]+arr1[i]+carry[i];
        if(c==0)
       {
            sum[i]=0;
           carry[i+1]=0;
       }
    else if(c==1)
    {
      sum[i]=1;
      carry[i+1]=0;
    }
    else if(c==2)
    {
     sum[i]=0;
     carry[i+1]=1;
    }
    else if(c==3)
    {
     sum[i]=1;
     carry[i+1]=1;
    }
    }
    cout<<"sum"<<""<<"carry"<<endl;
    for(i=0;i=4;i++)
    {
    cout<<sum[i]<<""<<carry[i+1]<<endl;
    }
    return 0;
    }
    no errors but the loop won't end :/

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No errors, but a warning:
    Code:
    warning: suggest parentheses around assignment used as truth value
    On this line (with an error in it):
    Code:
    for(i=0;i=4;i++)

  14. #14
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60
    Code:
    for(int i=0;i=4;i++)

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by xentaka View Post
    Code:
    for(int i=0;i=4;i++)
    Eh, i was already declared at the very top for all the for loops. So that's okay.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-08-2010, 01:30 PM
  2. need some help with binary addition.
    By InvariantLoop in forum C++ Programming
    Replies: 21
    Last Post: 01-27-2005, 05:52 AM
  3. New addition
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-06-2004, 07:41 PM
  4. saturated binary/hex addition
    By revelation437 in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 03-09-2004, 05:11 PM
  5. Binary tree addition
    By crag2804 in forum C Programming
    Replies: 2
    Last Post: 09-30-2002, 07:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21