return to tranceaddict TranceAddict Forums Archive > Main Forums > Chill Out Room

Pages: [1] 2 
C++ Hw
View this Thread in Original format
Trancealot
anyone of you a coder out there can help me..

I am good but this one is boggoling my mind???

Write a program that uses Newton's method to compute the nth root of a number x. Newton's method requires an initial "seed"- a guess at the answer to start the iterative algorithm. The user is asked to supply an initial guess at the solution. If the user enters -1 as the initial guess, the program is to provide an initial solution. The user should be repeatedly prompted to enter a positive integer n greater than or equal to 2 and a positive double value x. The user should also be asked to enter the desired accuracy of the solution as the number of decimal places that the solution is accurate to (between 0 and 15). The program should print a table showing the iteration number, current estimate of the solution, and the error (the absolute value of the difference between the last estimate and the current estimate). The table values should be left aligned in columns and the solution estimate and error should be printed in fixed-point notation using to the number of decimal places specified by the user. Make sure that your program checks for division by zero and exits gracefully.

This program computes the nth root of x using Newton's Method.
Input n: 3
Input x: 1001
Input an initial guess at the root, or -1 if unsure: -1
How many decimal places (0-15) should the solution be computed to: 7
Iteration Root Error
1 222.4474414 111.2192252
2 148.3050374 74.1424041
3 98.8851955 49.4198419
4 65.9575869 32.9276086
5 44.0484225 21.9091644
6 29.5375847 14.5108377
7 20.0741628 9.4634220
8 14.2107897 5.8633731
9 11.1261122 3.0846775
10 10.1128251 1.0132871
11 10.0045134 0.1083116
12 10.0033324 0.0011811
13 10.0033322 0.0000001
14 10.0033322 0.0000000
drizzt81
so what exactly do u want? a completely written program or what?
Trancealot
I supposed so...
Mail Man
quote:
Originally posted by Trancealot
I supposed so...


u lazy bum lol

im starting C++ in December.. is it hard?
Trancealot
well look at this problem..the last program I messed up so its getting harder
drizzt81
quote:
Originally posted by Trancealot
I supposed so...


well, while this sounds liks a cool problem, i don't have the time to write a proggy that does this stuff right now. Let me give you some pointers (har har, get the pun!!):

start out simple. Write the functions, which are going to handle your I/O first. See that you get the information correctly from the user.

Next see if you have write a function, let's call it
code:
int displayLine(int iTry, int iRoot, int iError)

which takes three ints and returns 0 if it executed sucessful. this function, uses the three integers provided to create one line of the array, which u are supposed to print.

If you are not getting many point for how teh array looks, u might just want to do a cout on this stuff and dump it to the screen before worrying about formatting.

one good thing is that there is a cout command, which let's u set the number of decimal places, which are displayed.

After you got your I/O sorted, it is time to crank out some Math book and figure out how the newton method is done. I don't know this off-hand (I remeber newton-raphson method for finding the zero's of functions).

From the problem statement, i assume that it will be an interative algorithm. You can then write a function
code:
int Newton(int iSeed, int& iRoot, int& iError)

which will do one iteration of your algorithm and put the new root into iRoot, the new error into iError and returns 0 if it is successful.

Now you should have everything u need to write a little main loop, which uses these function stubs to ask for input, compute the rows, till the error == 0 and prints out one row at a time.

I hope that this helps a bit
drizzt81
quote:
Originally posted by Mail Man


u lazy bum lol

im starting C++ in December.. is it hard?


not at all. once u know where to go to find functions that are in the libraries, you are golden.
TiestoInTheMix
hehe, here's how i programmed it in basic for my TI-83+ calculator:

:ClrHome
:Disp "ENTER EQUATION"
:Disp "IN QUOTES"
:Prompts Str0
:Str0->Y1
:d(Str0, "X")->Y2 //takes a derivative of Y1 and puts it into Y2
:Lbl 0
:ClrHome
:Disp "ENTER A GUESS"
:Prompt G
:While (Y1(G)>0.000000001)
:G-(Y1(G)/Y2(G))->G //Newton's formula
:Disp G
:End
:Disp "ANSWER IS:", G
:Pause
:Menu("CHOOSE ACTION", "CHANGE GUESS", 0, "QUIT", 9)
:Lbl 9
:""->Y1
:""->Y2


works like a charm
hehe
:gsmile:
ampburner
I like how
code:
:Disp
shows up as
quote:
isp


TiestoInTheMix
quote:
Originally posted by ampburner
I like how
code:
:Disp
shows up as



lol HEHE :p

someone once said Perl is cool because it uses more emoticons than all other languages combined... i wonder how that would look on here :p

AnotherWay83
quote:
Originally posted by TiestoInTheMix


lol HEHE :p

someone once said Perl is cool because it uses more emoticons than all other languages combined... i wonder how that would look on here :p


Perl...heh, gotta love that language

it's so in high level, i've actually seen entire poems written in it, and they actually get parsed perfectly fine

i will post 1 if u want.
PatMcGroin
ATLEAST YOUR NOT DOING C!!!!!!!

ing a! i took c++ 2 years ago, and now i'm taking c....god so horrible! i ing miss cin soooo much!

c is all harder with functions too, grrr

look at my last program i had to do:
http://www.cs.fsu.edu/~cgs3408/html/project5.htm

took me about 2 hours, but of course, with some help :D
CLICK TO RETURN TO TOP OF PAGE
Pages: [1] 2 
Privacy Statement