Thread: Power

  1. #31
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Ok so where in the code can i put that? I don't know where i could put it in... GAH confusing!

  2. #32
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Recursion could solve the problem, right? I mean if this were a function that took in an x and n parameter, you could just say something like:

    Example:
    Code:
    Function MyPow (double x, double n)
      double p = 1.0;
      int i;
    
      if(n < 0)
        return 1.0/MyPow(x, -n);
      else
        if(n)
          for(i = 0;i < n;++i)
            p *= x;      
      return p;
    End function

  3. #33
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Well i cant use the pow function.. and i have to use fnctions that we have learned inside of the class.

  4. #34
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The other option would be something like this:

    Example:
    Code:
    Function MyPow(double x, double n)
      double p = 1.0;
      int i, is_neg;
    
      if(n < 0)
      {
        is_neg = 1;
        n = -n;
      } else
        is_neg = 0;  
    
      ... Do your thing here
    
      if(is_neg)
        p = 1.0/p;
    
      return p;

  5. #35
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I never said anything about using pow(). I wrote my code straight from your code (I didn't even think I just eliminated unnecessary lines--its all you bud).

    The second example is more easily integrated into your current code, methinks. Since I am not going to urge you to use main() recursively. I suspect you are not on the topic of "writing your own functions" quite yet.

  6. #36
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Code thus far:
    Code:
    #include <FPT.h>
    int main()
    {
      //p=x^n inputs x,n output P
      double x,n,i,p;
      outS("Input a base number to be powered\n");
       x = inD() ;
      outS("Input a power\n");
      n = inD();
      i=0;
      p=1;
      if (n < 0){
        while (i < n){
          p=p*x;
          p=1/(p);
          i=i+1;
    }
    }
      else {
       while (i < n && n>0){
       p = p * x;
      i = i+1;
      
      }
      if (n==0){
      p=1;
      }
      }
        outD(p);
      
    }
    Ok so positive and 0 works for the powers. Just need to figure out the negative integers!

  7. #37
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Ya, im sorry i just dont understand it, its somehing simple im sure but i just cant figure it out!

  8. #38
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    *sigh* Lucid, I really can't just paste my code to where it applies on your project. Why? Because when you get this code working without one of us writing your code for you, you will be so stoked.

    I will do this much for you since it is driving all of us nuts:

    Indentation fixed
    Code:
    int main()
    {
      //p=x^n inputs x,n output P
      double x,n,i,p;
      outS("Input a base number to be powered\n");
      x = inD() ;
      outS("Input a power\n");
      n = inD();
      i=0;
      p=1;
      if (n < 0){
        while (i < n){
          p=p*x;
          p=1/(p);
          i=i+1;
        }
    
        /* Hint: Wouldn’t it make more sense to do 1.0/(p) after that other stuff? */
      }
      else {
        while (i < n && n>0){
          p = p * x;
          i = i+1;
        }
        if (n==0){
          p=1;
        }
      }
      outD(p);
    }
    I also highlighted in red a logical issue with your code too.

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
         while (i < n){
          p=p*x;
          p=1/(p);
          i=i+1;
        }
    If n is -5, and i is initialized to 0, how many loop iterations will you do 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.

  10. #40
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    GOD i can not think of what i need to set it to to make it enter the negative loop! Alright, i wont be mad if someone tells me... im an idiot. Im flustered to hell

  11. #41
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    HOLD UP! i think i got it!
    Code:
    #include <FPT.h>
    int main()
    {
      //p=x^n inputs x,n output P
      double x,n,i,p;
      outS("Input a base number to be powered\n");
      x = inD() ;
      outS("Input a power\n");
      n = inD();
      i=0;
      p=1;
      if (n < 0){
        while (i < fabs(n)){
          p=p*x;
          i=i+1;
        }
    	p=1/(p);   
      }
      else {
        while (i < n && n>0){
          p = p * x;
          i = i+1;
        }
        if (n==0){
          p=1;
        }
      }
      outD(p);
    }
    AGREEMENTS?!

  12. #42
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok well what if instead of saying if I have 0 is and I want to work my way up to a positive n I instead use my knowledge of the fact that n is less than 0 and say as long as n is less than i I can work my way down to n. Make sense?

  13. #43
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Lucid15 View Post
    HOLD UP! i think i got it!
    Code:
    #include <FPT.h>
    int main()
    {
      //p=x^n inputs x,n output P
      double x,n,i,p;
      outS("Input a base number to be powered\n");
      x = inD() ;
      outS("Input a power\n");
      n = inD();
      i=0;
      p=1;
      if (n < 0){
        while (i < fabs(n)){
          p=p*x;
          i=i+1;
        }
    	p=1/(p);   
      }
      else {
        while (i < n && n>0){
          p = p * x;
          i = i+1;
        }
        if (n==0){
          p=1;
        }
      }
      outD(p);
    }
    AGREEMENTS?!
    Bingo! Great job! Btw, you can also get away with

    Code:
        while (n < i){
          p=p*x;
          i=i-1;
        }
    fabs() is more expensive than just reversing the logic of the loop.

  14. #44
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Ok final check....
    Code:
    #include <FPT.h>
    int main()
    {
      //p=x^n inputs x,n output P
      double x,n,i,p;
      outS("Input a base number to be powered\n");
      x = inD() ;
      outS("Input a power\n");
      n = inD();
      i=0;
      p=1;
      if (n < 0){
        while (n < i){
          p=p*x;
          i=i-1;
        }
    	p=1/(p);   
      }
      else {
        while (i < n && n>0){
          p = p * x;
          i = i+1;
        }
        if (n==0){
          p=1;
        }
      }
      outD(p);
    }
    if i input 300 for x(base number) and -3 for n i get: .00000003

  15. #45
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Looks good to me I am glad you actually tested the code. I would have had to flame you for asking me to babystep you through compiling your hard labored work.

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, 02:46 AM
  4. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 11:56 AM