Hi. I need a little help writing a program. Below is what is the question and th
ID: 1926503 • Letter: H
Question
Hi. I need a little help writing a program. Below is what is the question and the code that I have started. My main problem with this code is getting the function to return values back to the main and using the pointer to load the roots.
Write a function that solves the quadratic equation ax2 + bx + c = 0. The prototype of the function should be:
int quadratic(double a, double b, double c, double* r1, double* r2);
When called, the function should do the following:
#include <stdio.h>
#include <math.h>
int quadratic(double a, double b, double c, double* r1, double* r2);
int main()
{
double x, y, z;
int ans;
double root1;
double root2;
printf("a = ");
scanf("%f", &x);
printf("b = ");
scanf("%f", &y);
printf("c = ");
scanf("%f", &z);
ans = quadratic(x, y, z, &root1, &root2);
if (ans==1) {
printf("not a quadratic equation");
}
if (ans==2) {
printf("Solution of linear equation is: %f", root1);
}
if (ans==3){
printf("root 1: %0.2f ", root1);
printf("root 2: %0.2f", root2);
}
if (ans==4){
printf("complex root 1: %0.2f", root1);
printf("complex root 2: %0.2f", root2);
}
return 0;
}
int quadratic(double a, double b, double c, double* r1, double* r2)
{
double sol;
sol= (b*b)-(4*a*c);
if (a==0 && b==0) {
return 1;
}
if (a==0){
*r1=-c/b;
return 2;
}
if (a!=0){
if (sol>0){
*r1= (-b)+sqrt(b*b-4*a*c)/(2*a);
*r2= (-b)-sqrt(b*b-4*a*c)/(2*a);
return 3;
}
if (sol<0) {
*r1= (-b)+sqrt(-(b*b-4*a*c))/(2*a);
*r2= (-b)-sqrt(-(b*b-4*a*c))/(2*a);
return 4;
}
}
}
Explanation / Answer
Your program is correct everywhere except you must replace double by float everywhere and then it will work.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.