Thread: Power

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

    Power

    So for my hw assignment i need to build a program that allows the user to input a base number and a power integer. It only needs to be whole powers. I tried this out... but it didn't seem to work. Any thougts?
    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;
      while(i<=n){
      x=x*x;
      i=i+1;
      }
        outD(x);
      
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What does "doesn't seem to work" mean?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Pick small numbers (like x=3, n=5) trace it out and you'll easily figure out what's going on.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Quote Originally Posted by Lucid15 View Post
    Code:
      i=0;
      while(i<=n){
      x=x*x;
      i=i+1;
      }
      
    }

    u r doing one extra multiplication.
    either put i=1 (in red line)
    or put while(i<n) (in blue line)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SONU
    u r doing one extra multiplication.
    either put i=1 (in red line)
    or put while(i<n) (in blue line)
    There's more, actually. On each iteration, x is squared, so actually what is computed is x^(2^n), not x^n. Without your correction, it would compute x^(2^(n+1)).

    Considering that the comment says "inputs x,n output P", I suspect that this is a hint provided by Lucid15's. My own hint: assign x to p, and then change p in the loop, and finally print p, not x.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    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;
      while(i<=n){
      p=x*x;
      i=i+1;
      }
        outD(p);
      
    }
    That's what i have. but when i test it x=2 n=3 i shuld be eight, but it comes out 4.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are setting p to x*x on each iteration. You need to set p on each iteration to a new value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    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;
      while(i<=n){
      x=x*x;
      p=x;
      i=i+1;
      }
        outD(p);
      
    }
    Thats what i have? it doesnt work though

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Can you not see that x*x is causing you to compute squares instead of powers?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    SO what do i need to do? sorry im really confused.. isee that i am squaring it, also we are not allowed to use the pow function. What do i need to dO?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose I asked you to compute 3^4. You would say:
    3^4 = 3 * 3 * 3 * 3 = 81

    But wait. To compute 3^4, you compute 3 * 3 = 9.
    Then, you compute 9 * 3 = 27.
    Then, you compute 27 * 3 = 81.

    Do you see the pattern? In each step, you continue to multiply by 3 (i.e., by x). The 9 occurs once on the right hand side, as the result of 3 * 3. It also occurs on the left hand side, where it is used to compute the next value. So, if you saved this in a variable, how would it look?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    im really confused as to how to make this work... I THOUGHT my code would do it right...

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you step through your code? Did you notice that instead of always multiplying by 3 every time, you did 3*3, then 9*9, then 81*81, ...? In other words, once you've got hold of 3, you have to keep it -- you can't do x=x*x, since then you've lost the three. You'll need a separate variable for the answer, different than the repeated multiplier.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Code:
    #include <FPT.h>
    int main()
    {
      //p=x^n inputs x,n output P
      double x,n,i,p,u;
      outS("Input a base number to be powered\n");
      x = inD() ;
      outS("Input a power\n");
      n = inD();
      i=1;
      u=x;
      while(i<n){
      x=x*u;
      p=x;
      i=i+1;
      }
        outD(p);
      
    }
    I belive this work for 2... but not for others

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay... you're going to regret this, but the answer is:
    Code:
    p = 1;
    i = 0;
    while (i < n)
    {
        p = p * x;
        i = i + 1;
    }
    or equivalently:
    Code:
    p = 1;
    i = 0;
    while (i < n)
    {
        p *= x;
        ++i;
    }
    or most appropriately:
    Code:
    p = 1;
    for (i = 0; i < n; ++i)
    {
        p *= x;
    }
    EDIT:
    Actually, I probably should have given you an example of computing a sum instead, which uses a similiar concept. It may be a little too late for that now, but to test your understanding of my example, try writing a program that computes the sum of integers from n to m, without using the formula.
    Last edited by laserlight; 09-21-2008 at 01:55 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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