Jump to content

Need help with programming assignment

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Dori

Dori

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
You'll need to find out how factorials and exponentiation work. You can find articles on both topics on Wikipedia: Factorial and Exponentiation.

#3
Dori

Dori

    Newbie

  • Members
  • Pip
  • 3 posts
i know how they work, well in for the most part.

i just don't have an idea how to solve this

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
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:

//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
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
Dori

Dori

    Newbie

  • Members
  • Pip
  • 3 posts
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?

Edited by Dori, 09 April 2008 - 06:56 AM.


#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
To create a function, just group the code you use for the calculation inside a method that returns a value. For example:

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.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums