Thread: decimal to number if digits in different bases

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,336
    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
    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.

  5. #5
    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);
    
    }

  6. #6
    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.

  7. #7
    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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

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

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I guess the question at hand is what is wrong with needing to compile in the math library? Mingw just does it implicitly. If you were to erase libm.a from your machine you would not be able to compile it with mingw either. Furthermore, you can explictly -lm on mingw and still compile just fine on windows (if your question is something to the effect of "how do I make a single makefile").

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And the reason that gcc-mingw is "different" here is that it's not using glibc, but rather the MS C-library (msvcrt.dll), which has both math and standard library functions in one library, so there's no need to inform gcc about the fact that you are using math functions, because it is part of the basic standard library anyways.

    That's just the way it is when working with different environments - sometimes you need to do things different from one system to another, because someone somewhere took some arbitrary decision to do things differently than some other system. It's not that one is definitely right and the other is wrong, just different.

    --
    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.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I just want the OP to be clear on one thing. For the sake of your makefiles or whatever the -lm is simply ignored by mingw.

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, 06: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