hello, i have only recently started learning C and i still havent really got to grips with the basics...
I am writing a program to determine the roots of a quadratic equation, using command line arguments. The program compiles... but doesnt do anything. I think the problem is that the program doesn't know what the variables a, b and c are. I need them to be the command line arguments (?) i.e you type "program_name a b c" ...but i'm stuck on how to make this work.
any pointers in the right direction would be greatly appreciated:
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
float a, b, c, droot, root1, root2;
double discriminant = (pow(b,2) - (4 * a * c));
if( argc != 3 )
{ printf("3 co-efficients were not entered. Please enter them in the order a, b, c.\n");
exit(1);
}
else if( discriminant < 0 )
{
printf("There are 0 roots.\n");
}
else if( discriminant == 0 )
{
printf("There is 1 root.\n");
droot = (-b) / (2*a);
printf("Root1=%f\n", droot);
}
else if( discriminant > 0 )
{
printf("There are 2 roots.\n");
root1 = (-b + sqrt(discriminant)) / 2 * a;
root2 = (-b - sqrt(discriminant)) / 2 * a;
printf("Root1=%f Root2=%f.\n", root1, root2);
}
return(0);
}
Thanks!