Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. #include <iostream> // to use cin and cout
  2. #include <typeinfo>
  3. #include <iomanip>
  4. #include <cmath> // to be able to use operator typeid
  5. // Include here the libraries that your program needs to compile
  6.  
  7.  
  8. using namespace std;
  9.  
  10. // Ignore this; it's a little function used for making tests
  11. inline void _test(const char* expression, const char* file, int line)
  12. {
  13. cerr << "test(" << expression << ") failed in file " << file;
  14. cerr << ", line " << line << "." << endl << endl;
  15. }
  16. // This goes along with the above function...don't worry about it
  17. #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))
  18.  
  19. // Insert here the prototypes of the functions
  20. void print_data(char n, int r, int h, double v);
  21. // Declare a global constant variable called PI that holds value 3.141592
  22. double PI = 3.141593;
  23. int main()
  24. {
  25. // Declare a variable named name that holds text
  26. char name;
  27. // Declares variables named height and radius that hold whole numbers
  28. int height, radius;
  29. // Declare a variable named volume that holds double precision real numbers
  30. double volume;
  31. // Prompts the user "May I get your first name please?: "
  32. cout << "May I get your first name please?: ";
  33. // Read the value from the keyboard and stores it in name
  34. cin >> name;
  35. // Prompt the user "Thanks ", name, " , now enter radius and height of the cone please: "
  36. cout << "Thanks " << name << " , now enter radius and height of the cone please: ";
  37. // Read the values from the keyboard and stores them in radius and height respectively
  38. cin >> radius >> height;
  39. // Call function volume_cone() to calculate the volume of the cone and assign the result to volume
  40. volume = volume_cone(radius, height);
  41. // Call function print_data() to print the values entered by the user and the volume of the cone
  42.  
  43.  
  44.  
  45.  
  46. system("pause"); // this is not required but the students can include it if they like
  47.  
  48. // Do NOT remove or modify the following statements
  49. cout << endl << "Testing your solution" << endl << endl;
  50. test(typeid(PI) == typeid(1.)); // Incorrect data type used for PI
  51. test(PI == 3.141592); // Incorrect value used for PI
  52. test(typeid(name) == typeid(string)); // Incorrect data type used for name
  53. test(typeid(height) == typeid(1)); // Incorrect data type used for height
  54. test(typeid(radius) == typeid(1)); // Incorrect data type used for radius
  55. test(typeid(volume) == typeid(1.)); // Incorrect data type used for volume
  56. test(fabs(volume_cone(3, 5) - 47.124) < 0.0001); // Incorrect rounding of volume
  57. test(fabs(volume_cone(3, 4) - 37.699) < 0.0001); // Incorrect rounding of volume
  58. system("pause");
  59. return 0;
  60. }
  61.  
  62.  
  63. //************************ Function definition *************************
  64. // Read the handout carefully for detailed description of the functions that you have to implement
  65.  
  66. // Calculates the square of the value received
  67. double square(double num)
  68. {
  69. return num * num;
  70. }
  71.  
  72. // Calculates the volume of the cone using the formula 1 / 3 x Pi x radius^2 x height
  73. double volume_cone(double radius, double height)
  74. {
  75. return round_off((1 / 3) * PI * square(radius), 2) * height;
  76. }
  77. // Rounds the value received in the first parameter to the number of digits received in the second parameter
  78. double round_off(double num, int deci)
  79. {
  80. return floor((num * pow(10, deci)) + 0.5) / pow(10, deci);
  81.  
  82.  
  83. }
  84.  
  85. // Prints the volume of the cone
  86. void print_data(char n, int r, int h, double v)
  87. {
  88. cout << "Ok " << n << endl;
  89. cout << "For a radius: " << r << " and a height: " << h << " the cone's volume is " << v << endl;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement