Thread: How to write a Power() method?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    How to write a Power() method?

    I'm stuck on this program in this book I'm reading. It basically wants me to write my own Power() method and return the value. If someone knows how to do this. That would be much appreciated. Here's my code

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter9Problem7
    {
        class Program
        {
    
            
            static void Main()
            {
                int x = 0;
                int y = 0;
                int result;
             
    
                Console.Write("\nEnter BASE: ");
                x = Convert.ToInt32(Console.ReadLine());
    
                Console.Write("\nEnter EXPONENT: ");
                y = Convert.ToInt32(Console.ReadLine());
    
                result = Power(x,y);
    
                Console.Write("\nBASE:{0}  EXPONENT:{1} = {2}", x, y, result);
             
    
           
    
            }
    
            static int Power(int x, int y)
            {
                
                
                for (int i = 1; i <= y; i++)
                    return 
            }
    
        }
    }


    I can't figure out the return part of it so I just left it That's where I'm stuck at

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You need to compute the result in the loop, and return it after the loop has finished.

    Given that the point of raising to an (integral power), all the loop will do is multiply by something by x the required number of times.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Well I think I figured it out it was real simple just

    return x * y;

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Gosh!! Me feels stupid!! heehee

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Well I feel even more stupid! I'm wrong it's not

    return x * y;

    I don't have a clue how to times x y number of times

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I got to thinking maybe I should use doubles

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Code:
           static int Power(int x, int y)
            {
                for (int i = 1; i <= y; i++)
                    return x * i;
            }
    
        }
    when I try this code it gives me not all paths return value error

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    That is because y could be less than 1.

    Remember with powers you are raising the value to itself.

    2^3 = 2*2*2 = 8

    So you need to change your logic since what you are doing currently you are multiplying x times i once and exiting the function.
    Last edited by prog-bman; 08-30-2014 at 10:05 PM.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by artistunknown View Post
    I don't have a clue how to times x y number of times
    0) Read all of the steps below and understand them, or print them out.

    1) Turn off your computer.

    2) Write down on paper the exact steps you would need to go through to do the calculation your self

    3) Check that your approach correctly hands special case of x = 0 and x = 1. If it doesn't, your approach is wrong, so return to step 2 and start again. Bear in mind that 0 to the power of 0 does not have a defined result.

    4) Pick a couple of arbitrary values of x that are neither 0 nor 1, one positive and one negative. Pick a couple of positive values of y. Test your approach with the the combinations of those values of y. If you don't get the right result for any of those combinations, your approach is wrong so return to step 2 and start again.

    5) Based on your approach, write down some pseudo-code that would produce the same result.

    6) Turn on your computer.

    7) Edit a source file, and work out a C equivalent of your pseudo-code.

    8) If it doesn't compile or link, return to step 7.

    9) Test your program with the values of x = 0 and x=1, and for the combinations of values you used in step 4. If your program produces the wrong result, return to step 7.

    10) If you've got this far, you have a program producing the right results. Rejoice.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter9Problem7
    {
        class Program
        {
    
            
            static void Main()
            {
                int x = 0;
                int y = 0;
               
             
    
                Console.Write("\nEnter BASE: ");
                x = Convert.ToInt32(Console.ReadLine());
    
                Console.Write("\nEnter EXPONENT: ");
                y = Convert.ToInt32(Console.ReadLine());
    
              
    
                Console.Write("\nBASE:{0}  EXPONENT:{1} = {2}", x, y,Power(x,y));
    
                Console.ReadLine();
           
    
            }
    
            static int Power(int x, int y)
            {
                int result = 1;
                while (y>0)
                {
                    y--;
                    result *= x;
                }
                return result;
            }
    
        }
    }
    Well sorry Grumpy I didn't take your advice I wouldn't have been able to figure out this one no matter how long I worked on it I found the answer on the internet and changed it a little to make it work this is what I came up with Thanks for the help

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by artistunknown View Post
    Well sorry Grumpy I didn't take your advice I wouldn't have been able to figure out this one no matter how long I worked on it I found the answer on the internet and changed it a little to make it work this is what I came up with Thanks for the help
    So you've got the answer, but learned nothing of value. If you can't reason out an answer to a trivial learning exercise like this, you won't have a hope with real-world problems.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Yeah, I know but I just don't want to give up just yet.

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Well at least I understand the code now I was a little confused by it at first
    I did attempt to work out the problem on my dry erase board but I just couldn't think outside my own little box

  14. #14
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Well with the logic I was writing I guess I could have written the Power function like this

    Code:
           static int Power(int x, int y)
            {
                int result = 1;
                for (int i = y; i >= 1; i--)
                {
                   
                    result = result * x;
                  
                }
                return result; 
               
            }

  15. #15
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Code:
            static int Power(int x, int y)
            {
                int result = 1;
                for (int i = 1; i <= 9; i++)
                {
                   
                    result = result * x;
                  
                }
                return result; 
               
            }
    This is the closest to what I was doing I just couldn't figure out the body Hopefully I will get better if not off to culinary school hee hee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-23-2012, 11:35 PM
  2. difference between this->method() and method()
    By nacho4d in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2009, 04:11 PM
  3. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 3
    Last Post: 09-19-2009, 10:35 AM
  4. No Power, but power being supplied.
    By jrahhali in forum Tech Board
    Replies: 6
    Last Post: 08-11-2005, 02:50 AM
  5. Read/Write Method
    By bigpmc in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2004, 03:28 PM