I appreciate any help with this program
I really was trying to figure it out myself, but I am running out of time
Find the roots of the equation of the form ax2 +bx+ c =0. Ask the user to input a, b and c. Calculate the discriminant = b2 – 4ac.
a. If the discriminant is less than zero, display the message “The roots are imaginary”.
b. Otherwise, calculate the roots and display them as follows:
i. If the roots are the same, display “The roots are the same” and display the single calculated root.
ii. If they are different, display “The roots are different” and display the calculated roots.
Use the squareRoot function in chapter 8.
The following is the square root function from chapter 8
Code:
]#include <stdio.h>
float absoluteValue (float x)
{
if (x < 0)
x = -x;
return (x);
}
float squareRoot (float x)
{
const float epsilon = .00001;
float guess = 1.0;
while ( absoluteValue (guess * guess - x) >= epsilon )
guess = ( x / guess + guess ) / 2.0;
return guess;
}
int main (void)
{
printf (" squareroot (2.0) = %f\n", squareRoot (2.0));
printf (" squareroot (144.0) = %f\n", squareRoot (144.0));
printf (" squareroot (2.0) = %f\n", squareRoot (144.0));
getch();
}
Ok I incorporated the square root function from above into my program and wrote the following:
Code:
#include <stdio.h>
float absoluteValue (float x)
{
if (x < 0)
x = -x;
return (x);
}
float squareRoot (float x)
{
const float epsilon = .00001;
float guess = 1.0;
while ( absoluteValue (guess * guess - x) >= epsilon )
guess = ( x / guess + guess ) / 2.0;
return guess;
}
int main (void)
{
int a,b,c,y,z,Z;
printf ("We will find the roots of the equation of the form ax^2 +bx+ c =0\n");
printf ("First enter a value for a\n");
scanf ("%i", &a);
printf ("Next enter a value for b\n");
scanf ("%i", &b);
printf ("Next enter a value for c\n");
scanf ("%i", &c);
y = (-b + (squareRoot(b*b*-4*a*c)))/2*a;
z = (-b - (squareRoot(b*b*-4*a*c)))/2*a;
Z = b*b*(-4*a*c);
if (Z < 0)
printf (" The roots are imaginary\n");
else if ( y==z)
printf ("The roots are the same and are equal to %i\n ", y);
else if ( y != z )
printf ("The roots are different and are equal to %i and %i\n ", y,z);
getch();
}
I am able to compile the program with out errors but it does not run correctly:
I am pretty sure it has to do with this part of the program
y = (-b + (squareRoot(b*b*-4*a*c)))/2*a;
z = (-b - (squareRoot(b*b*-4*a*c)))/2*a;
Z = b*b*(-4*a*c);
I do not think I can use the Square root function like this, but then I am stuck on how to use this function to get my program to operate correctly. I Appreciate any help with this program. Thanks very, very much, Bill