Page 1 of 1

Reputation:

looped and duped i need help

Posted 28 February 2010 - 03:12 PM

Hello everyone I am having a tough time here. I have tried several different approaches but all fail.

I need a simple c# program that accepts two inputs from the user. One is the base number and one is the exponential power number.

I then need a loop to calculate the answer.

Here is my problem.

First thing if the exponent is zero then the answer must be zero.
Second thing if hte exponent is one then the answer is the base number times 1.

Now if the exponent is not zero or one then...

I need to first take the base number times itself then each loop I need to take the previous answer and
multiply it by the base number.

Sounds simple I know but I am lost. Does this require a nested loop? One loop to get the base times itself then another loop to keep adding the new total times the base number. I guess I have to add some code in case the user enters 0 or 1?

Any suggestions would be greatly appreciated!

Below is the code I currently have to work with:

P.S. I am getting two errors. One says unreachable code is detected and the other error says not all code paths return a value?

public double Calculate()
{
double basenumber = double.Parse(textBoxBase.Text);
int powernumber = int.Parse(textBoxPower.Text);
double answer = 0.0;
int countInteger = 0;
{

for (countInteger = powernumber; countInteger > 0; countInteger--)
{

if (powernumber == 1)
{
answer = basenumber * powernumber;
}
if (powernumber == 0)
{
answer = basenumber * powernumber;
}
else
{
}
}

Is This A Good Question/Topic? 0

Replies To: looped and duped i need help

#2 Merddin Reputation: 12
• Posts: 37
• Joined: 19-February 10

Re: looped and duped i need help

Posted 28 February 2010 - 03:40 PM

You are on the right track. The issue is that you are checking for the value to 0 or 1 inside the for loop. Your if statement should be moved to outside the for loop, like this:

if (powernumber == 1)
{
answer = basenumber * powernumber;  // just set the answer to the basenumber (x^1 = x)
}
else if (powernumber == 0)
{
answer = basenumber * powernumber;  // just set the answer to 0 (x^0 = 0)
}
else
{
for (countInteger = powernumber; countInteger > 0; countInteger--)
{
// in here you want to multiply the number by itself each iteration
}

}

For inside your for loop remember that calculating a power is the same as multiplying the number by itself the number of times of the power:

eg. 2^5 = 2 * 2 * 2 * 2 * 2

You don't need to use the exponent inside the for loop

Hope that gets you heading in the right direction. Good luck with the coding.

This post has been edited by Merddin: 28 February 2010 - 03:41 PM

Reputation:

Re: looped and duped i need help

Posted 28 February 2010 - 03:48 PM

Thank you for your assistance! I really appreciate it. I took your advice and moved the ifs and else ifs...

I still am getting an error. The error says that "not all code paths return a value."

I am not sure what I still have wrong but here is the updated code after your suggestions:

public double Calculate()
{
double basenumber = double.Parse(textBoxBase.Text);
int powernumber = int.Parse(textBoxPower.Text);
double answer = 0.0;
int countInteger = 0;

{
if (powernumber == 1)
{
answer = basenumber * powernumber;
}

else if (powernumber == 0)
{
answer = basenumber * powernumber;
}
else
{
for (countInteger = powernumber; countInteger > 0; countInteger--)
{
}
}

}

#4 Merddin Reputation: 12
• Posts: 37
• Joined: 19-February 10

Re: looped and duped i need help

Posted 28 February 2010 - 03:59 PM

It is because of the location of your return:

if (powernumber == 1)
{
answer = basenumber * powernumber;
}

else if (powernumber == 0)
{
answer = basenumber * powernumber;
}
else
{
for (countInteger = powernumber; countInteger > 0; countInteger--)
{
}
return answer; // this is in the wrong spot
}

Since the return is nested with the final else, it may not be reached. It will only be reached if the value is not 0 or 1. You need to move the return out of the nested if-else if-else construct, so that the answer is returned no matter which condition is true.

Also, check your multiplication value within the for loop. If the user enters 5 for the base and 4 for the exponent, your code will generate the following:
5 * 4 * 4 * 4 * 4

This is not what you are looking for. You want to use:

This will generate
5 * 5 * 5 * 5 * 5

Hope that helps

Reputation:

Re: looped and duped i need help

Posted 28 February 2010 - 04:19 PM

Okay it is real close and it compiles and runs smooth. I still have got something wrong.
If I use 0 or 1 for an exponent the if's work perfect.

If I use a number 2 or larger I always get a 0?

For some reason if I enter say 4 to the power of 5... i just returns a zero?

Is this because I initialized the answer variable at zero?

Here is the code that returns zero instead of correct value.
I moved the return to outside of the if's so that it always returns a value.
I changed my other mistake to read answer *= answer;

I am clueless but it seems to me the method is getting the answer variable that was initialized at 0
and then incrementing it so it always returns back zero. But what do I know haha :0

Thanks for the help! public double Calculate()
{
double basenumber = double.Parse(textBoxBase.Text);
int powernumber = int.Parse(textBoxPower.Text);
double answer = 0.0;
int countInteger = 0;

{
if (powernumber == 1)
{
answer = basenumber * powernumber;
}

else if (powernumber == 0)
{
answer = basenumber * powernumber;
}
else
{
for (countInteger = powernumber; countInteger > 0; countInteger--)
{
answer *= answer;  //this here kicking out zeros?
}

}
}

#6 Merddin Reputation: 12
• Posts: 37
• Joined: 19-February 10

Re: looped and duped i need help

Posted 28 February 2010 - 04:57 PM

You have initialized answer to be zero in your declarations at the top of your code. Answer is never set to be anything other than zero, so your multiplication inside the for loop always ends up being zero. I've commented your code with some suggestions.

else
{
// try adding something here

for (countInteger = powernumber; countInteger > 0; countInteger--)
{
answer *= answer;  //the initial setting of answer is zero
}

}
}

Reputation:

Re: looped and duped i need help

Posted 28 February 2010 - 05:29 PM

I know what I need to do but I can't figure out how to do it? haha but thank you for all of your help!

I know that I need to initialize the answer variable to be the original base number. That sounds fairly simple. Well I tried doing that two ways but failed. I tried to initialize the answer variable = to the text box text and I also tried to initialize the variable to the basenumber variable. Either way the calculation spits out a huge number.

How can I initialize this answer variable to match the base number before it is countered the number of times equal to the power number?

Any more suggestions? I hope I am not driving you batty else
{
// try adding something here

//I tried adding several things here. I need to initialize the answer variable
to equal the base number. Problem is I tried it two ways and neither worked.
// 1st attempt

//  answer = double.parse(textboxbase.text);  >> produces huge number?

// 2nd attempt

//   answer = basenumber;  >> also produces huge numbers?

for (countInteger = powernumber; countInteger > 0; countInteger--)
{
answer *= answer;  //the initial setting of answer is zero
}

}
}

#8 Merddin Reputation: 12
• Posts: 37
• Joined: 19-February 10

Re: looped and duped i need help

Posted 28 February 2010 - 05:48 PM

My apologizes that was my mistake, your line