I have a task to find errors in this long line of code in order to correctly calculate the solutions of quadratic equations.
Code:
int main(int argc, char *argv[])
{
int validInput, solution_type;
double a, b, c, root_1, root_2, q;
/***********************
* Input / Validation
***********************/
/* Check numbers of arguments, and read input */
validInput = (argc = 4);
validInput = validInput && scanf(argv[1], "%lf", &a);
validInput = validInput && scanf(argv[2], "%lf", &b);
validInput = validInput && scanf(argv[3], "%lf", &c);
/* Calculate roots if input validated */
if(validInput)
{
if(a > 0)
Is a section of the code (the first section). And as you can probably guess, it goes on to calculate for a > 0 etc...
I dont really understand what the validinput section is saying? And a, b and c are never defined so Xcode is just saying a,b,c,root1,root2 are uninitialized and I also dont know what means.
I just need some help really, anything. Do I need to define these values?
-----------
I'll post the whole code below which may help, but i dont expect you to look through for errors and what not:
Code:
int main(int argc, char *argv[])
{
int validInput, solution_type;
double a, b, c, root_1, root_2, q;
/***********************
* Input / Validation
***********************/
/* Check numbers of arguments, and read input */
validInput = (argc = 4);
validInput = validInput && scanf(argv[1], "%lf", &a);
validInput = validInput && scanf(argv[2], "%lf", &b);
validInput = validInput && scanf(argv[3], "%lf", &c);
/* Calculate roots if input validated */
if(validInput)
{
if(a > 0)
{
/* Quadratic formulas can be used, but only
if b*b-4*a*c is larger or equal to 0 */
if(SOLUTION_EXISTS(a,b,c))
{
q=sqrt((b*b)-(4*a*c));
root_1=-0.5*(b+q)/a;
root_2=-0.5*(b-q)/a;
if(fabs(root_1-root_2)>ERROR)
{
solution_type=SOLUTION_TYPE_TWO;
}
else
{
solution_type=SOLUTION_TYPE_ONE;
}
}
else
{
solution_type=SOLUTION_TYPE_ZERO;
}
}
else
{
/* If quadratic formulaes do not apply */
if((b==0)||(c==0))
{
solution_type=SOLUTION_TYPE_MANY;
}
else if(b==0)
{
solution_type=SOLUTION_TYPE_ZERO;
}
else if(c==0)
{
root_1=0; root_2=0;
solution_type=SOLUTION_TYPE_ONE;
}
else
{
root_1=-(c/b); root_2=0;
solution_type=SOLUTION_TYPE_ONE;
}
}
/* Print appropriate message */
if(solution_type==SOLUTION_TYPE_TWO)
printf("Equation has two solutions: x1=%.3lf x2=%.3lf\n", root_1, root_2);
else if(solution_type==SOLUTION_TYPE_ONE)
printf("Equation has one solution: x1=%.3lf\n", root_1);
else if(solution_type==SOLUTION_TYPE_ZERO)
printf("Equation has no solutions\n");
else
printf("Equation has infinitely many solutions\n");
}
else
{
printf("**************************************\n");
printf("* !!! User input error detected !!! *\n");
printf("* Execution method: prog2A a b c *\n");
printf("* where a, b and c are real numbers *\n");
printf("**************************************\n");
}