# Extreme beginner quadratic roots solver

• 11-08-2009
browser
Extreme beginner quadratic roots solver
Right, so I am brand new to programming (doing it for a module at uni). We have to write a programme to find real solutions to a quadratic to 3 decimal places. We need to use command line arguments and the if-else construct.

Looking over various code I have found online and the very little that I actually do understand, I put this together and would like to understand what it all means I have made bold the bits I am unsure about.

Code:

```#include <stdio.h> #include <stdlib.h> #include <math.h> int main(int argc, char* argv[]) {     double  a, b, c, valida, validb, validc, x1, x2;     int    valid_input;             if(argc != 4)                                                                    {         printf("Enter three arguments\n");         exit(EXIT_FAILURE);     }             else     {         valida=sscanf(argv[1], "%lg", &a);         validb=sscanf(argv[2], "%lg", &b);         validc=sscanf(argv[3], "%lg", &c);         valid_input = (valida != 0) && (valida != EOF);         valid_input = valid_input && (validb !=0) && (validb != EOF);         valid_input = valid_input && (validc !=0) && (validc != EOF);                 if(!valid_input)         {                         printf("Please enter 3 valid digits...\n");                         exit(EXIT_FAILURE);         }                                 else                 {                                                                 if((a==0)&&(b==0))                         {                                 printf("It is not possible to find the roots of these numbers \n");                                 printf("as both 'a' and 'b' coefficients are zero\n");                                 exit(EXIT_FAILURE);                         }                                                 if(a==0)                         {                                 x1 = (-(c/b));                                 printf("There is 1 root");                                 printf("x1 = %.3lf\n",x1);                         }                                                 else                         {                                         if(((b*b)-(4*a*c)) > 0)                                         {                                                  x1 = ((-b)+(sqrt((b*b)-(4*a*c))))/(2*a);                                                 x2 = ((-b)-(sqrt((b*b)-(4*a*c))))/(2*a);                                                                                         printf("There are 2 roots\n");                                                 printf("x1 = %.3lf\n",x1);                                                 printf("x2 = %.3lf\n",x2);                                                 exit(EXIT_SUCCESS);                                         }                                                         if(((b*b)-(4*a*c)) == 0)                                         {                                                 x1 = ((-b)/(2*a));                                                                                         printf("There is 1 root\n");                                                 printf("x = %.3lf\n",x1);                                                 exit(EXIT_SUCCESS);                                         }                                                         else                                         {                                                 printf("Cannot calculate imaginary roots\n");                                                 exit(EXIT_FAILURE);                                         }                                                                                                                }                 }         }                         return(0); }```
For the first bold bit;
-Should I be using float or double?
-Is it right to set valid_input as an integer?
-Should I define a,b,c,x1,x2 before main()?

For the second (validation);
-Is %lg correct? and if using float, it should be %f, right?
-In the valid_input bit, have I said that it is only valid if ALL 3 lines are met or is each one separate (as in is it only invalid if a,b and c are zero, or if any of them are)?

For the third bit;
-Is %.3lf correct for 3 decimal places?
-Would it be different if it was a float? %.3f?
-Does it need an exit(EXIT_SUCCESS) after it?

In general;
-Should I define the discriminant before main() and then use it in the if-else instead?

---------------------------

I apologise if I have gone against any board rules, I have read the FAQ and searched the forum before posting and tried to follow the guidelines....

Thanks
• 11-08-2009
Epy
Float should be fine. Int is correct, logical statements return 1 or 0 as signed integers. Define your variables inside of main() like you already have it.

Should be %.3f for a float, I believe.

The exit() function is more for when errors are encountered, as I understand it. Should use return instead.
• 11-08-2009
The sscanf() parts you highlighted are used to just locate and put the right values from the *argv[] (a 2 D array), into the correct variables: a, b, or c

sscanf() is scanf() used on a designated string.

.3lf is the printf() format for a double, printed out to 3 decimal places.
• 11-08-2009
browser
Thanks, so

valida=sscanf(argv[1], "%f", &a)

would assign the first command line argument to the variable a in the programme....

but what about thei valida? is that another variable I have created? or does that mean something like, "to validate a the assignment of it must be a float?