beginner wanting criticism
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: beginner wanting criticism

  1. #1
    Join Date
    Jul 2010
    Location
    Washington state
    Posts
    3

    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_at-i)-equation(number_diff_at))/(-i);
                  if(floor(result-neg_result)!=0)
                  {
                                                 i=i*i;
                                                 }
                                                 }while(floor(result-neg_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;
           }

  2. #2
    Join Date
    Jul 2002
    Posts
    2,521

    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 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 01:44 AM.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: beginner wanting criticism

    Quote Originally Posted by clintavian View Post
    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;
    Quote Originally Posted by clintavian View Post
    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

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,337

    Re: beginner wanting criticism

    Quote Originally Posted by clintavian View Post
    Code:
    #include "math.h"
    Perhaps you meant
    Code:
    #include <math.h>
    or better yet:

    Code:
    #include <cmath>
    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.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,197

    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.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,637

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center