5 Replies - 11455 Views - Last Post: 16 April 2010 - 07:00 PM Rate Topic: -----

#1 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Calculating exponents using a loop

Posted 16 April 2010 - 06:02 PM

Hello and thanks in advance for any help/advice. I am attempting to create a program that calculates exponents using a loop structure. The user inputs the base value and the power value, clicks the button and...so far I'm not getting it to work correctly. My first thoughts were that a for loop would be ideal for this situation, but if that is not the case please let me know. I can get the program to work just fine using Math.Pow(x,y), but as I stated we are supposed to somehow do this with a loop. Here is the loop I have currently:

 public double CalculatePower(double baseValue)
        {
            //Use a loop to calculate exponents
            for (double i = 0; i <= powerValue; i++)
                
            {
                result = i *= baseValue;
            }
            return result;
        }



Currently, when I run the program it is adding "baseValue" to "powerValue" instead of raising "baseValue" to the power of "powerValue". Everything works fine, except for my math, and I'm just not sure how to do it in code.

Is This A Good Question/Topic? 0
  • +

Replies To: Calculating exponents using a loop

#2 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Calculating exponents using a loop

Posted 16 April 2010 - 06:22 PM

I don't really know what's wrong with your code :) But to calculate exponent, for example 2^10 (2 is baseValue and 10 is powerValue) with for loop you just need to multiply baseValue (2) times powerValue (10), and you get 1024. Simple :)
So it should look like:
public double CalculatePower(double baseValue, int powerValue)
{
  double result = 1;
  for (int i = 0; i < powerValue; i++) 
  {
      result *= baseValue;
  }
  return result;
}


But this will work ONLY for positive powerValue values!
For example, 2^(-10) is just 1/2^10, so we can put this in code ;)
public double CalculatePower(double baseValue, int powerValue)
{
  double result = 1;
  for (int i = 0; i < Math.abs(powerValue); i++) 
  {
      result *= baseValue;
  }
  if(powerValue > 0)
    return result;
  else
    return 1/result;
}


This post has been edited by k0b13r: 16 April 2010 - 06:40 PM

Was This Post Helpful? 1
  • +
  • -

#3 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Re: Calculating exponents using a loop

Posted 16 April 2010 - 06:42 PM

Okay, that works to a certain degree, meaning that it works fine for the very first base and power that I enter. Then, once I clear the output label and input text boxes, it has saved the value of result and uses it as the base instead of what is entered in the text box. For example, I enter 5^2 the first time and it works fine, giving me 25 as the answer, but when I clear (using a button coded to do this) and re-enter 5^2 it gives me 625 as the answer, which is actually 25^2. Any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Calculating exponents using a loop

Posted 16 April 2010 - 06:44 PM

Hm maybe you use global 'result' variable (I used local method variable)? For the first time it's equal to 1, and after first 5^2 it gets 25. And 2nd time it's not 1, but 25! So you get 25^2 instead of 5^2 ;) You have to clear result before every power calculation :P But it's only a theory :D

This post has been edited by k0b13r: 16 April 2010 - 06:44 PM

Was This Post Helpful? 1
  • +
  • -

#5 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Re: Calculating exponents using a loop

Posted 16 April 2010 - 06:47 PM

Nevermind, I see what I did wrong. I thought I could declare "result" with the other variables that I am using, instead of doing it within the method with the loop. It seems to be working fine now. Thanks much.
Was This Post Helpful? 0
  • +
  • -

#6 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Calculating exponents using a loop

Posted 16 April 2010 - 07:00 PM

You can do that (but I think it's not very ... intuitive), but you have to assign 1 to this variable on each method call ;)
public double CalculatePower(double baseValue, int powerValue)
{
  result = 1;
  (...)
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1