Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- User Defined Function Structure (NOT A PRACTICE PROBLEM):
- This is here if you struggle with how to structure a user-defined function. Use this format.
- The C code that describes what a function does is the function DEFINITION (Shown below).
- This is also a helpful link for the structure of a function:
- https://i0.wp.com/coder-tronics.com/wp-content/uploads/2014/02/Function-breakdown.png
- dataType function_name (parameters with data types){
- Local declarations
- Local statements including the return statement.
- }
- This link shows an example of passing arguments to a function
- https://cdn.programiz.com/sites/tutorial2program/files/pass-arguments-c-programming.jpg
- PROBLEM #1: Small User-Defined Function
- 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.
- You will call your function IN MAIN with NO arguments and with NO assigned return value.
- Your user defined function will take no parameters and return void.
- Inside your user function you will print the following message from the sample output below.
- Note: You should declare explicitly when a user-defined function will expect void and will return void.
- SAMPLE OUTPUT:
- A message for you: Have a nice day!
- PROBLEM #2: Multi-part Math; Pass By Value
- A. Write a program that prompts the user to enter in a value for the base of a square. Compute the area.
- Then print out the area of the square with a minimum width of 8 and
- precision of 2 decimal places to the right. NO FUNCTIONS NEEDED FOR PART A.
- SAMPLE OUTPUT PART A:
- Enter the base: 6
- 36.00
- B. Now compose a function to perform the SAME calculation of the area of a square.
- Use the working code from the Part A to perform the area calculation inside
- your function definition. You will need to figure out what the function needs
- for parameters (if any) and what return value it will need (if any).
- KEEP ALL OF YOUR MATH INSIDE THE FUNCTION. Your printf/scanf will stay in main.
- SAMPLE OUTPUT PART B:
- Enter the base: 6
- 36.00
- C. Modify the program from Part B, and split every step into individual functions. For example
- - One function to print the input message: printMessage()
- - One function to assign the input: getInput()
- - One function to compute the area: computeArea()
- - One function to print the area to the screen: printOutput()
- You can decide the function parameters and return types for the 4 functions. There are multiple ways to do it.
- PROBLEM #3: Computing Hypotenuse
- Compose a program that calculates the hypotenuse of a right triangle using multiple user defined functions.
- Your program should have a main function, a print instructions function, two get input functions, and a computation function.
- The main function should only have ONE function call to the ComputeHypotenuse function and ONE return statement.
- Your program must prompt the user to enter in two double type values. The program will then use a function to
- compute the hypotenuse of a triangle C given the two values of sides A and B.
- The equation you will use is A^2 + B^2 = C^2
- Use the following function prototypes:
- void PrintInstructions(void);
- double GetSideA(void);
- double GetSideB(void);
- void ComputeHypotenuse(void);
- SAMPLE OUTPUT:
- Enter in a value for side A: 5
- Enter in a value for side B: 6
- C squared is equal to: 61.000000
- PROBLEM #4 Compute a Fixed Monthly Payment
- Compose a program to calculate a fixed monthly payment. Use JUST main and one function.
- THIS USES AMORTIZATION. If you google "amortization calculator", you should find the formula used.
- -Main will prompt the user for inputs for annual percentage rate, principle, period in years.
- -The function will take these values as arguments, compute the fixed monthly payment given these parameters and return the fixed
- monthly payment.
- Finally main should print this to screen using dollar signs and precision of 2 places to the right of the decimal.
- Given a loan of $100000, an APR of 5% and a term of 30 years, the fixed payment should be $536.82
- SAMPLE OUTPUT:
- Enter the APR: 5
- Enter the Principle: 100000
- Enter the Years: 30
- Your Fixed Monthly Payment is: $ 536.82
- Bonus Problem / Challenge
- The value of pi (π) can be calculated by the following formula:
- Θ = ✓(6 x (1/1^2) + (1/2^2) + (1/3^2) + (1/4^2) + ... + (1/limit^2) )
- When you get to this problem, ask Justin to write the formula on the board.
- Thats much easier to read than that mess, but pastebin can't embed images so its the best I could do.
- Write a program that uses this formula to calculate pi. Each operation in the
- parentheses will be repeated to the number of the limit (n). Note, the program will
- prompt the user for the “limit” value. Test your program once a limit of 5 terms, 10
- terms and a limit of 20 terms. Print the results of each and observe how this
- algorithm gets more accurate depending on how many terms you use.
- SAMPLE OUTPUT (Limit = 1):
- Enter the limit of terms to compute Pi.
- 1
- Pi = 2.449490
- SAMPLE OUTPUT (Limit = 10000):
- Enter the limit of terms to compute Pi.
- 10000
- Pi = 3.141497
Add Comment
Please, Sign In to add comment