M4ritimeSeeker

PASS Week 9: User Defined Functions

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