0

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!

MAKZ
  • 165
  • 14
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
  • What does "doesn't output what it's supposed to" mean? – Crowman Feb 16 '14 at 14:10
  • @PaulGriffiths For example, if I input 1 5 6, It should return 2 distinct roots -2 and -3, but it returns -1.2 and -3, or if I enter 1 -1 0, it should return roots 0 and 1 but returns 0 and 0 – SpiderCode Feb 16 '14 at 14:21
  • @BLUEPIXY they are supposed to – SpiderCode Feb 16 '14 at 14:40
  • @SpiderCode ,`quad_roots(a2,a1,a0,&root1,&root2);` a2 == 0 or a2 != 0 In any case execute `lin_case=lin_root(a1,a0,&root1);` then update `root1` – BLUEPIXY Feb 16 '14 at 15:48
  • @BLUEPIXY Could you possibly write the code for what you mean for these two cases? I'm not sure how to update root1, and also in the a2 != 0 case, I am trying to do lin_root(a2,a1,&root1) not lin_root(a1,a0,&root1) – SpiderCode Feb 16 '14 at 15:53
  • e.g. quad_roots(1,5,6, &root1, &root2) -> Abracadabra -> root1 = -2, root2 = -3. but after.. in_root(5,6,&root1); -> root1 = -6/5. – BLUEPIXY Feb 16 '14 at 16:04

2 Answers2

0

All lin_root() function calls invoked from int quad_roots() { ... doesn't store the return value returned from lin_root(). This could be the reason for your code to not work.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
0

Here is an answer to your problem 2 :

#include <stdio.h>
#include <math.h>

void test_lin(float  b, float c)
{
    if((b==0) & (c==0))
        puts("x = any number i.e. there are infinitely many roots");
    else if(b==0) 
        puts("There are no roots");
    else printf("root, r = %g", -c/b);
    return;
}


void test_quad(float a, float b, float c)
{
    float d = b*b - 4*a*c, A = 2*a;

    if(d == 0)
        printf("\n Two identical real roots r1 = r2 = %g", -b/A);

    else if(d>0)
        printf("\nTwo distinct real roots r1 = %g and r2 = %g", -b/A + sqrt(d)/A, -b/A - sqrt(d)/A );

    else {
        d *= -1;
        printf("\n Two complex conjugate roots r1 = %g + %g i and r2 = %g - %g i \n", -b/A , sqrt(d)/A, -b/A , sqrt(d)/A);

    }

}
int main()
{
    float a,b,c , r1,r2;
    puts("ax*2 + bx + c = 0 : ");
    scanf("%f %f %f", &a, &b, &c);
    if(a==0)
        test_lin(b,c);
    else test_quad(a,b,c);

    return 0;

}
MAKZ
  • 165
  • 14
  • When I do this, I get all sorts of wrong errors when using large numbers, thus I edited the quadratic formula to reduce errors... I just wondered whether 'if, else if,....' could be changed to another, less messy, format? – SpiderCode Feb 16 '14 at 14:56
  • well, (1e308)^2 = 1e616 which is out of range in float. trychanging float to double and %g to %lg – MAKZ Feb 16 '14 at 15:03