Prompt the user for 3 numbers a b and c as part of the quadr

Prompt the user for 3 numbers a, b, and c as part of the quadratic equation ax^2 + bx + c = 0. Use the quadratic equation formula from the notes to determine if there are no real roots, one real root, or two real roots. Remember divide by 0.0 is undefined. You must create a function for the quadratic formula. Your function should not allow division by 0.0, and should not allow the computation of a squareroot of a negative number. You function should return true if there are any roots, and false otherwise. If there are roots your function also needs to pass those root back to the main block for output. Do not use global variables for anything. Output from your program might look like: Enter a: 2.0 Enter b: 5.0 Enter c: -3.0 There are two real roots and they are -3.0 and 0.5 Enter a: 0.0 Enter b: 5.0 Enter c: -3.0 There are no real roots Enter a: 1.0 Enter b: -3.0 Enter c: 0.0 There are two real roots and they are 3.0 and 0.0

Solution

#include <iostream>
#include <cmath>
using namespace std;
bool calculateQuadric();

int main() {
   bool val;
   val=calculateQuadric();
return 0;
}

bool calculateQuadric()
{
   float a, b, c, determinant;
   float root1, root2;
cout<<\"Enter a: \";
cin>>a;
   cout<<\"Enter b: \";
cin>>b;
   cout<<\"Enter c: \";
cin>>c;
determinant = b*b - 4*a*c;
  
   if(a==0)
   {  
       cout<<\"There are no real roots\ \";
       return false;
   }
   else
   {
       if (determinant > 0) {
               root1 = (-b + sqrt(determinant)) / (2*a);
               root2 = (-b - sqrt(determinant)) / (2*a);
               cout <<\"\ There are two real roots and they are \"<<root1<<\" and \"<<root2<<endl;  
               return true;          
   }
  
   else if (determinant == 0) {
           root1 = (-b + sqrt(determinant)) / (2*a);
               cout <<\"\ There is 1 real root and it is \"<<root1<<endl;
               return true;
       }
          
       else {
   //cout << \"There are two complex roots as the discriminant is negative\" << endl;
   return false;
   }
   }
}

 Prompt the user for 3 numbers a, b, and c as part of the quadratic equation ax^2 + bx + c = 0. Use the quadratic equation formula from the notes to determine i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site