Charas-Project

  • Home
  • Help
  • Search
  • Calendar
  • Login
  • Register
*
Please login or register.

Login with username, password and session length
 

News:

Click here to join us on IRC (#charas on irc.freenode.net)!



  • Charas-Project »
  • Off-Topic »
  • Archive »
  • Really Old Stuff »
  • General programming »
  • Problems
« previous next »
  • Print
Pages: [1]

Author Topic: Problems  (Read 3811 times)

Offline SonicChaos7

  • Time to crack that Eggman wide open
  • Zealot
  • *
  • Posts: 628
Problems
« on: October 20, 2006, 09:59:43 PM »
Ok, I'm writing a program for my computer science class here in college and I am stuck. Right now, we're writing functions and stuff. My main problems are writing equations to find out the power and square root. I could use cmath, but we're not allowed for those two. Anyway, the problem is like so:

Power Function:

Write a function that will calculate X^Y.


squareRoot:

This function will calculate the square root of a number using Newton's method with 25 iterations.

How would I type this to make it work?

BTW, Newton's method looks like:

guess = ((N/guess) + guess)/2
Logged

Offline DarkFlood2

  • Back from the dead.
  • Zealot
  • *
  • Posts: 778
(No subject)
« Reply #1 on: October 21, 2006, 02:53:56 AM »
I remember this from my class in Java last year, now the program may not be similar, but you should just need to have the input of one variable.. With your variable for the number, you place it into the function and run that in a loop 25 times. It should look something like this:

(Remember, this is for Java so it may not be the same, but its close)

  for(int i = 1; i <= 25 ;i++)
  {
      SqrNumber = (1 + Number/1)/2;
  }

That should allow you to get the square root of a number.

As for powers, you take your basic number ( b ) and you take yopur exponent ( e ) and run it in a loop.

So it would be like this:

  integer temp = b(the number you want raised);
  for(int i = 0; i < e(your exponent); i++)
  {
      b = b * temp;
  }

It will go until it reaches the maximum times that you need to raise the number. It basically multiplies the number by itself each run through the loop, and it stops once the maximum amount to be raised is reached.
Logged
Zeex - Level 70 Undead Warlock, Burning Legion Server.

Offline Osmose

  • So freakin' inactive
  • Royal
  • *
  • Posts: 3,041
(No subject)
« Reply #2 on: October 21, 2006, 03:00:30 AM »
I tried helping him earlier, and the real problem is that whatever he has to submit it to won't allow him to define any new variables - he can only work with the two it takes as input. And since C++ is option explicit, any variable is required to be declared.

I still say there's something wrong with whatever his teacher is making him submit with.
Logged
Hrm.

Offline DarkFlood2

  • Back from the dead.
  • Zealot
  • *
  • Posts: 778
(No subject)
« Reply #3 on: October 21, 2006, 03:10:48 AM »
Oh if he only gets 2 variables, then couldn't he use a while loop for his iterator and the exponents? Or are there no while loops in C++?

Either way, his teacher is a moron for not allowing even a simple temporary variable, or iterator variable. I KNOW that the square root can be done without an iterator, but with as much programming experience as I have had, you're gonna need a whole seperate object class if you want to do the exponent one.
Logged
Zeex - Level 70 Undead Warlock, Burning Legion Server.

Offline Osmose

  • So freakin' inactive
  • Royal
  • *
  • Posts: 3,041
(No subject)
« Reply #4 on: October 21, 2006, 03:39:25 AM »
I've barely done anything more than cout and cin and basic loops in C++, but I'm fairly sure even while and for loops require the iterator to be declared.
Logged
Hrm.

Offline SonicChaos7

  • Time to crack that Eggman wide open
  • Zealot
  • *
  • Posts: 628
(No subject)
« Reply #5 on: October 21, 2006, 05:11:01 AM »
Well, it's too late now. : It was due at midnight. Oh well. At leats I got most of it done.
Logged

Offline DarkFlood2

  • Back from the dead.
  • Zealot
  • *
  • Posts: 778
(No subject)
« Reply #6 on: October 21, 2006, 01:01:23 PM »
Oh thats too bad, but a while loop (at least in java) will continuousley go until it reaches something.
Logged
Zeex - Level 70 Undead Warlock, Burning Legion Server.

  • Print
Pages: [1]
« previous next »
  • Charas-Project »
  • Off-Topic »
  • Archive »
  • Really Old Stuff »
  • General programming »
  • Problems
 

  • SMF 2.0.10 | SMF © 2015, Simple Machines
  • XHTML
  • 2O11
  • RSS
  • WAP2
  • Simple Machines Forum