c++ : binary addition

This is a discussion on c++ : binary addition within the C++ Programming forums, part of the General Programming Boards category; You need to learn to indent properly. Also, the warning is a little misleading. The big problem is that you ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    You need to learn to indent properly.
    Also, the warning is a little misleading. The big problem is that you are assigning instead of comparing.
    But even then, it doesn't make sense, since i is initially 0, and the loop executes only while i equals 4. Hence, the loop won't run.

    Also, your code is way overcomplicated. I think you should formulate proper logic for this first.
    The method is the same if you add 5 bits as if you were adding n bits.
    It's just as usual addition, but in base 2 instead of base 10.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60
    Doh! Just saw it and assumed it was missed.

  3. #18
    Registered User
    Join Date
    May 2011
    Posts
    16
    i change i=4
    no wornings or errors
    but the summation is wrong if there is carry

    Code:
     #include<iostream>
    using namespace std;
    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;
    }
    }
    
    for(i=0;i<5;i++)
    {
    cout<<"sum= "<<sum[i]+carry[i+1];
    cout<<endl;
    }
    return 0;
    }

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Indent the code.
    Make a flowchart.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You know that this:
    Code:
    cout<<"sum= "<<sum[i]+carry[i+1];
    is not how carry works.

  6. #21
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60
    Courtesy of source code formatter:

    Code:
    #include<iostream>
    using namespace std;
    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;
            }
        }
    
        for(i=0; i<5; i++)
        {
            cout<<"sum= "<<sum[i]+carry[i+1];
            cout<<endl;
        }
        return 0;
    }

  7. #22
    Registered User
    Join Date
    May 2011
    Posts
    16
    is not how carry works.
    then how ?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Look at ordinary addition:
    Code:
     11
     387
    +295
    ----
     682
    Would you then write the answer down as 693? No you would not.

  9. #24
    Registered User
    Join Date
    May 2011
    Posts
    16
    i don't know how to write fuction to calculate other than this way

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Once you do the carry, you don't do it again. Do you not see how you have already added the carry in the first place?

  11. #26
    Registered User
    Join Date
    May 2011
    Posts
    16
    Code:
     cout<<"sum= "<<sum[i];
            cout<<endl;
    you mean like this ?

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right, although at some point you need to remember that you add from right to left.

  13. #28
    Registered User
    Join Date
    May 2011
    Posts
    16
    ok i want the output to be in the same row , like this : 0 0 0 1 0
    rather than : 0
    0
    0
    1
    0
    how can i do it ?

    and there is a value i keep getting with the sum -858993460 :S

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. Don't print a new line after every digit.

    2. Also make sure your arr1 are reading what you want them to be.

  15. #30
    Registered User
    Join Date
    May 2011
    Posts
    16
    2. Also make sure your arr1 are reading what you want them to be.
    what you mean ?
    Code:
    = { 0 }
    if i remove 0 from the carry , the output will be -858993460 without the summation

    another questions:
    1. how i can make the program to stop if the user entered numbers > 1
    i already write the if experssion and it's appear if the user entered > 1 but still ask for the second number

    2. if add 00001 to 00001 it will = 00000 and i want the last carry, so = 000001
    how i can fix that ?

    3. i want the user to enter the number without sparating : 10010
    i tried to use / % but it didnot work


    although at some point you need to remember that you add from right to left.
    4. is it a problem ? if it's how can fix it?

    and thank you,you've been a big help
    you gonna made my day!
    Last edited by Soul; 05-24-2011 at 05:10 PM.

Page 2 of 3 FirstFirst 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