Advertisement
daltond124

Untitled

Sep 29th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #define EPS 1e-9
  3. #define TRUE (1<2)
  4. #define FALSE (!TRUE)
  5. // The function that checks if two values are equal
  6. int eq (double a, double b);
  7. // The function that searchs from the lo to the hi for the max area
  8. // The function return the max area found
  9. double search(double lo, double hi, double length);
  10. // The function that returns the area (could be repurposed for
  11. // Moore)
  12. double foo(double total, double upper);
  13. // The main function
  14. int main(){
  15. // Read in the length
  16. double length;
  17. scanf("%lf", &length);
  18. // Initialize the lo and hi bound of the top rod of the frame
  19. double lo = 0;
  20. double hi = 1;
  21. // Check if we can use a better upper bound for the length of the top rod
  22. while (foo(length, hi) < foo(length, hi * 2))
  23. hi = hi * 2;
  24. // Double one last time to ensure we have the maximum within our range
  25. hi = hi * 2;
  26. // Compute the answer by searching from the lo to the hi bound
  27. double ans = search(lo, hi, length);
  28. // Print the answer
  29. printf("%lf\n", ans);
  30. // Return the answer
  31. return 0;
  32.  
  33. }
  34. double search(double lo, double hi, double length)
  35. {
  36. // Check if the lo and hi value are approximately the same
  37. if (eq(lo, hi))
  38. return foo(length, lo); // Get the area of the frame
  39. // Compute the mid points using the 2 thirds trick discussed in class
  40. // Note this breaks the search space into 3 "equal" parts
  41. double m1 = (2 * lo + hi) / 3;
  42. double m2 = (lo + 2 * hi) / 3;
  43. // Compute the area using the first and second mid points respectively
  44. double area1 = foo(length, m1);
  45. double area2 = foo(length, m2);
  46. // Check if area 1 is worse than area 2
  47. if (area1 < area2) {
  48. // Since area 1 is worse eliminate the first section of the search move up the lower bound lo = m1;
  49. return search(m1, hi, length);
  50.  
  51. }
  52. // Area 1 is better move down the hi bound
  53. return search(lo, m2, length);
  54. }
  55.  
  56. int eq (double a, double b)
  57. { // Get the difference
  58. double diff = a - b;
  59. if (diff < 0)
  60. diff = -diff; // absolute value of the difference
  61. if (diff < EPS)
  62. return TRUE; // ABSOLUTE ERROR CHECK PASSED
  63. if (a < 0)
  64. a = -a; // abs
  65. if (b < 0)
  66. b = -b; // abs
  67. if (a < b)
  68. a = b; // max
  69. // Scale the error by the magnitude of the big value
  70. // This will give a new relative error to check against
  71. if (a * EPS > diff)
  72. return TRUE; // RELATIVE ERROR CHECK PASSED
  73.  
  74. return FALSE; // Not equal values
  75. }
  76.  
  77. // This function computes the area of the frame given
  78. // 1. The total length of the wooden rod
  79. //2. The width of the frame (upper wood piece)
  80. double foo(double total, double upper){
  81. // Compute the remaining length after using the bottom and top
  82. double remainingRod = total - 2 * upper;
  83. // Compute the height by using the remaining rod in 6 different pieces
  84. double small = remainingRod / 6;
  85. // Return the area (product of the height and the width)
  86. return small * upper;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement