TranceAddict Forums

TranceAddict Forums (www.tranceaddict.com/forums)
- Chill Out Room
-- C++ Hw


Posted by Trancealot on Oct-06-2002 04:49:

C++ Hw

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


Posted by drizzt81 on Oct-06-2002 10:33:

so what exactly do u want? a completely written program or what?


Posted by Trancealot on Oct-06-2002 14:49:

I supposed so...


Posted by Mail Man on Oct-06-2002 14:55:

quote:
Originally posted by Trancealot
I supposed so...


u lazy bum lol

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


Posted by Trancealot on Oct-06-2002 15:10:

well look at this problem..the last program I messed up so its getting harder


Posted by drizzt81 on Oct-06-2002 15:32:

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


Posted by drizzt81 on Oct-06-2002 15:43:

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.


Posted by whiskers on Oct-06-2002 20:26:

hehe, here's how i programmed it in basic for my TI-83+ calculator:

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


works like a charm
hehe


Posted by ampburner on Oct-06-2002 21:55:

I like how

code:
:Disp
shows up as
quote:
isp



Posted by whiskers on Oct-06-2002 23:39:

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



lol HEHE

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


Posted by AnotherWay83 on Oct-07-2002 06:44:

quote:
Originally posted by TiestoInTheMix


lol HEHE

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


Perl...heh, gotta love that language

it's so fuckin 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.


Posted by PatMcGroin on Oct-07-2002 13:57:

ATLEAST YOUR NOT DOING C!!!!!!!

fucking a! i took c++ 2 years ago, and now i'm taking c....god so horrible! i fucking 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


Posted by Kirby on Oct-07-2002 14:10:

I'm too lazy to write an entire program solving this problem right now but I did it a few times before for my math hw (prgm in C++) so if you come up with a solution to your problem, and you have anything that doesn't work about it, post it up on the thread and I'd be happy to help you with something that doesn't work... I <3 C++... !!! ::wishes I was taking computer science this term::


Posted by whiskers on Oct-07-2002 20:54:

quote:
Originally posted by AnotherWay83


Perl...heh, gotta love that language

it's so fuckin 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.


yes! post it! i wanna see it!

btw, what's more high level, pearl or c++?


Posted by drizzt81 on Oct-08-2002 02:06:

quote:
Originally posted by Kirby
I'm too lazy to write an entire program solving this problem right now but I did it a few times before for my math hw (prgm in C++) so if you come up with a solution to your problem, and you have anything that doesn't work about it, post it up on the thread and I'd be happy to help you with something that doesn't work... I <3 C++... !!! ::wishes I was taking computer science this term::


i love C++ too.. unfortunately I am done with all my CS classes too..
now i am in love with assembly


Posted by drizzt81 on Oct-08-2002 02:07:

quote:
Originally posted by PatMcGroin
ATLEAST YOUR NOT DOING C!!!!!!!

fucking a! i took c++ 2 years ago, and now i'm taking c....god so horrible! i fucking 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


C is simple. All I need is one book that explains what u get in the standard libraries.. then u will own that shit!

i like C. C++ is more fun, i admit.. Java is boring, nothing 'nifty', no pointers etc


Posted by Trancealot on Oct-08-2002 03:46:

#include
#include
#include
#include
using namespace ::std;
int main()
{
double x,n,r,f,fprime,error,a,b;
int d,iteration;

cout<<"input n: ";
cin>>n;
cout<<"input x: ";
cin>>x;
cout<<"input an initial guess at root, -1 if unsure: ";
cin>>r;
cout<<"input amount of decimal values: ";
cin>>d;
do{
if(r!=-1)
{
r=r;
}
else if(r==-1)
{
srand(r);
}
f=pow(r,n)-x;
fprime=n*pow(r,n-1);
a=x-(f/fprime);
b=a;
}while(a!=.01);
for(b=a;b=.01;b--)
for(iteration=1;iteration<=15;iteration++)

cout< cout< < return 0;
}


this is all the code I have produced...I am having major trouble in the do loop....that is what is messing me up....I need to produce when the user inputs all the values newton's method........as it does over and over in the do loop the program should print the iteration #,current estimate of the solution, and error(absolute of last estimate - current estimate) when it hits 0...this is due tomorrow please help!!


Posted by Trancealot on Oct-08-2002 03:48:

it works on the complier but does not output anything...something wrong with the due loop..


Posted by tranceaholic on Oct-08-2002 03:59:

try to debug it..maybe by puting a cout in the loop to see what happens and if the loop is executing correctly or not...


Posted by Trancealot on Oct-08-2002 04:31:

seems like I need to make a recursive loop of the
f=pow(r,n)-x;
fprime=n*pow(r,n-1);
a=x-(f/fprime);

because everytime it solves for A it needs to be plugged back inside the a to get a new a.


Posted by drizzt81 on Oct-08-2002 11:14:

mah friend, i gave u like 4 function definitions up there? WHY the HECK??? are you writing spaghetti code. This program would be so easy, if you used a 'top-down' approach to it, but u seem to hate writing functions. And yes, it is a GREAT application for recursive functions, if you want it to be (the CPU cache likes recursion, u know?)

anyhow, why are you computing a new random seed in the do loop?

why not use a while loop?

oh and 'error' is a reserved word, u shouldn't be using that as a variable...


Posted by Trancealot on Oct-08-2002 13:32:

my problem was I did not actually make algo's to calculate the table and that I have done by actually thinking finally....the newton's method is easy because it is a formula..but the hard part is making the repating algo's of the formula in the loop of going to .01 and subtracting the calculated value - current value....I will post up the working code today because I have half of it working good....amd LAB to finish it in



Powered by: vBulletin
Copyright © 2000-2021, Jelsoft Enterprises Ltd.