Please bear with me as this is probably a trivial question! I have no clue what I'm doing wrong. When trying to write a program to solve quadratic equations in a certain way I defined 3 functions: main, quad_roots and lin_root, where quad_roots solves a quadratic equation and lin_root solves a linear equation.
Problem 1: For cases where I call the lin_root function into my quadratic function, it doesn't output what it's supposed to. What am I doing wrong?
Problem 2: I was wondering if there is a neater way of coding this, keeping the return values as they are?
my main function:
#include<stdio.h>
#include<math.h>
#pragma warning(disable: 4996)
int main(void)
{ double a2,a1,a0;
double root1,root2;
int quad_case;
int lin_case;
printf("Enter the coefficients of linear equation a2*x^2+a1*x+a0=0 \n");
printf("In the order a2,a1,a0, separated by spaces: ");
scanf_s("%lf %lf %lf",&a2,&a1,&a0);
quad_case=quad_roots(a2,a1,a0,&root1,&root2);
lin_case=lin_root(a1,a0,&root1);
switch (quad_case) {
case -3 : printf("\n x = any number i.e. there are infinitely many roots \n");
break;
case -2 : printf("\n There are no roots \n");
break;
case -1 :
switch (lin_case) {
case -1 : printf("\n x = any number i.e. there are infinitely many roots \n");
break;
case 1 : printf("\n This is a linear equation with one root r1 = %g \n",root1);
break;
case 0 : printf("\n There are no roots \n");
break;
}
break;
case 2 : printf("\n Two distinct real roots r1 = %g and r2 = %g \n",root1,root2);
break;
case 1 : printf("\n Two identical real roots r1 = r2 = %g \n",root1);
break;
case 0 : printf("\n Two complex conjugate roots r1 = %g + %gi and r2 = %g - %gi \n",root1,root2,root1,root2);
break;
default: printf("\n NAN \n");
break;
}
}
my lin_root function:
#include<stdio.h>
#include<math.h>
#pragma warning(disable: 4996)
int lin_root(double A, double B, double* r1)
{
if(A != 0)
{*r1=-B/A;
return(1);
}
else if(A == 0 && B != 0)
{
return(0);
}
else if(A == 0 && B == 0)
{
return(-1);
}
}
my quad_roots function:
#include<stdio.h>
#include<math.h>
#pragma warning(disable: 4996)
int quad_roots(double a2,double a1, double a0, double* r1, double* r2)
{ double discriminant, determinant, two=2, four=4, p, q;
p=(a1/a2);
q=(a0/a2);
discriminant=a1*a1-four*a2*a0;
determinant=p*p-four*q;
if (a2 == 0 && a1 == 0 && a0 == 0)
{
return(-3);
}
else if (a2 == 0 && a1 == 0 && a0 != 0)
{
return(-2);
}
else if (a2 == 0)
{lin_root(a1,a0,r1);
return(-1);
}
else if(a2 != 0 && a1 != 0 && a0 == 0)
{lin_root(a2,a1,r1);
*r2 = 0;
return(2);
}
else if(determinant > 0 && a1 > 0)
{*r1 = -(p/two) + (((sqrt(p))*(sqrt(p-four*(a0/a1))))/two);
*r2 = (a0/a2)/(*r1);
return(2);
}
else if(determinant > 0 && a1 == 0 && a2 < 0)
{*r1 = ((sqrt(-a2))*(sqrt(a0)))/(a2);
*r2 = -((sqrt(-a2))*(sqrt(a0)))/(a2);
return(2);
}
else if(determinant > 0 && a1 == 0 && a2 > 0)
{*r1 = ((sqrt(-a0))*(sqrt(a2)))/(a2);
*r2 = -((sqrt(-a0))*(sqrt(a2)))/(a2);
return(2);
}
else if(determinant > 0 && a1 < 0)
{*r1 = -(p/two) + (((sqrt(-p))*(sqrt(-p-(-four*q*(1/p)))))/two);
*r2 = (a0/a2)/(*r1);
return(2);
}
else if(determinant == 0)
{*r1 = *r2 = -a1/(two*a2);
return(1);
}
else if(determinant < 0 && a1 > 0)
{*r1 = -(p/two);
*r2 = (((sqrt(p))*(sqrt(p-four*(a0/a1))))/two);
return(0);
}
else if(determinant < 0 && a1 == 0 && a2 < 0)
{*r1 = 0;
*r2 = ((sqrt(-a2))*(sqrt(a0)))/(a2);
return(0);
}
else if(determinant < 0 && a1 == 0 && a2 > 0)
{*r1 = 0;
*r2 = ((sqrt(-a0))*(sqrt(a2)))/(a2);
return(0);
}
else if(determinant < 0 && a1 < 0)
{*r1 = -(p/two);
*r2 = (((sqrt(-p))*(sqrt(-p-(-four*q*(1/p)))))/two);
return(0);}
}
Thanks in advance!