Thread: Can't raise x to the power of y

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    Can't raise x to the power of y

    Hi, is there a way to raise x to the power of y (x^y), without using the math.h library and the pow(x,y) function?

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    With a for loop ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Write pow() yourself.

    I don't think you want to do that. Just use pow() if you can.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    yeah, i have to do it without using pow(x,y)

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    So write your own pow() function.

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Use a for loop. Or use a for loop that squares and loops through the bits of y.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Hi I'm trying to use the for loop, but i'm having trouble figuring out how to use it. Do you know what i'm doing wrong?

    Code:
      int n;
      int output[60];
      int index = 0;
      int condition;
      int quotient;
      int size = 0;
      int nsize;
      int converted_number=0;
      int power;
      int i;
    
      printf("Enter base(b) between [1,10], and a number(n), such that digits of n is between[0,b-1] in this format 'n b': ");
      scanf("%d %d", &n, &b);
    
      printf("n is: %d\n", n);   //Test
      printf("b is: %d\n", b);   //TEST
      nsize = n;
    
      if ((b < 1) || (b > 10))
      {
        printf("Your base is not between 1 and 10");
        return 0;
      }
    
      if (n < 0)
      {
        printf("n must be positive");
        return 0;
      }
    
      while (nsize != 0)  //counting the total number of digits
      {
        nsize = nsize/10;
        ++size;
      }
      printf("size of n is: %d \n", size);  //testing to see the size
    
      while (size > 0)
      {
        output[index] = n % 10;
        n = n/10;
        printf("number to convert: %d\n", output[index]);
    
        for (i = 1; i <=index; i++)       //FOR LOOP IN HERE!!!
        {
          power = output[index]*;      //NOT SURE ON WHAT TO DO HERE
          converted_number += (power*output[index]);
        }
    
        printf("%d * %d^%d %d\n", output[index], b, index, converted_number);
        --size;
        index++;
      }
    
      printf("converted number is: %d", converted_number);
    
    
    
      return 0;
    }

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    int power(int base, int ext)
    { 
       int i;
       int res=1;
       
       for(i=1; i <= ext; i++)
          res = res * base;
    
       return res;
    }
    here is a basic pow function. It can handle just int. You will have to work around to make it to work for all datatype.

    ssharish2005

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    x^y simply means, mutliply x by itself y times.

    Such that, 5^10
    = 5 * 5 * 5 * 5 *5 * 5 * 5 * 5 * 5 *5

    Quite easy to put in a loop.
    Last edited by zacs7; 08-30-2007 at 08:16 PM. Reason: Giving the answer

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    This sounds like a homework problem, but I'll give you the hint that pow(x, y) can be rewritten in terms of other functions from <math.h>, so that's one way, if you're allowed to use those. Otherwise, if y is an integer, you can implement it in terms of a loop as described above.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Oh, you mean it only has to work for integer values of y?
    Well I wont bother to post my solution then.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raise to the power problem
    By swgh in forum C Programming
    Replies: 7
    Last Post: 12-01-2008, 08:12 AM
  2. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  3. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 11:56 AM
  4. power recursion
    By datainjector in forum C Programming
    Replies: 2
    Last Post: 11-06-2002, 04:56 PM