Solve a quadratic equation in two linked files. Write a function to calculate th
ID: 3718663 • Letter: S
Question
Solve a quadratic equation in two linked files. Write a function to calculate the roots of any quadratic equation of the form ax +bx+c. a, b, and c are all single-precision floating point values and are passed to the function on the stack. The order you put the arguments on the stack is up to you When you return, there should be three items on the stack, two floating point values (referred to below as r1 and r2) and a Status integer. Status indicates the nature of the results returned. Again, the order you put the three values on the stack is up to you. Status 0:2 real roots in r1 & r2 1 1 real root in r1; r2 can be ignored 2: 2 complex roots of the form (r1 t ir2) 3 no roots computed (error); r1 and r2 can be ignored Your program should prompt for the values a, b, and c, then label and print the results. For the purposes of this function, assume two numbers that are within 0.001 are the same Submit the Following Filels: roots.asm, roots_main.asm Required Input: Three 32-bit floating point numbers (a, b, and c) Required Output Your output should look something like the following examples. Note this is three runs. Quadratic solver a: 3.8 b: 4.5 C: 1.2 There are two roots: e.23107084351742913, 1.7310708435174291 Quadratic solver a: 2.e b: 4.e C: 2.e There is one root: -1.00 Quadratic solver a: 4.3 b: -2.45 C: 2.e There are two complex roots: e. 284883721 +/-?.619643878ìExplanation / Answer
import java.util.Scanner;
public class QuadraticSolver {
public QuadraticSolver() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a,b,c,d,r1,r2;
try{
System.out.println("Quadratic Solver");
System.out.print("a:");
a = sc.nextDouble();
System.out.print("b:");
b = sc.nextDouble();
System.out.print("c:");
c = sc.nextDouble();
// calculate d to find type of roots
d = ((b*b)-4*a*c);
if(d>0){
System.out.print("There are two roots: ");
r1 = (-b - Math.sqrt(d))/(2*a);
r2 = (-b + Math.sqrt(d))/(2*a);
System.out.print(r1 + ", "+r2);
}
else if(d==0){
System.out.print("There is one roots ");
r2 = (-b + Math.sqrt(d))/(2*a);
System.out.print(r2);
}
else{
System.out.print("There are two complex roots: ");
System.out.print((-b/(2*a)));
System.out.print(" +/- ");
System.out.print((Math.sqrt(-d)/(2*a)));
}
}
catch(Exception e){
System.out.println("no roots computed error");
}
}
}
--------------------------------------------------------------
Sample output:
Quadratic Solver
a:3
b:4.5
c:-1.2
There are two roots: -1.7310708435174291, 0.23107084351742913
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.