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

Write a C++ program to find the squareroots of a quadratic equation of the form

ID: 3689357 • Letter: W

Question

Write a C++ program to find the squareroots of a quadratic equation of the form Ax^2 + Bx + C = 0. Prompt the user to enter the coefficients A, B, and C. The program can only handle real squareroots; thus, the program must reject the coefficients if the discriminant (B^2 - 4AC)

Explanation / Answer

Hi below i have written the sample code for to calculate the roots of the Quadratic Equation for your reference, #include #include #include #include typedef double complex cplx; void quad_root (double a, double b, double c, cplx * ra, cplx *rb) { double d, e; if (!a) { *ra = b ? -c / b : 0; *rb = 0; return; } if (!c) { *ra = 0; *rb = -b / a; return; } b /= 2; if (fabs(b) > fabs(c)) { e = 1 - (a / b) * (c / b); d = sqrt(fabs(e)) * fabs(b); } else { e = (c > 0) ? a : -a; e = b * (b / fabs(c)) - e; d = sqrt(fabs(e)) * sqrt(fabs(c)); } if (e < 0) { e = fabs(d / a); d = -b / a; *ra = d + I * e; *rb = d - I * e; return; } d = (b >= 0) ? d : -d; e = (d - b) / a; d = e ? (c / e) / a : 0; *ra = d; *rb = e; return; } int main() { cplx ra, rb; quad_root(1, 1e12 + 1, 1e12, &ra, &rb); printf("(%g + %g i), (%g + %g i) ", creal(ra), cimag(ra), creal(rb), cimag(rb)); quad_root(1e300, -1e307 + 1, 1e300, &ra, &rb); printf("(%g + %g i), (%g + %g i) ", creal(ra), cimag(ra), creal(rb), cimag(rb)); return 0; }
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