decimal to number if digits in different bases

This is a discussion on decimal to number if digits in different bases within the C Programming forums, part of the General Programming Boards category; anyone knows how they would start this off? I need a which will take a decimal positive integer x then ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    19

    decimal to number if digits in different bases

    anyone knows how they would start this off?

    I need a which will take a decimal positive integer x then the program prints the number of digits required to represent the decimal integer x in bases 2, 4, 8, and 16.

    For example: If the input is 18, then the output should be:

    Given decimal number: 18
    Number of digits required in base 2: 5
    Number of digits required in base 4: 3
    Number of digits required in base 8: 2
    Number of digits required in base 16: 2

    any help will be appreciated

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You could use tables, or a division loop (this is the "bad at math" approach).

    You could use exponents/logarithms (this is the "good at math" approach).

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    how exactly would it start though?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would start with a piece of paper and work out a psuedocode algorithm. Since the problem is using the traditional CS bases (2, 4, 8, 16) I would probably draw upon my knowledge of binary.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    alright thanks. i got how to do it

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    You could use tables, or a division loop (this is the "bad at math" approach).

    You could use exponents/logarithms (this is the "good at math" approach).
    However, unless the numbers are really huge, it's probably faster to do the "bad at math" method than to calculate the log(X)/log(Y) - even if you can-precalculate the log(Y).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    19

    i cant figure out whats wrong with it any help please

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
            int x, a, b, c, d;
    
            printf("Please enter a number>");
            scanf("%d", &x);
    
            a=1+(log(x)/log(2));
            b=1+(log(x)/log(4));
            c=1+(log(x)/log(8));
            d=1+(log(x)/log(16));
    
            printf("Given decimal number: %d\n", x);
            printf("Number of digits required in base 2: %d\n", a);
            printf("Number of digits required in base 4: %d\n", b);
            printf("Number of digits required in base 8: %d\n", c);
            printf(" Number of digits required in base 16: %d", d);
    
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you think is wrong? Or, put another way, how is the result differnet from what you expect?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    it just doesnt compile. it gives me this when i try to compile it. i am using unix

    > gcc num-digits.c
    Undefined first referenced
    symbol in file
    log /var/tmp//ccNuKjnz.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure it doesn't matter, but can you try renaming the file to something without '-' in the name? If there's an undefined name in the input, it should tell you.

    I copied and pasted your code onto my machine and compiled it with gcc-mingw (I have a windows machine), and it works just fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    yeah it works on windows but it doesnt want to compile on unix. hmm i wonder what it is

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    can you do "od -t x1 numdigits.c | head > numbytes.dump" and post the contents of numbytes.dump here?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    is this what you wanted. i'm a new at this so dont much.
    Code:
    jgnlez@shellfish:~/ece175> od -t x1 num-digits.c | head > numbytes.dump
    jgnlez@shellfish:~/ece175> cat numbytes.dump
    0000000 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e
    0000020 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 6d 61 74
    0000040 68 2e 68 3e 0a 0a 69 6e 74 20 6d 61 69 6e 28 29
    0000060 0a 7b 20 0a 09 69 6e 74 20 78 2c 20 61 2c 20 62
    0000100 2c 20 63 2c 20 64 3b 0a 09 0a 09 70 72 69 6e 74
    0000120 66 28 22 50 6c 65 61 73 65 20 65 6e 74 65 72 20
    0000140 61 20 6e 75 6d 62 65 72 3e 22 29 3b 0a 09 73 63
    0000160 61 6e 66 28 22 25 64 22 2c 20 26 78 29 3b 0a 0a
    0000200 09 61 3d 31 2b 28 6c 6f 67 28 78 29 2f 6c 6f 67
    0000220 28 32 29 29 3b 0a 09 62 3d 31 2b 28 6c 6f 67 28

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think I know what the problem is - you need to link to the math library.

    Try adding -lm at the end of the gcc command.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    that made it work. but how do i make it work without adding -lm nevermind i got it thanks.
    Last edited by jorgejags; 09-24-2008 at 12:47 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  4. Number of digits?
    By falador in forum C Programming
    Replies: 2
    Last Post: 05-13-2004, 03:52 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

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