Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. ILLUSTRATING THE USE OF MULTIPLE CALLED FUNCTIONS
  2. WITH CALLED FUNCTIONS PLACED ABOVE THE MAIN PROGRAM
  3.  
  4. A. Design a program in C which compute for one time only the following for a box in the shape of a cube with length=width=height=a:
  5.  
  6. (i) the total surface area, i.e., 6a2;
  7. (ii) the volume of the cube, i.e., a3 and
  8. (iii) the volume of the largest sphere that can fit into the cube,
  9. i.e., (4/3)π(a/2)3.
  10.  
  11. You are to use three prototype or called functions in your design which are to be placed before the main function, with the output to be printed on the console from within the main function and displayed in a tabular form.
  12.  
  13. B. Modify your program so that it will run in an infinite loop except when the user enters a value of 0 for a, at which point the program will terminate execution.
  14.  
  15. SOURCE CODE A
  16. #include <stdio.h>
  17. #define pi 3.1415926
  18.  
  19. float surfaceArea(float a){
  20. float result;
  21. result = 6 * a * a;
  22. return result;
  23. }
  24.  
  25. float volume(float a){
  26. float result;
  27. result = a * a *a;
  28. return result;
  29. }
  30.  
  31. float sphereVol(float a){
  32. float result,radius;
  33. radius=0.5*a;
  34. result = (4/3) * pi * radius * radius * radius;
  35. return result;
  36. }
  37.  
  38. int main(void){
  39. float a;
  40. float area,vol,svol;
  41. printf("Please enter the value of a: ");
  42. scanf("%f",&a);
  43.  
  44. area = surfaceArea(a);
  45. vol = volume(a);
  46. svol = sphereVol(a);
  47.  
  48. printf("a\t\tSurfaceArea\t\tVolOfCube\t\t VolOfSphere\n");
  49. printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t\t%.2f\n",a,area,vol,svol);
  50.  
  51. return 0;
  52. }
  53.  
  54.  
  55. SAMPLE SOLUTION A
  56.  
  57.  
  58.  
  59. SOURCE CODE B:
  60. #include <stdio.h>
  61. #define pi 3.1415926
  62.  
  63. float surfaceArea(float a){
  64. float result;
  65. result = 6 * a * a;
  66. return result;
  67. }
  68. float volume(float a){
  69. float result;
  70. result = a * a *a;
  71. return result;
  72. }
  73. float sphereVol(float a){
  74. float result,radius;
  75. radius=0.5*a;
  76. result = (4/3) * pi * radius * radius * radius;
  77. return result;
  78. }
  79.  
  80. int main(void){
  81. float a;
  82. float area,vol,svol;
  83.  
  84. printf("a\t\tSurfaceArea\t\tVolOfCube\t\t VolOfSphere\n\n");
  85.  
  86. while(1){
  87. printf("Please enter the value of a: ");
  88. scanf("%f",&a);
  89.  
  90. if(a==0){
  91. break;
  92. }
  93.  
  94. area = surfaceArea(a);
  95. vol = volume(a);
  96. svol = sphereVol(a);
  97.  
  98. printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t\t%.2f\n",a,area,vol,svol);
  99. } //end while
  100. return 0;
  101. } //end main
  102.  
  103. SAMPLE SOLUTION B:
  104.  
  105.  
  106.  
  107. ANOTHER SAMPLE SOLUTION B:
  108.  
  109. Note: when the user input a value of 0 for a, the execution is terminated in agreement with specification
  110.  
  111. ILLUSTRATING THE USE OF MULTIPLE CALLED FUNCTIONS
  112. WITH CALLED FUNCTIONS PLACED BELOW THE MAIN PROGRAM
  113.  
  114. A. Design a program in C which compute for one time only the following for a box in the shape of a cube with length=width=height=a:
  115.  
  116. (i) the total surface area, i.e., 6a2;
  117. (iv) the volume of the cube, i.e., a3 and
  118. (v) the volume of the largest sphere that can fit into the cube,
  119. i.e., (4/3)π(a/2)3.
  120.  
  121. You are to use three prototype or called functions in your design, and display the output in a tabular form.
  122.  
  123. C. Modify your program so that it will run in an infinite loop except when the user enters a value of 0 for a, at which point the program will terminate execution.
  124.  
  125. SOURCE CODE A
  126. #include <stdio.h>
  127. #define pi 3.1415926
  128. float surfaceArea(float a);
  129. float volume(float a);
  130. float sphereVol(float a);
  131.  
  132. int main(void){
  133. float a;
  134. float area,vol,svol;
  135.  
  136. printf("a\t\tSurfaceArea\t\tVolOfCube\t\t VolOfSphere\n\n");
  137.  
  138. //while(1){
  139. printf("Please enter the value of a: ");
  140. scanf("%f",&a);
  141.  
  142. //if(a==0){
  143. //break;
  144. //}
  145.  
  146. area = surfaceArea(a);
  147. vol = volume(a);
  148. svol = sphereVol(a);
  149.  
  150. printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t\t%.2f\n",a,area,vol,svol);
  151.  
  152. //} //end while
  153. return 0;
  154. } //end main
  155.  
  156. float surfaceArea(float a){
  157. float result;
  158. result = 6 * a * a;
  159. return result;
  160. }
  161.  
  162. float volume(float a){
  163. float result;
  164. result = a * a *a;
  165. return result;
  166. }
  167.  
  168. float sphereVol(float a){
  169. float result,radius;
  170. radius=0.5*a;
  171. result = (4/3) * pi * radius * radius * radius;
  172. return result;
  173. }
  174.  
  175.  
  176.  
  177.  
  178. SAMPLE SOLUTION A:
  179.  
  180. B. SOURCE CODE B:
  181. #include <stdio.h>
  182. #define pi 3.1415926
  183. float surfaceArea(float a);
  184. float volume(float a);
  185. float sphereVol(float a);
  186.  
  187. int main(void){
  188. float a;
  189. float area,vol,svol;
  190.  
  191. printf("a\t\tSurfaceArea\t\tVolOfCube\t\t VolOfSphere\n\n");
  192.  
  193. while(1){
  194. printf("Please enter the value of a: ");
  195. scanf("%f",&a);
  196.  
  197. if(a==0){
  198. break;
  199. }
  200.  
  201. area = surfaceArea(a);
  202. vol = volume(a);
  203. svol = sphereVol(a);
  204.  
  205. printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t\t%.2f\n",a,area,vol,svol);
  206.  
  207. } //end while
  208. return 0;
  209. } //end main
  210.  
  211. float surfaceArea(float a){
  212. float result;
  213. result = 6 * a * a;
  214. return result;
  215. }
  216.  
  217. float volume(float a){
  218. float result;
  219. result = a * a *a;
  220. return result;
  221. }
  222.  
  223. float sphereVol(float a){
  224. float result,radius;
  225. radius=0.5*a;
  226. result = (4/3) * pi * radius * radius * radius;
  227. return result;
  228. }
  229.  
  230. SAMPLE SOLUTION B:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement