473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Loops

Using the Newton-Raphson method, find the two roots of the equation 3x^2 + 2x -2=0. (Hint: There is one positive root and one negative root.)

(x+1) = x - (3x^2 +2x -2) / (6x +2) This is the equation given for the Newton-Raphson method.
Expand|Select|Wrap|Line Numbers
  1. Head File 
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5. #include "Newton.cpp"
  6. #include "Euler.cpp"
  7. #include "displaymenu.cpp"  
  8.  
  9. Main Program File
  10.  
  11. #include "head.h"
  12.  
  13. int main(void)    //Note…some code will be removed after buffering test.
  14. {
  15. //Declare all variables here that are needed to call/receive values for Exercises
  16. double guess=1.0, root=0.0;    //variables needed for Exercise 6.a. in case A.
  17. double differs=0.000001, eApprox=0.0;    //variables needed for case B.
  18. char choice = 81;  // 81 is decimal code for capital Q; 113 is lower case q
  19. int enterkey;
  20. do {
  21.     //Your displaymenu() function will replace the next 6 steps 
  22.      cout <<"Call displaymenu and enter an a  ";//desplaymenu()   
  23.         choice = toupper(getchar( ));  enterkey = getchar( );
  24.         cout << choice <<":"<< enterkey << endl;
  25. switch (choice)    // a decision structure to select only one case out of many
  26. {
  27. case 'A':    
  28.     cout<<"Call Newton(given guess), returns and saves root\n ";    // Exercise 6.a.
  29.     root=Newton(guess);
  30.     cout<<"Newton's root is:  "<<root;
  31. break;
  32. case 'B':    
  33.     cout<<"Call Euler(given differs), returns and saves eApprox\n";     // Exercise 3.
  34.     cout<<" differs is set to:  "<<differs<<endl;
  35.     //eApprox = Euler(differs);
  36.     cout<<" eApproximate is:  "<<eApprox<<endl;
  37. break;            
  38. default:    //input data validation intercepted here
  39. if (choice != 'Q')    // a decision structure to skip or enter next {block}
  40. {cout << "\nIncorrect Choice: Enter (A,B, or Q as valid)\n";
  41. }//endif
  42. } //  end switch
  43.  
  44. } while (choice != 'Q');  //end do-while
  45.  
  46. cout << " Fini\n";
  47.  
  48.  
  49. system("pause");
  50. return 0;
  51. } // end main
  52.  
  53.  
  54. Newton File  My newton does not work  at the moment.
  55. double Newton(double x)   // returns y = f(x)
  56. {
  57.     double root;
  58. int count;
  59.     for (count = 1; count < 10; count++);
  60.     {
  61.  
  62.   cout<<" The x value of Newton is:  "<<x<<endl;
  63.   cout<<"The root: "<<root<<endl;
  64.   root=x-((3(pow((x),2))+2x-2)/(6x+2));
  65.     }//end for
  66.    return root;
  67.  
  68. } //end Newton
  69.  
I still didnt make the Euler file or displaymenu file.
Oct 15 '08 #1
2 1293
r035198x
13,262 8TB
..
I still didnt make the Euler file or displaymenu file.
Where is your displaymenu function/file?
Oct 16 '08 #2
JosAH
11,448 Expert 8TB
My newton does not work at the moment.

Expand|Select|Wrap|Line Numbers
  1. double Newton(double x)   // returns y = f(x)
  2. {
  3.     double root;
  4. int count;
  5.     for (count = 1; count < 10; count++);
  6.     {
  7.  
  8.   cout<<" The x value of Newton is:  "<<x<<endl;
  9.   cout<<"The root: "<<root<<endl;
  10.   root=x-((3(pow((x),2))+2x-2)/(6x+2));
  11.     }//end for
  12.    return root;
  13.  
  14. } //end Newton
  15.  
Of course it doesn't work: you assign the same value to your variable 'root'
over and over again. On top of that: 2x doesn't mean 2*x in C or C++; those
languages are a bit stupid, they need an explicit multiplication sign. You have
to fix your notation at several locations.

kind regards,

Jos
Oct 16 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
4
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) {...
17
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.