Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write and debug a program that solves for x in the equation a*x^2 + b*x + c = 0.

ID: 3666296 • Letter: W

Question

Write and debug a program that solves for x in the equation a*x^2 + b*x + c = 0. The user should enter values for a, b, and c (use double data types). There are five possible outcomes for x:
        1. if a = 0 and b = 0, no solution can be found for x
        2. if a = 0 and b != 0, x = -c/b
        3. if a != 0 and (b^2 - 4*a*c) = 0, x1 = x2 = -b/(2*a)
        4. if a != 0 and (b^2 - 4*a*c) > 0, x1 = (-b + sqrt(b^2 - 4*a*c))/(2*a)
                                             x2 = (-b - sqrt(b^2 - 4*a*c))/(2*a)
        5. if a != 0 and (b^2 - 4*a*c) < 0, x1 = (-b/(2*a) + (sqrt(4*a*c - b^2)/(2*a)) "i"
                                             x2 = (-b/(2*a) - (sqrt(4*a*c - b^2)/(2*a)) "i"                                 
                                             where "i" = sqrt(-1); "i" and "+" in text
                                             only; real and imaginary parts must
                                             be calculated separately.
Your program should properly display the results for x. The source file should have header documentation and comments within the body of the program as well.

Explanation / Answer

#include <iostream>
#include <math.h>
using namespace std;

int main(){
   double a, b, c;
   cout << "Enter the values of a, b, c: ";
   cin >> a >> b >> c;
   if(a == 0 && b == 0){
       cout << "No results can be found for x ";
   }
   else if(a == 0 && b != 0){
       cout << "x = " << -c / b << " ";
   }
   else if(a != 0 && b*b - 4 * a * c == 0){
       cout << "x1 = x2 = " << -b / (2 * a) << " ";
   }
   else if(a != 0 && b*b - 4 * a * c > 0){
       cout << "x1 = " << (-b + sqrt(b * b - 4 * a * c)) / (2 * a) << " ";
       cout << "x2 = " << (-b - sqrt(b * b - 4 * a * c)) / (2 * a) << " ";
   }
   else if(a != 0 and b * b - 4 * a * c < 0){
       double real = -b / (2 * a);
       double imaginary = sqrt(4 * a * c - b * b) / (2 * a);
       cout << "x1 = " << real << " + " << imaginary << "i ";
       cout << "x1 = " << real << " - " << imaginary << "i ";
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote