Thread: Binary to Decimal help...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    Binary to Decimal help...

    i cant figure out how i should write a c program regarding this problem...

    when i enter a binary number,say 101101, the result would be the decimal equivalent of 45...

    it goes like this,

    1 0 1 1 0 1 equals
    32 16 8 4 2 1

    so,form right to left, it goes like 1,2,4,8,16,32,64,128,256,512,1024...so on...(like IP addressing,VLSM...)
    it will add only on values of 1 and not 0...

    counting is from right to left,but it wont start until 1 is the rightmost...

    using if and then or loops is not allowed...only arithmetic operators...

    thanks in advance...

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    just write the program using loops, then remove the loop, and copy the inside of the loop into your code over and over

    edit: if your just learning how to program, im sorry, but you have a terrible teacher/lecturer/tutor/textbook
    Last edited by LordPc; 07-01-2010 at 07:43 AM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by anarcho View Post
    using if and then or loops is not allowed...only arithmetic operators...
    Without using at least a while() loop, you can only do this if the number is of fixed length, and then it will just be cut and paste some almost identical code over and over. Here's a clue: put the binary number into a char string:
    Code:
    char num[]="101101";
    So the fixed length is 6. If you want to work right to left (aka "big endian"), start with num[5]:
    Code:
    int total = 0, add = 1;
    if (num[5] == '1') total += add;
    add *= 2;
    if (num[4] == '1') total += add;
    add *= 2;
        [...etc]
    If you can't even use "if", those lines could be:
    Code:
    total += add*(num[5] - '0');
    Notice, '0' (which '0' == 48) not 0. For an understanding of that read up on the ASCII table:
    http://www.idevelopment.info/data/Pr...ii_table.shtml
    http://en.wikipedia.org/wiki/ASCII
    Last edited by MK27; 07-01-2010 at 09:23 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Is this a recursion problem you are supposed to do? Cause, without using loops, that is about the only way to do it.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    So why can't you go from left to right? Might be easier IMO.

    [Edit] as noted recursion is the only alternative if loops are a no-no.
    Last edited by itCbitC; 07-01-2010 at 09:57 AM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    [Edit] as noted recursion is the only alternative if loops are a no-no.
    No, if this is to deal with a fixed number of bits (in the example, 6), you don't need either, but you will end up with some "worst practice" long noodle style code exemplifying unnecessary duplication.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Would it really be possible to do with recursion without "if" ?

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Assuming the instructor really wants them to use recursion like Kennedy suggested, then the instructor is an absolute retard. o_O
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Yes. You use return values and tertiaries.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes but using tertiaries is equivalent to using if statements, what I meant of course is, without any way of testing a condition.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Methinks! MK27 has a good point because w/o a fixed no. of input characters this won't be possible w/o using ifs or loops.
    And agree with Susonics too, because even with the use of recursion the EOF condition has to be tested for the input.
    Last edited by itCbitC; 07-01-2010 at 12:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread