Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Examine and discuss the following program (Try running it multiple times and change the value of variable "i"):
- #include <stdio.h>
- int main(void)
- {
- int i = 99;
- printf("The address of i is %p.\n", &i);
- return 0;
- }
- Structure of a Function in C:
- The portion of C code that contains the program of a function is the function definition.
- The following illustrates it’s form.
- /* You would put your block comment here describing what the function does */
- type function_name (parameters with types){
- Local declarations
- Local statements including the return statement.
- }
- PROBLEM #1 Basic Function
- Compose a program that will print the message "A message for you: Have a nice day!"
- to the user and then exit returning a 0 to the system on completion. Here is the catch, your
- printf() statement DOES NOT go in your main function. Your main function should call
- the user-defined function titled "PrintMessage()" with no parameters and
- with no return values being used in main. Your user defined function will take
- no arguments and return void.
- Inside your user function you will use the printf() function to print the following:
- "A message for you: Have a nice day!"
- /*** SKELETON CODE FOR PROBLEM 1 ***/
- #include <stdio.h>
- //FUNCTION PROTOTYPE (Declaration) GOES HERE
- int main(void){
- return 0;
- }
- //START USER FUNCTION HERE
- PROBLEM #2 Multi‐part program: Pass By Value
- A. Write a program that prompts the user to enter in values for the base and
- side of a square, calculates the area of the square, then prints the result to the user.
- Create floating point variables to hold the base, side, and area of the square.
- Then compute the area.
- Print out the area of the square with a minimum width of 8 and
- precision of 2 decimal places to the right.
- (NO USER FUNCTIONS ARE NEEDED FOR THIS PART. JUST PROGRAM IN MAIN)
- B. Now modify the program you just made in Part A.
- Compose a function to perform the calculation of the area of a square.
- Use the code from Part A to perform the calculations, and move it inside
- your new function.
- HOW TO STRUCTURE THE FUNCTION:
- First identify the data the function will need to take
- as parameters, and then identify the value if any that would be returned
- from the function. Now create the function prototype (declaration) in the global area of
- the source code. Copy and paste your function prototype under the main function,
- and begin writing the function definition (Refer to Problem 1's structure).
- Test your function by running the program. Does it work with floating point values?
- What changes if any do you need to make to take floating point parameters
- and return a floating point value?
- C. Modify the program in problem #1 by adding separate functions for prompting the user,
- getting the user input, and providing output to the user by taking
- arguments. Your main function should only contain functions calls and a
- return statement to return the values to the calling function.
- Problem #3
- Compose a program that will compute the hypotenuse of a triangle.
- This program uses multiple user-defined functions. Your program should have a
- main function, a print instructions function, two get input functions, and a compute hypotenuse function.
- The main function should ONLY BE COMPRISED OF FOUR FUNCTION CALL to the functions listed below and
- one return statement. Your program must prompt the user to enter in two double type values for the triangle sides.
- One function for getting side A and one for getting side B. The program will then use a function to
- compute the hypotenuse of a triangle given the two values of sides A and B.
- For those who do not recall the equation is the Pythagorean Theorem A^2 + B^2 = C^2.
- Create and use the following functions:
- void PrintInstructions( );
- double GetSideA( );
- double GetSideB( );
- void ComputeHypotenuse( );
- The challenge here is to know when to call what function and what to do with its return
- values. The main function will only contain 2 local statements: a function call (ComputeHypotenuse)
- and a return statement (i.e. return 0).
- /** SKELETON CODE FOR PROBLEM 3 **/
- #include <stdio.h>
- //FUNCTION PROTOTYPES GO HERE
- int main(void){
- void PrintInstructions( );
- double GetSideA( );
- double GetSideB( );
- ComputeHypotenuse();
- return 0;
- }
- //PRINT INSTRUCTIONS FUNCTION GOES HERE
- //GET SIDE A FUNCTION GOES HERE
- //GET SIDE B FUNCTION GOES HERE
- //COMPUTER HYPOTENUSE FUNCTION GOES HERE
Add Comment
Please, Sign In to add comment