
July 16th, 2010, 07:16 PM
#1
beginner wanting criticism
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...
Code:
#include <iostream>
#include "math.h"
using namespace std;
double a;
double b;
double c;
double d;
double e;
double f;
double linearsolver(double slope, double x_coordinate, double y_coordinate)
{
double solution = 0  y_coordinate;
solution = solution/slope;
solution = solution + x_coordinate;
return solution;
}
double equation(double x)
{
double result;
result=a*x*x*x*x*x+b*x*x*x*x+c*x*x*x+d*x*x+e*x+f;
return result;
}
double differentiation(double number_diff_at)
{
double i=0.001;
double result,neg_result;
do
{
result=(equation(number_diff_at+i)equation(number_diff_at)/i;
neg_result=(equation(number_diff_ati)equation(number_diff_at))/(i);
if(floor(resultneg_result)!=0)
{
i=i*i;
}
}while(floor(resultneg_result)!=0);
double deriv_result=(result+neg_result)/2;
return deriv_result;
}
double newtons_method(double initialguess, int iterations)
{
double x=initialguess;
double y,slope,result;
int n;
for(n=iterations;n!=0;n)
{
slope=differentiation(x);
y=equation(x);
result=linearsolver(slope,x,y);
x=result;
}
return result;
}
int main()
{
double initialguess, result;
int n;
cout<<"a: ";
cin>>a;
cout<<"b: ";
cin>>b;
cout<<"c: ";
cin>>c;
cout<<"d: ";
cin>>d;
cout<<"e: ";
cin>>e;
cout<<"f: ";
cin>>f;
cout<<"\ninitial guess: ";
cin>>initialguess;
cout<<"iterations: ";
cin>>n;
result=newtons_method(initialguess,n);
cout<<"\nsolution is: "<<result<<"\n";
cin>>n;
return 0;
}

July 17th, 2010, 12:40 AM
#2
Re: beginner wanting criticism
This program accepts any user input, without checking it. Does this algorithm have some input restrictions (for example, some variable must be nonnegative)? 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.

July 18th, 2010, 02:01 PM
#3
Re: beginner wanting criticism
Originally Posted by clintavian
Code:
double linearsolver(double slope, double x_coordinate, double y_coordinate)
{
double solution = 0  y_coordinate;
solution = solution/slope;
solution = solution + x_coordinate;
return solution;
}
This is unacceptably verbose. At the least you should write.
Code:
double solution = y_coordinate;
solution /= slope;
solution += x_coordinate;
return solution;
However, I would just write a single line.
Code:
return y_coordinate / slope + x_coordinate;
Originally Posted by clintavian
Code:
double equation(double x)
{
double result;
result=a*x*x*x*x*x+b*x*x*x*x+c*x*x*x+d*x*x+e*x+f;
return result;
}
There's no need for a local variable if you write it like this. But you may be able to improve performance by changing it to
Code:
double x2 = x * x;
double x3 = x * x2;
double x4 = x * x3;
double x5 = x * x4;
return a * x5 + b * x4 + ... = f;
Note the use of spaces between operators to make the code easier to read.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it.  P. D. Ouspensky

July 19th, 2010, 01:16 AM
#4
Re: beginner wanting criticism
Originally Posted by clintavian
Perhaps you meant
or better yet:
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 16.
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.

July 19th, 2010, 07:35 AM
#5
Re: beginner wanting criticism
I don't think I'd write something like this.
result=a*x*x*x*x*x+b*x*x*x*x+c*x*x*x+d*x*x+e*x+f;
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.

July 19th, 2010, 09:39 AM
#6
Re: beginner wanting criticism
And for any mathematicians out there ...
Code:
double equation_horner(double x)
{
return f + x * (e + x * (d + x * (c + x * (b + x*a))));
}
sorry.
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
