TheRoice

quadratics

Oct 10th, 2021
704
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <math.h>
  2. #include <stdio.h>
  3.  
  4. /*
  5. This problem will solve quadratic equations.
  6.  
  7. This program will ask users for A, B, and C.
  8. it will then calculate and print both roots
  9. for x, even if they are complex numbers.
  10.  
  11. If a = 0 the program should print an error.
  12.  
  13. If the roots are a pair of real numbers that
  14. differ, both should be displayed.
  15.  
  16. If there is only one (repeated) root, it should
  17. be displayed and the user should be told that
  18. it is a repeated root.
  19.  
  20. If the solution is a pair of complex numbers,
  21. they should be displayed in the form
  22. x = P + Qi and x = P - Qi where P and Q are
  23. real numbers, i is the square root of -1
  24.  
  25. Strongly advised to make a function to return
  26. the discriminant (b^2 - 4ac)
  27.  
  28. Do not use global variables
  29.  
  30. (-b + or - sqrt(b^2-4ac)) / 2a
  31.  
  32. */
  33.  
  34. double calculate_discriminate(double a, double b, double c);
  35. double take_root(double discriminate);
  36. double calculate_numerator(double b, double root);
  37. double calculate_denominator(double a);
  38. double calculate_quadratic(double numerator, double denominator);
  39.  
  40.  
  41. int main() {
  42.     double a;
  43.     double b;
  44.     double c;
  45.  
  46.     printf("Please enter a number for 'a' \n");
  47.     scanf_s("%lf", &a);
  48.    
  49.     printf("Please enter a number for 'b' \n");
  50.     scanf_s("%lf", &b);
  51.    
  52.     printf("Please enter a number for 'c' \n");
  53.     scanf_s("%lf", &c);
  54.  
  55.     double discriminate = calculate_discriminate(a, b, c);
  56.     double root = take_root(discriminate);
  57.     double added_numerator = calculate_numerator_adding(b, root);
  58.     double subtracted_numerator = calculate_numerator_subtracting(b, root);
  59.     double denominator = calculate_denominator(a);
  60.     double answer1 = calculate_quadratic(added_numerator, denominator);
  61.     double answer2 = calculate_quadratic(subtracted_numerator, denominator);
  62.  
  63.     printf("Your answer is %lf, and %lf.", answer1, answer2);
  64.  
  65.  
  66. }
  67. /*
  68. This calculates the discriminate and returns that number
  69. as a double.
  70. */
  71. double calculate_discriminate(double a, double b, double c) {
  72.     double discriminate = ((b * b) - (4 * a * c));
  73.     return discriminate;
  74. }
  75.  
  76. /*
  77. This takes the root of the discriminate and returns it
  78. */
  79. double take_root(double discriminate) {
  80.     double root = sqrt(discriminate);
  81.     return root;
  82. }
  83.  
  84. double calculate_numerator_adding(double b, double root) {
  85.     double numerator = (-1 * b + root);
  86.     return numerator;
  87. }
  88.  
  89. double calculate_numerator_subtracting(double b, double root) {
  90.     double numerator = (-1 * b - root);
  91.     return numerator;
  92.  
  93. double calculate_denominator(double a) {
  94.     double denominator = 2 * a;
  95.     return denominator;
  96. }
  97.  
  98. double calculate_quadratic(double numerator, double denominator) {
  99.     double answer = numerator / denominator;
  100.     return answer;
  101. }
  102.  
RAW Paste Data