Menu

help using c++ to solve simple equations

Will P
2008-06-18
2012-09-26
  • Will P

    Will P - 2008-06-18

    i figured C++ would be able to print the result of an equation for whatever variable you want it to solve for by using the following code(i wrote it up from scratch so of course it's wrong lol):

    include <iostream>

    using namespace std;

    int main ()
    {
    double x;
    x * 3 = 12;

    cout&lt;&lt;&quot;Solve for x: 3x = 12.\n&quot;;
    cout&lt;&lt;&quot;x = &quot;&lt;&lt; x &lt;&lt; endl;
    
    system(&quot;PAUSE&quot;);
    return 0;
    

    }

    i get an error returned on statement x * 3 = 12; about a non integer i think. any ideas how to change this and make it right? or can C++ not solve equations the way i thought it could?

     
    • BiT

      BiT - 2008-06-18

      it seems that you are not to familiar with declaring variables and since you said you are teaching yourself I would suggest reading http://newdata.box.sk/bx/c/. This is not by any means the greatest resource but will help you understand some of the basics.

      you have
      double x; //this is a floating point variable and looking your problem I would suggest int data type

      x has not been initialized and has no value.

      if you want x to equal 4 then you need to assign the that value to x

      int x=4;

      or int x;
      x=4;

      now the math would be

      x = x*3;
      which would make x=12;

      you could also do it

      x*=3;

      again at this point I think you need to do a little bit of reading.

      BiT

       
    • Will P

      Will P - 2008-06-18

      thanks a lot cpns, i actually did know how to do what BiT explained(as i have read quite a few e-books on beginner C++ programming) but i was just wondering if C++ could solve the problems for you. also, i assigned x to double instead of int because i planned on trying other equations besides for that one afterwards. i appreciate it =P

       
    • cpns

      cpns - 2008-06-18

      I am not sure BiT has understood what you were asking. Everything he told you is correct, but does not really answer you question or correct your misconception of what kind of language C++ is.

      > x * 3 = 12;

      C++ will not do that (inference). It can evaluate expressions, but it cannot solve equations without you coding the algorithm for its solution. For example, in this case you rearrange the equation so that it is an assignment expression:

      x = 12 / 3 ;

      The point being that the left hand side of an assignment expression must be an L-Value ( http://en.wikipedia.org/wiki/Value_%28computer_science%29 ), i.e. it must have an address. x * 3 is not an L-Value and cannot be assigned the value 12. It only has the value 12 if x happens to be 4 already.

      This is probably not what you really wanted, and more complex equations, such as simultaneous, quadratic, or polynomial equations are more complex. Typically though you need a different algorithm for each different type of equation, or a numerical analysis library such as

      If you want a language more suited to this sort of thing, then you should mook at somethoing like MatLab. There are free clones of MatLab, such as Octave, and FreeMat. Here are some Octave examples: http://www.math.uic.edu/~hanson/Octave/OctaveNonlinearEG.html . Start here perhaps: http://en.wikipedia.org/wiki/Comparison_of_numerical_analysis_software or http://en.wikipedia.org/wiki/List_of_numerical_analysis_software

      If you want to do it all in C++, then using a third-party numerical analysis library may be your best way forward.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.