Create a program to calculate the roots of the quadratic equation: ax +bx+c 0. P
ID: 641186 • Letter: C
Question
Create a program to calculate the roots of the quadratic equation: ax +bx+c 0. Prompt the user for the coefficients a, b and c. The program will use the quadratic formula to calculate the roots. Your program should distinguish among the three possible cases: a) two distinct real roots, b) one real root, c) two complex roots. Run the program with these values for (a, b, c): (3,8, 5), (2,-4, 2), (4.5,-1.3, 2), (-4, 1, 5) and two other triples of your choice. The output should look like the one of the following depending on the case: The two real roots are: #1 and #2 The unique real root is: #1 The complex roots are: #1 + #2 i and #1--#2 i There should be a space between a number and any adjacent word or symbol "+"or "-” in the complex case. To test if a variable equals zero you have to use: "if (d-0)", not "if (d-0)" (double equals sign) Coding Process (Suggested): 1. Create a shell program that compiles 2. Create all variables that your program will require (don't forget the roots) 3. Code the user input part for all the variables 4. Compute the root (or roots) for your user input 5. Determine which root case you have before printing output 6. Output the correct value to the screen and allow the user to see it before exiting.Explanation / Answer
read A; if [ $A = 0 ]; then echo "Not a quadratic equation."; exit 0; fi read B; read C; D=$(( ($B)*($B)-4*($A)*($C) )); #integer math only! if [ $D = 0 ]; then echo -n "x = " echo -e "scale=3 -0.5*($B)/($A)" | bc exit 0; fi echo $D if [ $D -gt 0 ]; then echo -n "x1 = " echo -e "scale=3 0.5*(-($B)+sqrt($D))/($A)" | bc echo -n "x2 = " echo -e "scale=3 0.5*(-($B)-sqrt($D))/($A)" | bc else echo -n "x1 = (" echo -e "scale=3 -0.5*($B)/($A)" | bc echo -n ", " echo -e "scale=3 0.5*sqrt(-($D))/($A)" | bc echo ")" echo -n "x2 = (" echo -e "scale=3 -0.5*($B)/($A)" | bc echo -n ", " echo -e "scale=3 -0.5*sqrt(-($D))/($A)" | bc echo ")" fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.