Problem with mathematical function in program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newbprogrammer
    New Member
    • Feb 2008
    • 2

    Problem with mathematical function in program

    I just started programming in c++ and i tried to explore and do programs on my own... So i wanted to do a program that would help me in my daily school work ... a program to solve quadratic equations.. I wrote the code correctly but errors are still detected and i have no idea why.. almost gave up and banged my head on the wall .. so i would really appreciate help.. This is the code so far..
    [code=cpp]
    #include <cmath>
    #include <iostream>

    using namespace std;

    int main()
    {
    double x;
    double y;
    double z;

    cout<<"Please enter the coeff of (x square):";
    cin>> x;
    cout<<"Please enter the coeff of (x):";
    cin>> y;
    cout<<"Please enter the number without the unknown (x):";
    cin>> z;

    double b = (((y * y) - (4 * x * z)));
    double a = ((-y + (sqrt (b)) / (2 * x)));
    double c = ((-y - (sqrt (b)) / (2 * x)));

    cout<<"X = "<< a << "or" << c;
    cin.ignore();
    cin.get();
    }[/code]
    The answers i get arnt even close to the actual answers...

    Please tell me what is wrong.. PLEASE PLEASE PLEASE
    -------------------------------------------------------------------
    This should help in understanding what i am trying to do.....

    This program is supposed to solve equations like (3x^2 * -7x + 2)
    so.. Using the general formula to solve this equation we take the coeff of x^2 to be "x", coeff of x to be "y" and coeff of number without unknown x to be "z".
    Therefore x = 3
    y = -7
    z = 2
    The equation to find x = ((-y) + sqrt((y^2) - (4*x*z))) / 2*x)
    so... resulting to.. x = (7 + (sqrt (49 - 24)) / 6
    and.. x = (7 + 5)/6
    x = 2 //
    As you can see.. in my code.. double b should = 25...

    Any questions please ask.. i need help to solve this program.. I just really want to finish this program so please help
    Last edited by sicarie; Feb 17 '08, 01:46 PM. Reason: Please use code tags, they are there for a reason
  • vikasdev
    New Member
    • Jan 2008
    • 4

    #2
    In the statement
    [code=cpp]
    double a=(.......) .check the parenthesis u hav given.it shud be
    a=(-y+(sqrt(b)))/(2*x);[/code]
    now it will give correct output....
    and plz check that b(determinant)> =0,before proceeding further...
    Last edited by sicarie; Feb 17 '08, 01:46 PM. Reason: Again, code tags, instead of annoying bold

    Comment

    • newbprogrammer
      New Member
      • Feb 2008
      • 2

      #3
      Thanks alot....it works now but if determinant is less then 0 how do i make the code say answer not valid or possible.. i just have to add (double b > 0)??

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Have you looked at if statements? You might do something like:

        [CODE=cpp]// Calculate value of b...
        if (b < 0) {
        // Answer is an imaginary number.
        } else if (b == 0) {
        // Only one real answer
        } else {
        // Two real answers.
        }[/CODE]

        Comment

        Working...