M4ritimeSeeker

PASS Week 8: User Defined Functions/Flow of Control

Oct 19th, 2021
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. User Defined Function Structure (NOT A PRACTICE PROBLEM):
  2.  
  3. This is here if you struggle with how to structure a user-defined function. Use this format.
  4. The C code that describes what a function does is the function DEFINITION (Shown below).
  5.  
  6. dataType function_name (parameters with data types){
  7. Local declarations
  8. Local statements including the return statement.
  9. }
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. PROBLEM #1: Small User-Defined Function
  26.  
  27. Compose a program that will print a simple message to the user. You will create a user-defined function to print the message.
  28. Your main function should call the user defined function with NO arguments and with NO assigned return value.
  29. As well, besides your "return 0;" statement, main should ONLY CONTAIN A SINGLE FUNCTION CALL TO THE FUNCTION YOU MADE.
  30. Your user defined function will take no arguments and return void.
  31. Inside your user function you will print the following message from the sample output below.
  32.  
  33. Note: You should declare explicitly when a user-defined function will expect void and will return void.
  34.  
  35. SAMPLE OUTPUT:
  36.  
  37. A message for you: Have a nice day!
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. PROBLEM #2: Multi-part Math with/without Functions
  55.  
  56. A. Write a program that prompts the user to enter in a value for the base
  57. of a square. Compute the area.
  58. Then print out the area of the square with a minimum width of 8 and
  59. precision of 2 decimal places to the right. NO FUNCTIONS NEEDED FOR PART A.
  60.  
  61. SAMPLE OUTPUT PART A:
  62.  
  63. Enter the base: 6
  64. 36.00
  65.  
  66.  
  67. B. Now compose a function to perform the SAME calculation of the area of a square, except with a function.
  68. Use the working code from the Part A to perform the calculations inside
  69. your function definition. You will need to figure out what the function needs
  70. for parameters (if any) and what return value it will need (if any).
  71. KEEP ALL OF YOUR MATH INSIDE THE FUNCTION. Variables, input, and output can go anywhere you want.
  72.  
  73. SAMPLE OUTPUT PART B:
  74.  
  75. Enter the base: 6
  76. 36.00
  77.  
  78.  
  79. C. Modify the program from Part B, and split every step into individual functions. For example
  80. - One function to print the input message: printMessage()
  81. - One function to assign the input: getInput()
  82. - One function to compute the area: computeArea()
  83. - One function to print the area to the screen: printOutput()
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. PROBLEM #3: Computing Hypotenuse
  99.  
  100. Compose a program that calculates the hypotenuse of a right triangle using multiple user defined functions.
  101. Your program should have a main function, a print instructions function, two get input functions, and a computation function.
  102. The main function should only have ONE function call to the ComputeHypotenuse function and ONE return statement.
  103. Your program must prompt the user to enter in two double type values. The program will then use a function to
  104. compute the hypotenuse of a triangle C given the two values of sides A and B.
  105.  
  106. The equation you will use is A^2 + B^2 = C^2
  107.  
  108. Use the following function prototypes:
  109. void PrintInstructions(void);
  110. double GetSideA(void);
  111. double GetSideB(void);
  112. void ComputeHypotenuse(void);
  113.  
  114.  
  115. SAMPLE OUTPUT:
  116.  
  117. Enter in a value for side A: 5
  118. Enter in a value for side B: 6
  119. C squared is equal to: 61.000000
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. PROBLEM #4 Compute a Fixed Monthly Payment (Useful for Project 2)
  134.  
  135. Compose a program to calculate a fixed monthly payment. Use JUST main and one function.
  136. THIS USES AMORTIZATION. If you google "amortization calculator", you should find the formula used.
  137.  
  138. -Main will prompt the user for inputs for annual percentage rate, principle, period in years.
  139. -The function will take these values as arguments, compute the fixed monthly payment given these parameters and return the fixed
  140. monthly payment.
  141.  
  142. Finally main should print this to screen using dollar signs and precision of 2 places to the right of the decimal.
  143. Given a loan of $100000, an APR of 5% and a term of 30 years, the fixed payment should be $536.82
  144.  
  145. SAMPLE OUTPUT:
  146.  
  147. Enter the APR: 5
  148. Enter the Principle: 100000
  149. Enter the Years: 30
  150. Your Fixed Monthly Payment is: $ 536.82
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. Bonus Problem / Challenge
  163.  
  164. The value of pi (π) can be calculated by the following formula:
  165.  
  166. Θ = ✓(6 x (1/1^2) + (1/2^2) + (1/3^2) + (1/4^2) + ... + (1/limit^2) )
  167.  
  168. When you get to this problem, ask Justin to write the formula on the board.
  169. Thats much easier to read than that mess, but pastebin can't embed images so its the best I could do.
  170.  
  171. Write a program that uses this formula to calculate pi. Each operation in the
  172. parentheses will be repeated to the number of the limit (n). Note, the program will
  173. prompt the user for the “limit” value. Test your program once a limit of 5 terms, 10
  174. terms and a limit of 20 terms. Print the results of each and observe how this
  175. algorithm gets more accurate depending on how many terms you use.
  176.  
  177. SAMPLE OUTPUT (Limit = 1):
  178.  
  179. Enter the limit of terms to compute Pi.
  180. 1
  181. Pi = 2.449490
  182.  
  183.  
  184. SAMPLE OUTPUT (Limit = 10000):
  185.  
  186. Enter the limit of terms to compute Pi.
  187. 10000
  188. Pi = 3.141497
Advertisement
Add Comment
Please, Sign In to add comment