Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Example Code for Pointers:
- This code will swap the values of 'a' and 'b' using a function and pointers.
- USE THIS IF YOU NEED AN EXAMPLE FOR POINTER SYNTAX.
- void exchange(int*, int*); //Function prototype
- int main(void){
- int a = 5;
- int b = 10;
- exchange(&a,&b); //Function call
- return 0;
- }
- /* Function definition */
- void exchange(int* ptrA, int* ptrB)
- {
- int c;
- c = *ptrA;
- *ptrA = *ptrB;
- *ptrB = c;
- return;
- }
- PROBLEM #1: Using Pointers to Doubles
- 1) Prompt the user to input two floating point (double) values and store them in two double variables.
- 2) Then you will declare two double POINTER variables that point to the variables declared
- in the previous step.
- 3) Create another double variable called "result".
- 4) Now add the two inputs together USING THE POINTER VARIABLES. Assign this value to the result variable,
- and print it to the screen. (The amount of decimal places or zeroes don't matter).
- SAMPLE OUTPUT
- Enter in two values:
- 44.5 23.45
- 44.50 and 23.45 = 67.95
- PROBLEM #2: Pass By Reference
- In this program you will modify the program from problem 1 by adding a function that will
- be given pointers as input. Passing a pointer to a function is known as "Pass By Reference".
- Passing a normal variable to a function (like you've been doing thus far) is known as "Pass By Value".
- 1) Using the program from problem 1, create a function called “addNbrs( , , )” that takes
- three double pointers and returns nothing to the caller.
- 2) KEEP ALL OF the local declarations for the two input variables and the user prompts,
- and output in main. REMOVE THE TWO POINTER VARIABLES IN MAIN.
- 3) Call the addNbrs() function in main using the addresses of the two input variables and
- result variable.
- 4) Move the expression (the one that added the pointers) into the new function and assign
- the result using the pointers passed to the function from main.
- SAMPLE OUTPUT
- Enter in two values:
- 44.5 22.43
- 44.50 and 22.43 = 66.93
- PROBLEM #3: Calculating the Area and Perimeter of a Square/Rectangle Using Pointers
- 1) Create two double variables and prompt the user for input for them. These are
- the lengths of 2 adjacent sides of your 4 sided polygon.
- 2) Then create two double variables for the area and perimeter and set them to 0.
- 3) Create and call a SINGLE function that will compute the area AND perimeter of the polygon using pointers.
- 4) This function will take two double variables and two double variable addresses
- as input, and return nothing. (4 variables total as parameters).
- 5) Compute the area and perimeter in the function and store them in the two pointer values that
- were passed to the function
- 6) In main, print the area and perimeter.
- SAMPLE OUTPUT
- Enter in two values:
- 33.5 44.6
- Area = 1494.10 and the perimeter is: 156.20
- PROBLEM #4: Calculating Hours and Minutes from Seconds (You guessed it, using pointers)
- Honestly trying to explain this problem is hard, and I think seeing the sample output FIRST
- is better. Basically, the user enters in seconds, and you convert it into the following format.
- Have hours print no higher than 24, minutes no higher than 60, and seconds no higher than 60.
- SAMPLE OUTPUT 1:
- Enter in the number of seconds to convert:
- 12001
- 3 Hours, 20 Minutes, 1 Seconds
- SAMPLE OUTPUT 2:
- Enter in the number of seconds to convert:
- 1234
- 0 Hours, 20 Minutes, 34 Seconds
- Here are the steps:
- 1) Create a program that prompts the user to enter in a value for number of
- seconds using a long data type.
- 2) Create a function that will take a value representing seconds as input, and will return
- the number of hours, minutes and seconds contained in that value of seconds.
- 3) The three measures should be returned to the calling function by using pointers.
- 4) You may assume that the user will not enter more than 24 hours worth of seconds.
- PROBLEM #5: Seperating Numbers to the Left and Right of a Decimal Place
- Write a program that uses a function that receives a pointer to a floating-point
- number and two integer variables and sends back the integer and fraction parts as
- integers using pointers up to the hundredth place. In your test driver or main
- function prompt for the user to input a floating point number, then call the
- function and output the parts from main after the function returns them using
- pointers.
- Hint use modulus and division.
- So if a user enters in 98.65 , the function should return 98 and 65
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement