Advertisement
M4ritimeSeeker

PASS Week 9/22

Sep 20th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1.  
  2. FLOW OF CONTROL/FUNCTIONS EXAMPLE PROBLEM
  3.  
  4. Answer the questions in the comments of this code BEFORE running it.
  5. Run it afterward to check your results!
  6.  
  7. #include <stdio.h>
  8.  
  9. //What are these two below statements called?
  10. int myFunction1(void);
  11. int myFunction2(int);
  12.  
  13. // What order do the functions execute in this program (Including main)?
  14. // When does each function begin and end when the program is executed? (Extension of prev question)
  15. int main(void){
  16.  
  17. int x = 7;
  18. int y = 0;
  19.  
  20. myFunction1(); //Computes something
  21. myFunction2(x); //Computes something
  22.  
  23. y = myFunction1() + myFunction2(x); //What does y equal?
  24.  
  25. //What type of function is printf?
  26. //What type are myFunction1 and myFunction2?
  27. printf("%d\n", y);
  28.  
  29. return 0;
  30. }
  31.  
  32. int myFunction1(void){
  33. return 5;
  34. }
  35.  
  36. int myFunction2(int myVar){
  37. return myVar + 2;
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. POINTER QUESTIONS
  57.  
  58.  
  59. What 3 different values/information does a variable store upon declaration?
  60. (For example, what 3 pieces of information does the statement "int x = 10" store?)
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. Answer the questions in the comments of this code.
  77. Run it afterward to check your results, try changing the statements/values and see what you get!
  78.  
  79.  
  80.  
  81. #include <stdio.h> /* for printf() */
  82. int main(void)
  83. {
  84. int i;
  85. int *p = &i; //What value does variable *p store?
  86.  
  87. printf("The address of i is %p.\n", (void*) p); //What will this print? (Remember what the %p formatter does)
  88.  
  89. return 0;
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. What does this program do?
  102. (You don't have to know the variable values, just what the program will do)
  103.  
  104.  
  105. //Function Prototype
  106. void exchange(int* pa, int* pb);
  107.  
  108. int main(void){
  109.  
  110. int a, b;
  111. exchange(&a, &b);
  112.  
  113. return 0;
  114. }
  115.  
  116. /* Function: exchange */
  117. void exchange(int* pa, int* pb) {
  118. int c;
  119. c = *pa;
  120. *pa = *pb;
  121. *pb = c;
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. PRACTICE PROBLEMS
  139.  
  140.  
  141. Problem #1 multi‐part problem Pass By Value
  142. (THERE WILL BE 3 PROGRAMS TOTAL, THEY ALL DO THE SAME THING BUT IN DIFFERENT WAYS)
  143.  
  144. A (First program). Write a program that prompts the user to enter in a value for the base and
  145. side of a square. Create a variable to hold the area of the square. Compute the
  146. area. Print out the area of the square with a minimum width of 8 and
  147. precision of 2 decimal places to the right.
  148.  
  149. B (Second Program). Now compose a function to perform the calculation of the area of a square.
  150. First identify the data the function will need to take as parameters and then
  151. identify the value if any that would be returned from the function. Now
  152. create the function PROTOTYPE in the GLOBAL AREA of the source code. Copy or
  153. rewrite your processes in a function definition UNDER the main function (Not INSIDE the main function).
  154. Test your function by running the program. Does it work with
  155. floating point values? What changes if any do you need to make to take
  156. floating point parameters and return a floating point value?
  157.  
  158. C (Third Program). Modify the program in problem #1 by adding functions to prompt the user
  159. and to get the user input and to provide output to the user by taking
  160. arguments. Your main function should only contain functions calls and a
  161. return statement.
  162.  
  163. D. Review and Discuss the solutions (Pastebin of Solutions will be provided after completion)
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. Problem #2 (Read this problem more than once, the wording is confusing):
  185.  
  186. Write a program that uses a void function that receives two integer variables and a pointer to a floating‐point
  187. variable (use type DOUBLE for this problem) and sends back the integer and decimal parts as
  188. integers using pointers up to the hundredth place.
  189. So if a user enters in 98.65 , the function should return 98 and 65. Hint use modulus and division.
  190.  
  191. IN YOUR MAIN FUNCTION
  192. declare your two integer variables and prompt for the user to input a floating point number, then call the
  193. function described above and output the parts from main after the function returns them using
  194. pointers (I.e. Give the function values and let it run, THEN output the results in main).
  195.  
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement