M4ritimeSeeker

PASS Week 2/17

Feb 14th, 2021 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. REFERENCE MATERIAL
  2.  
  3.  
  4. Structure of a Function in C:
  5. The portion of C code that contains the program of a function is the function definition.
  6. The following illustrates it’s form.
  7.  
  8. /* You would put your block comment here describing what the function does */
  9.  
  10. type function_name (parameters with types){
  11. Local declarations
  12. Local statements including the return statement.
  13. }
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. Example Code for Pointers:
  21. This code will swap the values of A and B using a function and pointers.
  22.  
  23. void exchange(int*, int*); //Function prototype
  24.  
  25. int main(void){
  26. int a, b;
  27. exchange(&a,&b); //Function call
  28.  
  29. return 0;
  30. }
  31.  
  32. /* Function definition */
  33. void exchange(int* pa, int* pb)
  34. {
  35. int c;
  36. c = *pa;
  37. *pa = *pb;
  38. *pb = c;
  39. return;
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. FLOW OF CONTROL/FUNCTIONS EXAMPLE PROBLEM
  57.  
  58. Answer the questions in the comments of this code BEFORE running it.
  59. Run it afterward to check your results!
  60.  
  61.  
  62.  
  63. #include <stdio.h>
  64.  
  65. //What are these two below statements called?
  66. int myFunction1(void);
  67. int myFunction2(int);
  68.  
  69. // List the chronological order the functions execute in this program (Including main)
  70. int main(void){
  71.  
  72. int x = 7;
  73. int y = 0;
  74.  
  75. myFunction1(); //What does this do? What arguments does this take?
  76. myFunction2(x); //What does this do? What arguments does this take?
  77.  
  78. y = myFunction1() + myFunction2(x); //What does y equal?
  79.  
  80. //What is different between the printf function and myFunction1/myFunction2?
  81. printf("%d\n", y);
  82.  
  83. return 0;
  84. }
  85.  
  86. int myFunction1(void){
  87. return 5;
  88. }
  89.  
  90. int myFunction2(int myVar){
  91. return myVar + 2;
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. Answer the questions in the comments of this code.
  109. Run it afterward to check your results, try changing the statements/values and see what you get!
  110.  
  111. #include <stdio.h> /* for printf() */
  112.  
  113. int main(void)
  114. {
  115. int i;
  116. int* p = &i; //What value does variable *p store?
  117.  
  118. printf("%p\n", p); //What will this print? (Remember what the %p formatter does)
  119.  
  120. return 0;
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. Problem #1 Using pointers
  142.  
  143. Construct a program that prompts the user to enter in two type double values and
  144. store them into two type double variables. Then declare two pointer variables and assign them to point to
  145. the variables declared in the previous step. Now add the two inputs together USING THE
  146. POINTERS to the two variables and assign the result to a new value variable, print it to
  147. screen.
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. Problem #2 Pass by Reference
  162.  
  163. Using the program from problem 1, create a function called “AddNbrs()” that takes
  164. three double pointer variables and returns nothing to the caller. Keep the two input
  165. variables and the user prompts and outputs within main. Move the expression that adds the
  166. values into the new function. Then assign the result using the
  167. pointers passed to the function from main.
  168.  
  169. /*** SKELETON CODE ***/
  170.  
  171. #include <stdio.h>
  172.  
  173. void AddNbrs(double*, double*, double*); //Function prototype given
  174.  
  175. int main()
  176. {
  177.  
  178. //START PROGRAMMING HERE
  179.  
  180. return 0;
  181. }
  182.  
  183. //WRITE FUNCTION HERE
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. Problem #3
  198.  
  199. Create a program that makes use of pointers to take in two user-supplied values, being the
  200. length of 2 sides of a 4 sided polygon (square or rectangle). Your program should
  201. prompt the user from inside main to enter in the length of two adjacent sides of a 2-
  202. sided polygon. It should then call a single function to compute the area AND perimeter of the
  203. polygon and return those values using pointers to the calling function. The output
  204. should be presented in a meaningful way to the user.
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. Problem #4
  220.  
  221. Create a program that prompts the user to enter in a value for number of
  222. seconds using a long data type. Create a function that when given a value
  223. representing seconds will return the number of hours, minutes and seconds
  224. contained in that value of seconds. While the function can take the seconds variable
  225. as input, the three measures should be returned to the calling
  226. function by using pointers.
  227.  
  228. You may assume that the user will not enter more than 24 hours worth of seconds.
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240. Problem #5:
  241.  
  242. Write a program that uses a function that receives a pointer to a type double
  243. number and two integer variables and sends back the integer and fraction parts as
  244. integers using pointers up to the hundredth place. In your main
  245. function prompt for the user to input a type double number, then call the
  246. function, then output the parts from main after the function returns them using
  247. pointers. Hint use modulus and division.
  248.  
  249. So if a user enters in 98.65 , the function should return 98 and 65
Add Comment
Please, Sign In to add comment