Hi.
I need some help with a programming assignment i have to return tomorrow.
I'm not asking for a complete code, i just need to know how i can possibly finish it.
1. I have to write a substitute for Math.Pow(x,y) without using the Math class.
2. I have to write a program that calculates "n!"
Thanks.
Need help with programming assignment
Started by Dori, Apr 08 2008 04:49 AM
5 replies to this topic
#1
Posted 08 April 2008 - 04:49 AM
|
|
|
#2
Posted 08 April 2008 - 05:50 AM
You'll need to find out how factorials and exponentiation work. You can find articles on both topics on Wikipedia: Factorial and Exponentiation.
#3
Posted 08 April 2008 - 06:48 AM
i know how they work, well in for the most part.
i just don't have an idea how to solve this
i just don't have an idea how to solve this
#4
Posted 08 April 2008 - 11:24 AM
Hi, Dori. You need to use a piece of code called a 'loop'.
We'll start with the factorial question. To solve it logically, you need to see what needs to be done with example.
If we try 5! - it is 5*4*3*2*1 = 125.
So what we need to do is multiply the number by all the numbers down to 1. It might be easier just to do it backwards, from one to five.
Here's some code that will do this:
This code should work out the factorial, by cycling through all the numbers from 1 to n, and multiplying it on each time.
Try to work out the Math.Pow() one yourself. A loop will be needed again, although the process will be slightly easier.
Good luck, and I hope this post reaches you in time!
Xav
We'll start with the factorial question. To solve it logically, you need to see what needs to be done with example.
If we try 5! - it is 5*4*3*2*1 = 125.
So what we need to do is multiply the number by all the numbers down to 1. It might be easier just to do it backwards, from one to five.
Here's some code that will do this:
//Replace the "= 5" bit with your required number
int n = 5;
int answer = 1;
for (int i = 1; i <= n; i++)
{
answer *= i;
}
MessageBox.Show(n + "! = " + answer);
This code should work out the factorial, by cycling through all the numbers from 1 to n, and multiplying it on each time.
Try to work out the Math.Pow() one yourself. A loop will be needed again, although the process will be slightly easier.
Good luck, and I hope this post reaches you in time!
Xav
#5
Posted 09 April 2008 - 05:24 AM
thanks for the tip.
seeing as no one in the class had even a slightest clue on how to solve it the teacher gave us a chance till tomorrow.
EDIT
it seems as I'm supposed to create a function replacement for Math.Pow
anyone got some good glossary on that?
seeing as no one in the class had even a slightest clue on how to solve it the teacher gave us a chance till tomorrow.
EDIT
it seems as I'm supposed to create a function replacement for Math.Pow
anyone got some good glossary on that?
Edited by Dori, 09 April 2008 - 06:56 AM.
#6
Posted 09 April 2008 - 09:20 AM
To create a function, just group the code you use for the calculation inside a method that returns a value. For example:
To use your new method MyPowMethod() with 5 cubed, you do this:
In the method, you need to replace my commented text with the code to work out the power.
private double MyPowMethod(double number, int power)
{
double answer = 1;
//Do code here to work out answer.
//Use variables "number" and "power" as the values.
//Then, place value in "answer".
//Finally, give the answer back to the user.
return answer;
}
To use your new method MyPowMethod() with 5 cubed, you do this:
double TheAnswer = MyPowMethod(5, 3)
In the method, you need to replace my commented text with the code to work out the power.


Sign In
Create Account

Back to top









