M4ritimeSeeker

PASS Week 2/24

Feb 21st, 2021 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. Structure of a Function in C:
  2. The portion of C code that contains the program of a function is the function definition.
  3. The following illustrates it’s form.
  4.  
  5. /* You would put your block comment here describing what the function does */
  6.  
  7. type function_name (parameters with types){
  8. Local declarations
  9. Local statements including the return statement.
  10. }
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. Review of Functions and passing by value:
  23.  
  24. Review the following code and understand the function, the flow of control and when
  25. it returns to the caller.
  26.  
  27. /* This program demonstrates that one function can be
  28. called multiple times.
  29. Written by:
  30. Date:
  31. */
  32.  
  33. #include <stdio.h>
  34.  
  35. // Function Declarations
  36. void printOne (int x);
  37.  
  38. int main (void)
  39. {
  40. // Local Declarations
  41. int a;
  42.  
  43. // Statements
  44. a = 5;
  45. printOne (a); //first call
  46.  
  47. a = 33;
  48. printOne (a); // Second call
  49.  
  50. // Done. Return to operating system.
  51. return 0;
  52. } // main
  53.  
  54. /* =================== printOne ===================
  55. Print one integer value.
  56. Pre x contains number to be printed
  57. Post value in x printed
  58. */
  59.  
  60. void printOne (int x)
  61. {
  62. // Statements
  63. printf("%d\n", x);
  64. return;
  65. } // printOne
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. Example Code for Pointers:
  83. This code will swap the values of A and B using a function and pointers.
  84.  
  85. void exchange(int*, int*); //Function prototype
  86.  
  87. int main(void){
  88. int a, b;
  89. exchange(&a,&b); //Function call
  90.  
  91. return 0;
  92. }
  93.  
  94. /* Function definition */
  95. void exchange(int* pa, int* pb)
  96. {
  97. int c;
  98. c = *pa;
  99. *pa = *pb;
  100. *pb = c;
  101. return;
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. Review of Pointers:
  117.  
  118. Construct a program that prompts the user to enter in two values and store them into variables
  119. of type double. Declare two pointer variables of type double and assign them to point to
  120. the variables declared in the previous step. Now add the two inputs together using the
  121. POINTERS to the two variables and assign the result to a new value variable, print it to
  122. screen.
  123.  
  124. /* SKELETON CODE */
  125.  
  126. #include <stdio.h>
  127.  
  128. int main()
  129. {
  130. double var1 = 0.0;
  131. double var2 = 0.0;
  132. double result = 0.0;
  133.  
  134. //POINTERS GO HERE
  135.  
  136.  
  137. printf("Enter in two values:\n");
  138. scanf("%lf %lf", &var1, &var2);
  139.  
  140. //MATH GOES HERE
  141.  
  142. printf("%.2lf and %.2lf = %.2lf\n", var1, var2, result);
  143.  
  144. return 0;
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. PRACTICE PROBLEMS
  161.  
  162.  
  163.  
  164. Problem #1
  165.  
  166. A. Write a program that prompts the user to enter in a value for the base and side of a
  167. square. Create a variable that will hold the area of the square. Compute the area using the
  168. pow() function in the math.h library. Print out the area of the square with a
  169. minimum width of 8 and precision of 2 decimal places to the right.
  170. (NO FUNCTIONS OR POINTERS USED IN PART A)
  171.  
  172.  
  173. B. Now compose a function to perform the calculation of the area of a square. The parameters
  174. will be the base of the square, and the return value will be the area. Now create the function
  175. prototype in the global area of the source code. Copy the function prototype and
  176. paste it under main, swap out the semicolon with an open curly brace
  177. and write your function definition.
  178. Test your function by running the program.
  179.  
  180.  
  181.  
  182. BONUS: Modify your program from part B to use pointers. It should take only pointer parameters,
  183. and have no return values (Meaning it returns void). Remember, the function TYPE must change to void to return void.
  184.  
  185. For example: "double calcArea(Your Parameters here){" returns a double, and you need to
  186. change it to "void calcArea(Your Parameters Here){" to return void (i.e. it doesn't have a value to return).
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. Problem #2
  200.  
  201. Write a program that generates a few random numbers using a call to the random number
  202. function and prints it to the screen. Run the program 2‐3 times and observe the results.
  203. Now add the srand() call with the time() argument to add randomness to the numbers. Run
  204. the program a few more times and observe the results.
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213. Problem #3:
  214.  
  215. Create a program that will generate a random set of numbers between 10 and 20 inclusively.
  216.  
  217. Example:
  218. First, set the range: range = (max value - min value) + 1;
  219. Second, shift to the right: rand() % range + 1; //this starts the counting at 1 instead of zero.
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. Examine the following expressions and determine the result. 1 or 0?
  234.  
  235. 1.) 1 > 5
  236.  
  237. 2.) x > 10 given int x = 10;
  238.  
  239. 3.) (x > 10) || (y > 20) given int x = 7; int y = 20;
  240.  
  241. 4.) (x > 10) && (y > 20) given int x = 7; int y = 20; Does the second expression even happen?
  242.  
Add Comment
Please, Sign In to add comment