MilaDimitrovaa

quadEq

Mar 19th, 2022
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int quadEq(double a, double b, double c, double* x1, double* x2){
  6.     double d = (b * b) - 4 * a * c;
  7.  
  8.     if(d > 0){
  9.         *x1 = (-b - sqrt(d)) / 2 * a;
  10.         *x2 = (-b + sqrt(d)) / 2 * a;
  11.     }else if(d == 0){
  12.         *x1 = -b / 2 * a;
  13.         *x2 = *x1;
  14.     }else{
  15.         return -1;
  16.     }
  17.         return 0;
  18. }
  19.  
  20. int main(int argc, char *argv[]){
  21.     if(argc != 4){
  22.         fprintf(stderr, "Invalid arguments!\n");
  23.         return 1;
  24.     }
  25.  
  26.     double a = atof(argv[1]);
  27.     double b = atof(argv[2]);
  28.     double c = atof(argv[3]);
  29.     double x1,x2;
  30.  
  31.     if(quadEq(a,b,c,&x1,&x2) == 0){
  32.         printf("x1 = %.2lf , x2 = %.2lf\n", x1,x2);
  33.     }else{
  34.         printf("No real roots");
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment