Hey everyone,

I have a question on one of my homework problems. I'm in basic structured programming; C++.

The question states the following:

The root of the quadratic equation ax^2 + bx + c = 0, a ≠ 0 are given by the following formula:

-b +/- √b^2 – 4ac
2a

In this formula, the term b^2 – 4ac = 0, then the equation has a single (repeated) root. If b^2 – 4ac > 0, the equation has two real roots. If b^2 – 4ac < 0, the equation has two complex roots. Write a program that prompts the user to input the value of a (the coefficient of x^2), b (the coeeficient of x), and c (the constant term), and outputs the type of roots of the equation. Furthermore, if b^2 – 4ac >= 0, the program should output the roots of the quadratic equation.


(Hint: Use the function pow from the header file cmath to calculate the square root.)

**********************************************


I would post my starting point but it's basically just my header files and void main()

The only header files we've learned so far are iostream, iomanip, cmath, fstream and this problem is out of the control structures chapter.

Thanks for any help!!

:mrgreen:

I think I've gotten the first part, but i can't figure out how to output if it's >=

#include <iostream>
#include <cmath>

using namespace std;

void main()
{
    double a, b, c, x;

    cout << "Enter the value of a:  " << flush;
    cin >> a;
    cout << "Enter the value of b:  " << flush;
    cin >> b;
    cout << "Enter the value of c:  " << flush;
    cin >> c;
    cout << endl;

    x = pow(b,2) - 4*a*c;

    if (x == 0)
        cout << "The equation has a single, repeated root." << endl;
    else if (x > 0)
        cout << "The equation has two real roots." << endl;
    else if (x < 0)
        cout << "The equation has two complex roots." << endl;
    else
        cout << "!! INVALID INPUT !!" << endl;

}

ok jus try out this code>>>>;;;;;;

#include<iostream>
#inculde<cmath>
using namespace std;
int main()
{float a,b,c;
cout<<"enter values for a,b,c\na: ";
cin>>a;
cout<<"b: ";
cin>>b;
cout<<"c: ";
cin>>c;
 
if (pow(b,2)>=4*a*c){
      cout<<"the roots are"<<endl;
      cout<<(-b+pow(b*b-4*a*c,1/2.0))/(2*a);
      cout<<(-b-pow(b*b-4*a*c,1/2.0))/(2*a);
}
else
      cout<<"you have complex roots";
     
return 0;
}

hm, lots of build errors... i'll let you know what happens

thanks

1. Don't use void main. Use int main
2. Keep using double that ok.
3. Split the quad equation into parts. Do each part at a time.
4. Don't know how to use pow. Check the following:

http://cplusplus.com/reference/clibrary/cmath/pow.html

Thanks,

I started out using int main but we haven't been using any returns so i quit until i need it again. I guess i should get into a habbit of using it though.

I'm really not even sure what the question is asking for? Maybe if I knew that I would know where else I have to go and what I'm doing wrong.

Member Avatar for iamthwee

>I started out using int main but we haven't been using any returns so i quit until i need it again.

There is no choice in it. Always use int main.

>>(Hint: Use the function pow from the header file cmath to calculate the square root.)

That is in error. the pow() function raises X to the power of Y. you need to use the sqrt() function to get the square root of a number.

In the equation
(-b +/- √b^2 – 4ac ) / 2a


first find the value of b^2-4ac using the pow() function to get b^2. Then use sqrt() to get the square root of that result. That gives you
(-b +/- sqrt(result)) / 2a where result is the previous computation.

Now you need to make two computations to get the findl result
(-b + sqrt(result)) / 2a
and
(-b - sqrt(result)) / 2a

Note1: that when the term b^2 – 4ac = 0, then the equation has a single (repeated) root. -- which means you do only one final computation, not two as described above because the value of result will be 0 making the result of both final computations the same. (-b + result)/2a is reduced to -b/2a

Note2: when b^2-4ac < 0 you can not do any of the other computations because the square root of a negative number is a complex number, not a real number. Most c programs can not deal with complex numbers, althrough there are advanced math libraries which can. But you are not at that level, so don't worry about it. I would just display a message that the root results in a complex number, and the program should quit.

>>(Hint: Use the function pow from the header file cmath to calculate the square root.)

That is in error. the pow() function raises X to the power of Y. you need to use the sqrt() function to get the square root of a number.

In the equation
(-b +/- √b^2 – 4ac ) / 2a


first find the value of b^2-4ac using the pow() function to get b^2. Then use sqrt() to get the square root of that result. That gives you
(-b +/- sqrt(result)) / 2a where result is the previous computation.

Now you need to make two computations to get the findl result
(-b + sqrt(result)) / 2a
and
(-b - sqrt(result)) / 2a

Note1: that when the term b^2 – 4ac = 0, then the equation has a single (repeated) root. -- which means you do only one final computation, not two as described above because the value of result will be 0 making the result of both final computations the same. (-b + result)/2a is reduced to -b/2a

Note2: when b^2-4ac < 0 you can not do any of the other computations because the square root of a negative number is a complex number, not a real number. Most c programs can not deal with complex numbers, althrough there are advanced math libraries which can. But you are not at that level, so don't worry about it. I would just display a message that the root results in a complex number, and the program should quit.

wow thank you soooo much. I am on my way out to spend time with the lady, but can't wait to get back and try again.


I'll try with int main () from now on too.... thanks guys! I'll let you know what happpens

I am on my way out to spend time with the lady, but can't wait to get back and try again.

Yes, certainly. Glad you keep your priorities straight:cheesy:

Here's what I got... Look right?

//header files
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    //declared variables
    double a, b, c, x, result1, result2;

    //prompt for input; initialize variables
    cout << "Enter value of a:  " << flush;
    cin >> a;
    cout << "\nEnter value of b:  " << flush;
    cin >> b;
    cout << "\nEnter value of c:  " << flush;
    cin >> c;
    cout << endl;

    //calculate pow(b,2)-4ac
    x = pow(b,2) - 4 * a * c;

    //set conditions and output roots
    if (x == 0)
    {
        result1 = ( -b + sqrt(x) ) / (2 * a);
        cout << "Two repeated roots of:  " << result1 << endl;
    }
    if (x < 0)
        cout << "Two complex roots; (-) under the radical:  " << endl;
    if (x > 0)
    {
        result1 = ( -b + sqrt(x) ) / (2 * a);
        result2 = ( -b - sqrt(x) ) / (2 * a);
        cout << "Two real roots of :  " << result1 << " and " << result2 << endl;
    }
}

close: When result is 0 the equation reduces to -b/2a because the square root of 0 is 0.

if (x == 0)
    {
        result1 = -b / (2 * a);
        cout << "Two repeated roots of:  " << result1 << endl;
    }

close: When result is 0 the equation reduces to -b/2a because the square root of 0 is 0.

if (x == 0)
    {
        result1 = -b / (2 * a);
        cout << "Two repeated roots of:  " << result1 << endl;
    }

ah ok! fixed it... thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.