TranceAddict Forums (www.tranceaddict.com/forums)
- Chill Out Room
-- C++ Hw
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
so what exactly do u want? a completely written program or what?
I supposed so...
| quote: |
| Originally posted by Trancealot I supposed so... |
well look at this problem..the last program I messed up so its getting harder
| quote: |
| Originally posted by Trancealot I supposed so... |
code:
int displayLine(int iTry, int iRoot, int iError)
code:
int Newton(int iSeed, int& iRoot, int& iError)
| quote: |
| Originally posted by Mail Man u lazy bum lol im starting C++ in December.. is it hard? |
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

I like how
shows up ascode:
:Disp
| quote: |
isp |
| quote: |
| Originally posted by ampburner I like how shows up ascode: |

| 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 |
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 
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::
| 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. |
| 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:: |
| 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 |
#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<
}
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!!
it works on the complier but does not output anything...something wrong with the due loop..
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...
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.
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...
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.