Advertisement
M4ritimeSeeker

Pass Week 9/22 Solutions

Sep 20th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. PROBLEM 1 SOLUTIONS
  2.  
  3.  
  4.  
  5. Problem #1A Solution
  6.  
  7.  
  8.  
  9. #include <stdio.h>
  10.  
  11. int main(void){
  12. //Local Declarations
  13. float base = 0.0;
  14. float area = 0.0;
  15.  
  16. //Local Statements
  17.  
  18. //prompt user
  19. printf("Enter in a value for the base of a square.\n");
  20.  
  21. //get input
  22. scanf("%f", &base);
  23.  
  24. //process data
  25. area = base * base;
  26.  
  27. //output information
  28. printf("The area of a square is %8.2f\n", area);
  29.  
  30. return 0;
  31. }//end main
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. Problem #1B Solution
  48.  
  49.  
  50.  
  51. #include <stdio.h>
  52.  
  53. float ComputeAreaOfSquare(float);
  54.  
  55. int main(void){
  56.  
  57. //Local Declarations
  58. float base = 0.0;
  59. float area = 0.0;
  60.  
  61. //Local Statements
  62.  
  63. //prompt user
  64. printf("Enter in a value for the base of a square.\n");
  65.  
  66. //get input
  67. scanf("%f", &base);
  68.  
  69. //process data by calling your function
  70. area = ComputeAreaOfSquare(base);
  71.  
  72. //output information
  73. printf("The area of a square is %8.2f\n", area);
  74.  
  75. return 0;
  76. }//end main
  77.  
  78.  
  79. //You would write function descriptions here
  80. float ComputeAreaOfSquare(float baseIn){
  81.  
  82. float area = baseIn * baseIn;
  83. return area;
  84.  
  85. }//end function
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. Problem #1C Solution
  105.  
  106.  
  107.  
  108. #include <stdio.h>
  109.  
  110. void PrintInstructions(void);
  111. float GetInput(void);
  112. float ComputeAreaOfSquare(float);
  113. void PrintOutput(float);
  114.  
  115. int main(void){
  116.  
  117. //Local Declarations
  118. float base = 0.0;
  119. float area = 0.0;
  120. //Local Statements
  121. PrintInstructions();
  122. base = GetInput();
  123. area = ComputeAreaOfSquare(base);
  124. PrintOutput(area);
  125. return 0;
  126.  
  127. }//end main
  128.  
  129.  
  130. //You would write function descriptions here
  131. void PrintInstructions(){
  132.  
  133. printf("Enter in a value for the base of a square.\n");
  134. return;
  135.  
  136. }//end function
  137.  
  138.  
  139. //You would write function descriptions here
  140. float GetInput(){
  141.  
  142. float baseIn = 0.0;
  143. scanf("%f", & baseIn);
  144. return baseIn;
  145.  
  146. }//end function
  147.  
  148.  
  149. //You would write function descriptions here
  150. float ComputeAreaOfSquare(float baseIn){
  151.  
  152. float area = baseIn * baseIn;
  153. return area;
  154.  
  155. }//end function
  156.  
  157.  
  158. void PrintOutput(float areaIn){
  159.  
  160. printf("The area of a square is %8.2f\n", areaIn);
  161. return;
  162.  
  163. }//end function
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. Problem #1 Solution Review and Discussion
  174. 1. All function declarations should be in the global declarations area.
  175.  
  176. 2. Notice the formal return statements in each function.
  177.  
  178. 3. Notice that I have not added function descriptions. This is for brevity in
  179. the lecture, you should include them on your deliverables.
  180.  
  181. 4. In the final solution notice how clean that the main function is? Its
  182. mostly all function calls.
  183.  
  184. 5. We have assumed all data entered was valid and in range. Normally you
  185. would always test input before processing it.
  186.  
  187. 6. All of these problems are using the design construct of sequence. In
  188. coming modules we will be using selection and repetition.
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. Problem #2 Solution
  213.  
  214.  
  215.  
  216. #include <stdio.h>
  217.  
  218. void GetParts(int*, int*, double);
  219.  
  220. int main(void){
  221.  
  222. double srcNbr = 0.0;
  223. int nbr1 = 0;
  224. int nbr2 = 0;
  225.  
  226. printf("Enter in a floating point value: ");
  227. scanf("%lf", &srcNbr);
  228.  
  229. GetParts(&nbr1, &nbr2, srcNbr);
  230.  
  231. printf("The whole number is: %d and the fractional is: %d\n", nbr1, nbr2);
  232.  
  233. return 0;
  234.  
  235. }//end main
  236.  
  237.  
  238. void GetParts(int* nbr1, int* nbr2, double srcNbr){
  239.  
  240. int whole, fractional;
  241. double fraction;
  242.  
  243. whole = (int) srcNbr;
  244. fraction =(srcNbr * 100) / 100 - whole;
  245. fractional = (int)(fraction*100);
  246.  
  247. *nbr1 = whole;
  248. *nbr2 = fractional;
  249.  
  250. return;
  251.  
  252. }//end function
  253.  
  254.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement