M4ritimeSeeker

PASS Week 2/10

Feb 8th, 2021 (edited)
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. Examine and discuss the following program (Try running it multiple times and change the value of variable "i"):
  2.  
  3. #include <stdio.h>
  4.  
  5. int main(void)
  6. {
  7. int i = 99;
  8. printf("The address of i is %p.\n", &i);
  9. return 0;
  10. }
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. Structure of a Function in C:
  24. The portion of C code that contains the program of a function is the function definition.
  25. The following illustrates it’s form.
  26.  
  27. /* You would put your block comment here describing what the function does */
  28.  
  29. type function_name (parameters with types){
  30. Local declarations
  31. Local statements including the return statement.
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. PROBLEM #1 Basic Function
  46.  
  47. Compose a program that will print the message "A message for you: Have a nice day!"
  48. to the user and then exit returning a 0 to the system on completion. Here is the catch, your
  49. printf() statement DOES NOT go in your main function. Your main function should call
  50. the user-defined function titled "PrintMessage()" with no parameters and
  51. with no return values being used in main. Your user defined function will take
  52. no arguments and return void.
  53.  
  54. Inside your user function you will use the printf() function to print the following:
  55. "A message for you: Have a nice day!"
  56.  
  57.  
  58.  
  59. /*** SKELETON CODE FOR PROBLEM 1 ***/
  60.  
  61.  
  62. #include <stdio.h>
  63.  
  64. //FUNCTION PROTOTYPE (Declaration) GOES HERE
  65.  
  66. int main(void){
  67.  
  68.  
  69. return 0;
  70. }
  71.  
  72. //START USER FUNCTION HERE
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. PROBLEM #2 Multi‐part program: Pass By Value
  93.  
  94. A. Write a program that prompts the user to enter in values for the base and
  95. side of a square, calculates the area of the square, then prints the result to the user.
  96. Create floating point variables to hold the base, side, and area of the square.
  97. Then compute the area.
  98. Print out the area of the square with a minimum width of 8 and
  99. precision of 2 decimal places to the right.
  100. (NO USER FUNCTIONS ARE NEEDED FOR THIS PART. JUST PROGRAM IN MAIN)
  101.  
  102.  
  103.  
  104. B. Now modify the program you just made in Part A.
  105. Compose a function to perform the calculation of the area of a square.
  106. Use the code from Part A to perform the calculations, and move it inside
  107. your new function.
  108.  
  109. HOW TO STRUCTURE THE FUNCTION:
  110. First identify the data the function will need to take
  111. as parameters, and then identify the value if any that would be returned
  112. from the function. Now create the function prototype (declaration) in the global area of
  113. the source code. Copy and paste your function prototype under the main function,
  114. and begin writing the function definition (Refer to Problem 1's structure).
  115.  
  116. Test your function by running the program. Does it work with floating point values?
  117. What changes if any do you need to make to take floating point parameters
  118. and return a floating point value?
  119.  
  120.  
  121.  
  122. C. Modify the program in problem #1 by adding separate functions for prompting the user,
  123. getting the user input, and providing output to the user by taking
  124. arguments. Your main function should only contain functions calls and a
  125. return statement to return the values to the calling function.
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. Problem #3
  144.  
  145. Compose a program that will compute the hypotenuse of a triangle.
  146. This program uses multiple user-defined functions. Your program should have a
  147. main function, a print instructions function, two get input functions, and a compute hypotenuse function.
  148.  
  149. The main function should ONLY BE COMPRISED OF FOUR FUNCTION CALL to the functions listed below and
  150. one return statement. Your program must prompt the user to enter in two double type values for the triangle sides.
  151. One function for getting side A and one for getting side B. The program will then use a function to
  152. compute the hypotenuse of a triangle given the two values of sides A and B.
  153.  
  154. For those who do not recall the equation is the Pythagorean Theorem A^2 + B^2 = C^2.
  155.  
  156.  
  157. Create and use the following functions:
  158. void PrintInstructions( );
  159. double GetSideA( );
  160. double GetSideB( );
  161. void ComputeHypotenuse( );
  162.  
  163. The challenge here is to know when to call what function and what to do with its return
  164. values. The main function will only contain 2 local statements: a function call (ComputeHypotenuse)
  165. and a return statement (i.e. return 0).
  166.  
  167.  
  168.  
  169. /** SKELETON CODE FOR PROBLEM 3 **/
  170.  
  171. #include <stdio.h>
  172.  
  173. //FUNCTION PROTOTYPES GO HERE
  174.  
  175. int main(void){
  176. void PrintInstructions( );
  177. double GetSideA( );
  178. double GetSideB( );
  179. ComputeHypotenuse();
  180. return 0;
  181. }
  182.  
  183. //PRINT INSTRUCTIONS FUNCTION GOES HERE
  184.  
  185. //GET SIDE A FUNCTION GOES HERE
  186.  
  187. //GET SIDE B FUNCTION GOES HERE
  188.  
  189. //COMPUTER HYPOTENUSE FUNCTION GOES HERE
Add Comment
Please, Sign In to add comment