a to the power b (in C)

This is a discussion on a to the power b (in C) within the C Programming forums, part of the General Programming Boards category; Hello ,this is my first post here,so please excuse me if i broke any rules, or i posted in a ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    a to the power b (in C)

    Hello ,this is my first post here,so please excuse me if i broke any rules, or i posted in a wrong section.

    I have this homework for school:we have to come up with a program that transforms a number in radix a to a number in radix b.
    Ive come up with this so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main()
    {
        char number[50];
        int radix;
        int i;
        int result;
        int max=strlen(number);
        int test=0;
        int c=max-1;
    
    printf("Insert the number you want to transform\n");
    scanf("\n%s",number);
    printf("\nInsert its radix\n");
    scanf("\n%d",&radix);
    
    printf("\nmax=%d",max);
    test=pow(radix,c);
        printf("\n%d",test);
    
    for(i=0;i<max;i++){
        result=result+(number[i]*(pow(radix,c)));
        c--;}
        printf("\nresult=%d",result);
    
    }

    The problem is, the pow(a,b) function doesnt seem to work right.Ive even tried to define my own function but that didnt work either.
    So please help me,but keep in mind that im a really newb(this is my first semester in programing),so dont come with really complicate answers.
    Thank you in advance!

    Mblue

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Before discussing the pow() function, let's look at this:
    Code:
        int max=strlen(number);
        int test=0;
        int c=max-1;
    What is the string number at this point in the code? How long is it?
    What value is max?

    C is a language where ordering of expressions affect their value. It is not mathematics where you can say "Let X be Y - 6", and then say "Now make Y be 8, what value is X?" - there may be programming languages that do allow that, but I have never worked in one that behaves that way. Thus, in C and most other programming languages, you must make sure everything you use to calculate something else has the correct value first.

    I'm pretty sure that pow() does the right thing, but beware that if you are using integers, it may result in "interesting" rounding errors: 2 ^ 3 = 8, but pow(2, 3) may give 7.999999..., which when chopped to be an integer turns into 7 - which wasn't quite what you wanted, was it? You need to either use a simple loop to calculate the power, or ROUND the integer value by adding a small value before chopping the fraction part (e.g. (pow(x, y)+0.1) - that should be OK when using integer values for x and y, as long as x and y are reasonable size (no bigger than 2^31, such as 10 ^ 10, 8^10 or 4^15 for example).

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

  3. #3
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Also note that your program won't work with radix > 10. You're reading an integer, which may not contain letters, which you'll need to represent digits > 9. In programming, base 16 is pretty common, so you might want to think again about the overall structure of your program. If I were you, I would read the number as a string, write a simple function to retrieve its decimal value according to the given radix and then convert it to whatever radix the user supplied. Hence for a quick start, try writing a function that converts an int (in base 10) to a string that contains the corresponding representation in the given radix (i.e. the second part of your exercise).

    On a sidenote, you should make sure that the input number respects the limits of its radix. What if the user supplies 69 and claims it to be in base 2? But you can safely skip this check until everything else works as expected.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Snafuist View Post
    Also note that your program won't work with radix > 10. You're reading an integer, which may not contain letters, which you'll need to represent digits > 9. In programming, base 16 is pretty common, so you might want to think again about the overall structure of your program. If I were you, I would read the number as a string, write a simple function to retrieve its decimal value according to the given radix and then convert it to whatever radix the user supplied. Hence for a quick start, try writing a function that converts an int (in base 10) to a string that contains the corresponding representation in the given radix (i.e. the second part of your exercise).
    But number is a string, despite it's name. Wake up!

    --
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by mblue View Post
    The problem is, the pow(a,b) function doesnt seem to work right.Ive even tried to define my own function but that didnt work either.
    Of course it works right. You know you're a beginner, therefore you must know that chances are always greatest that you aren't doing something properly.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    But number is a string, despite it's name. Wake up!
    Doh!
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  2. No Power, but power being supplied.
    By jrahhali in forum Tech Board
    Replies: 6
    Last Post: 08-11-2005, 02:50 AM
  3. The destructive power of a nuclear bomb
    By InvariantLoop in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 03-24-2005, 01:46 AM
  4. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 10:56 AM

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