M4ritimeSeeker

PASS Week 9/29

Sep 28th, 2020 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. REVIEW OF FUNCTIONS
  2.  
  3.  
  4.  
  5.  
  6. Review the following code and understand the function, the flow of control and when it returns to the caller.
  7.  
  8. /* This program demonstrates that one function can be
  9. called multiple times.
  10. Written by:
  11. Date:
  12. */
  13.  
  14. #include <stdio.h>
  15.  
  16. // Function Declarations
  17. void printOne (int x);
  18.  
  19. int main (void)
  20. {
  21. // Local Declarations
  22. int a;
  23.  
  24. // Statements
  25. a = 5; // First call
  26. printOne (a);
  27.  
  28. a = 33;
  29. printOne (a); // Second call
  30.  
  31. // Done. Return to operating system.
  32. return 0;
  33.  
  34. } // End main
  35.  
  36. /* =================== printOne ===================
  37. Print one integer value.
  38. Pre-call: x contains number to be printed
  39. Post-call: value in x printed
  40. */
  41.  
  42. void printOne (int x)
  43. {
  44. // Statements
  45. printf("%d\n", x);
  46. return;
  47.  
  48. } // End printOne
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. Problem #1 A thru C
  68.  
  69. A. Write a program that prompts the user to enter in a value for the base and side of a
  70. square. Create a variable to hold the area of the square. Compute the area using the
  71. pow() function in the math.h library. Print out the area of the square with a
  72. minimum width of 8 and precision of 2 decimal places to the right.
  73.  
  74. B. Now compose a function to perform the calculation of the area of a square. First
  75. identify the data the function will need to take as parameters and then identify the
  76. value if any that would be returned from the function. Now create the function
  77. declaration in the global are of the source code. Copy or rewrite your processes in a
  78. function definition under the main function. Test your function by
  79. running the program. Does it work with floating point values? What changes if any
  80. do you need to make to take floating point parameters and return a floating point
  81. value?
  82.  
  83. C. Modify the program in part #A by adding functions to prompt the user and to
  84. get the user input and to provide output to the user by taking arguments. Your main
  85. function should only contain functions calls and a return statement.
  86.  
  87. NOTE: If you compile this on a Linux machine you must link the math library using the “‐lm”
  88. argument Example: gcc source.c ‐lm
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. Problem #2
  108.  
  109. Write a program that uses functions to compute the perimeter and area of a right triangle
  110. when given the length of the two sides (a and b). Hint: Use three functions, one for
  111. calculating c, a second for the area, and a third for the perimeter.
  112.  
  113. c^2 = a^2 + b^2
  114. area = .5 * (a * b)
  115. perimeter = a + b + c
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. RANDOM NUMBERS
  136.  
  137.  
  138. Problem #3
  139.  
  140. Write a program that generates a random number using a call to the random number
  141. function and prints it to the screen. Call the random number functions several times in the program and see what happens!
  142. Run the program 2‐3 times and observe the results.
  143. Now add the srand() call with the time() argument to add randomness to the numbers. You
  144. ONLY need to call srand() ONCE in the program before the rand() calls.
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. Problem #4
  152.  
  153. Create a program that will generate a random set of numbers between 10 and 20 inclusively.
  154.  
  155. Example:
  156. First, set the range: range = (max value - min value) + 1;
  157. Second, shift to the right: rand() % range + 1; //this starts the counting at 1 instead of zero.
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. LOGICAL OPERATOR REVIEw
  179.  
  180.  
  181.  
  182. Examine the following code by compiling it and running it.
  183.  
  184. /* Demonstrate the results of logical operators.
  185. Written by:
  186. Date:
  187. */
  188.  
  189. #include <stdio.h>
  190. #include <stdbool.h>
  191.  
  192. int main (void)
  193. {
  194.  
  195. // Local Declarations
  196. bool a = true;
  197. bool b = true;
  198. bool c = false;
  199.  
  200. // Statements
  201. printf(" %2d AND %2d: %2d\n", a, b, a && b);
  202. printf(" %2d AND %2d: %2d\n", a, c, a && c);
  203. printf(" %2d AND %2d: %2d\n", c, a, c && a);
  204. printf(" %2d OR %2d: %2d\n", a, c, a || c);
  205. printf(" %2d OR %2d: %2d\n", c, a, c || a);
  206. printf(" %2d OR %2d: %2d\n", c, c, c || c);
  207. printf("NOT %2d AND NOT %2d: %2d\n", a, c, !a && !c);
  208. printf("NOT %2d AND %2d: %2d\n", a, c, !a && c);
  209. printf(" %2d AND NOT %2d: %2d\n", a, c, a && !c);
  210. return 0;
  211.  
  212. } // End main
  213.  
  214. /* Results:
  215. 1 AND 1: ?
  216. 1 AND 0: ?
  217. 0 AND 1: ?
  218. 1 OR 0: ?
  219. 0 OR 1: ?
  220. 0 OR 0: ?
  221. NOT 1 AND NOT 0: ?
  222. NOT 1 AND 0: ?
  223. 1 AND NOT 0: ?
  224. */
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. Examine the following code by compiling it and running it.
  234.  
  235. /* Demonstrates the results of relational operators.
  236. Written by:
  237. Date:
  238. */
  239.  
  240. #include <stdio.h>
  241.  
  242. int main (void)
  243. {
  244.  
  245. // Local Declarations
  246. int a = 5;
  247. int b = -3;
  248.  
  249. // Statements
  250. printf(" %2d < %2d is %2d\n", a, b, a < b);
  251. printf(" %2d == %2d is %2d\n", a, b, a == b);
  252. printf(" %2d != %2d is %2d\n", a, b, a != b);
  253. printf(" %2d > %2d is %2d\n", a, b, a > b);
  254. printf(" %2d <= %2d is %2d\n", a, b, a <= b);
  255. printf(" %2d >= %2d is %2d\n", a, b, a >= b);
  256. return 0;
  257.  
  258. } // main
  259.  
  260. /* Results:
  261. 5 < -3 is ?
  262. 5 == -3 is ?
  263. 5 != -3 is ?
  264. 5 > -3 is ?
  265. 5 <= -3 is ?
  266. 5 >= -3 is ?
  267. */
  268.  
  269.  
Add Comment
Please, Sign In to add comment