I'm teaching myself c++ and giving myself random stuff to program. This program works, mostly, but I know for a fact that it could have been done much much better. This program is basically Newton's Method http://en.wikipedia.org/wiki/Newtons_method for polynomials. I'm mainly seeking out criticism on this code because I know it isn't the best quality. I know I shouldn't use global variables and I should do comments, but ya...
This program accepts any user input, without checking it. Does this algorithm have some input restrictions (for example, some variable must be non-negative)? In the case there are such restrictions, you need to check the values typed by user.
If you write in C++, why don't you use classes? Think about class that has a, b... f member variables, and linearsolver, equation.. methods. Main function creates such class instance, fills its members, and calls newtons_method function.
Local variables like initialguess, slope, result have pretty informative names. However, general parameters have short and useless names like x, y.
In some places indentation looks a bit strange. Visual Studio "Format Selected Text" function is a good way to fix this.
Generally, this program is readable and clear.
Last edited by Alex F; July 17th, 2010 at 12:44 AM.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
At least wrap some parentheses around the various parts of it. That will make it clearer to the reader exactly what order the calculations are being performed in, and probably help you avoid mistakes when you don't get the calculation order right.
Bookmarks