Thread: adding two binary numbers

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your code already stores input as an array of char. Every char is an integral value, so each char value can be stored in an int.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  2. #17
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    This is my new code but there are still bugs in it. Could you please tell me what's wrong with it.


    Code:
    # include<stdio.h>
    #include <string.h>
    char x[32];
    char y[32];
    char z[32];
    char b[32];
    int c[32];
    int a,s;
    char sum[32];
    int carry;
    main()
    {
    printf("Please input the first binary number.\n");
    scanf("%s", x);
    printf("Please input the second binary number.\n");
    scanf("%s", y);
    for(a=31;a>=0;a--){
     if (x[a]=='0')
          b[a]=0;
     else if (x[a]=='1')
          b[a]=1;}
    for(a=31,s=0;a>=0;a--,s++){
     if (y[a]=='0')
          c[a]=0;
     else if (y[a]=='1')
          c[a]=1;}
          carry=0;
    for(a=31;a>=0;a--){
     sum[a]=b[a]+c[a]+carry;
     if (sum[a]>1){
        sum[a]=sum[a]-2;
        carry=1;}
     else{
        carry=0;}
    }
    for(a=0;a<=31;a++)
     printf("%d", sum[a]);
    }

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by renz15 View Post
    Could you please tell me what's wrong with it.
    That's your job. Why don't you tell us what part doesn't seem to work right?
    Code:
    char x[32];
    char y[32];
    char z[32];
    char b[32];
    int c[32];
    int a,s;
    char sum[32];
    int carry;
    Why are all of those global when you only have one function anyway?
    Code:
    main()
    Use:
    Code:
    int main( void )
    Then return 0; at the end of main.
    Code:
    {
    printf("Please input the first binary number.\n");
    scanf("%s", x);
    printf("Please input the second binary number.\n");
    scanf("%s", y);
    for(a=31;a>=0;a--){
    What happens if I enter 8 digits instead of 32?
    What happens if both of the end digits are 1? ... and you have a carry?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by renz15 View Post
    I have a question. How can I store every digit of the inputted binary number using array of ints?
    Just subtract '0' from each index location...
    Code:
    for (int x = 0; x <32; x++)
      array[x] -= '0';
    Now it's all numerically 1s and 0s...

  5. #20
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Quote Originally Posted by quzah View Post
    What happens if I enter 8 digits instead of 32?
    What happens if both of the end digits are 1? ... and you have a carry?
    there are zeroes after the answer and when the first digits of the binary numbers is 1, the carry wont show.
    Attached Images Attached Images adding two binary numbers-bin-jpg 

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by renz15 View Post
    there are zeroes after the answer
    There are zeros because you are telling it you area always reading 32 bits, and you only read four.

    So what is your actual question? What does your program do or not do that it shouldn't or should? Why is it you think it doesn't work?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If only there were some way to determine the end of a string so you don't have to cycle through every index of each array ...

    And some suggestions:
    - Fix the indentation/layout of your code so it's easier to read
    - Make your variable names meaningful so the code is easier to read
    - You drag the variable 's' around and manipulate it, but never use it for anything
    - Heed Quzah's advice about the proper form of "main()"

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Matticus View Post
    If only there were some way to determine the end of a string so you don't have to cycle through every index of each array ...
    Speaking of strings... If they do enter 32 1s/0s, they won't have enough room for the nul terminator, which means it will get written past the end of the array (that's bad, just in case the OP doesn't know).


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yes, I actually noticed that just now while writing my own version of this program.

    I wonder if the OP has considered making a loop that would cycle through the character array and count until it reached a certain (special) character. Perhaps they could use this count for something helpful.

  10. #25
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Quote Originally Posted by Matticus View Post
    Yes, I actually noticed that just now while writing my own version of this program.

    I wonder if the OP has considered making a loop that would cycle through the character array and count until it reached a certain (special) character. Perhaps they could use this count for something helpful.
    Can you tell the code for that 'cause I really don't know. Please.

  11. #26
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Never a time like the present to learn. C-Style Strings tutorial
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #27
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Let's see if you have what it takes. By answering the questions I will ask, we will approach the code I mentioned together.

    Question 1: In 'C', what is the difference between a string and a character array?

  13. #28
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Matticus View Post
    Let's see if you have what it takes. By answering the questions I will ask, we will approach the code I mentioned together.

    Question 1: In 'C', what is the difference between a string and a character array?
    What is this, Are you smarter than a 5th grader? I choose c, wait....d**n.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #29
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Quote Originally Posted by Matticus View Post
    Let's see if you have what it takes. By answering the questions I will ask, we will approach the code I mentioned together.

    Question 1: In 'C', what is the difference between a string and a character array?
    In a string, we can store set of words and when we compile it, the compiler automatically puts a null character while in a character array we can only store characters and we need to assign a place for the null character. Am I right?

  15. #30
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by AndrewHunter View Post
    Hardly, I despise television with a passion unrivaled by most.

    Quote Originally Posted by AndrewHunter View Post
    I choose c...
    Me, too. That's why I haunt this board

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numbers adding together = bad
    By Zyk0tiK in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 04:37 PM
  2. Adding big numbers
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 03:36 PM
  3. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  4. adding numbers
    By Allen_Buchannon in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 05:43 PM
  5. adding odd numbers
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 09-06-2001, 01:44 PM