Hi everyone
I am having some trouble with a program meant to solve for x in ax^2+bx+c=0 given user-input values for a, b, and c. This is homework and my professor requires separate functions for the input and operations in the main function and constants for any numbers in equations. The assignment also requires that we use if else statements to determine the value of x. The assignment sheet can be found here: Assignment G1 if you want any more clarification
I have written a code for it that I think should work and the compiler (DEV-C++) does not find any errors. However, when I try to execute the program it allows me to input the values but when I press Enter windows tells me the program has encountered an error and needs to close and asks if I want to send an error report. If any of you can help me I would greatly appreciate it!
Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void getData (double* ptrA, double* ptrB, double* ptrC);
double determine (double coefA, double coefB, double coefC);
int main (void)
{
double coefA;
double coefB;
double coefC;
getData(&coefA, &coefB, &coefC);
determine(coefA, coefB, coefC);
system("pause");
return 0;
}
void getData (double*ptrA, double*ptrB, double*ptrC)
{
double coefA;
double coefB;
double coefC;
printf("This function solves for x in the equation ax*x+bx+c=0. Please\n");
printf("enter values for coefficients a, b, and c: ");
scanf("%f %f %f", coefA, coefB, coefC);
*ptrA = coefA;
*ptrB = coefB;
*ptrC = coefC;
return;
}
double determine (double coefA, double coefB, double coefC)
{
double x;
double det;
double solA;
double solB;
const int number1 = 2;
const int number2 = 4;
double rootDet;
if ((coefA == 0) && (coefB == 0) && (coefC == 0))
printf("Any value of x is a solution.\n");
else if ((coefA == 0) && (coefB == 0) && (coefC != 0))
printf("No solution exists.\n");
else if ((coefA == 0) && (coefB != 0))
{
x = -coefC / coefB;
printf("The value of x is %f\n", x);
}
else if (coefA != 0)
{
det = (coefB * coefB) - (number2 * coefA * coefC);
if (det == 0)
{
x = -coefB / (number1 * coefA);
printf("The value of x is %f.\n", x);
}
else if (det > 0)
{
rootDet = sqrt(det);
solA = (-coefB + rootDet) / (number1 * coefA);
solB = (-coefB - rootDet) / (number1 * coefA);
printf("The solutions are %f and %f.\n", solA, solB);
}
else if (det < 0)
printf("The solutions have an imaginary component.\n");
return;
}}