Hello, so I am making a program to solve quadratic equations.. or at least I am trying to. I am running into a wall however. I get the following error message: 1.c: In function 'qe':
error: control reaches end of non-void function
Anyways, I am using math.h but ONLY for the pow function. I am not allowed to use it for anything else. That mean I wrote a square root function that my quadratic equation function calls, and an absolute value function that my square root function calls.
I have been working on this for a while and need some input into what I am doing wrong. Below is my code. Thank you so much for your help.
Code:
/* 8. An equation of the form
ax^2 + bx + c = 0
is known as a quadratic equation.The values of a, b, and c in the preceding example
represent constant values. So
4x^2 - 17x - 15 = 0
represents a quadratic equation where a = 4, b = –17, and c = –15.The values of x
that satisfy a particular quadratic equation, known as the roots of the equation, can
be calculated by substituting the values of a, b, and c into the following two
formulas:
If the value of b^2–4ac, called the discriminant, is less than zero, the roots of the
equation, x1 and x2, are imaginary numbers.
Write a program to solve a quadratic equation.The program should allow
the user to enter the values for a, b, and c. If the discriminant is less than
zero, a message should be displayed that the roots are imaginary; otherwise,
the program should then proceed to calculate and display the two roots of
the equation. (Note: Be certain to make use of the squareRoot function that
you developed in this chapter.) */
#include <math.h>
#include <stdio.h>
//function to find absolute value
float absoluteValue (float d)
{
if ( d < 0 )
d = -d;
return d;
}
// Function to compute the square root of a number
float squareRoot (float d)
{
const float epsilon = .00001;
float guess = 1.0;
while ( absoluteValue (guess * guess - d) >= epsilon )
guess = ( d / guess + guess ) / 2.0;
return guess;
}
// quadratic equation function
float qe (float a, float b, float c)
{
float x1, x2;
float d;
d = (pow (b, 2) - (4 * a * c));
if (d < 0)
printf ("x1 and x2 are imaginary numbers");
x1 = (-b + squareRoot (d)) / (2 * a);
printf("x1 is %f", x1);
x2 = (-b - squareRoot (d)) / (2 * a);
printf("x2 is %f", x2);
}
int main (void)
{
float a, b, c;
printf("What is a?: ");
scanf("%f\n", &a);
printf("What is b?: ");
scanf("%f\n", &b);
printf("What is c?: ");
scanf("%f\n", &c);
qe (a, b, c);
return 0;
}