Advertisement
M4ritimeSeeker

PASS Week 11: Pointers

Nov 9th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. Example Code for Pointers:
  2. This code will swap the values of 'a' and 'b' using a function and pointers.
  3. USE THIS IF YOU NEED AN EXAMPLE FOR POINTER SYNTAX.
  4.  
  5. void exchange(int*, int*); //Function prototype
  6.  
  7. int main(void){
  8. int a = 5;
  9. int b = 10;
  10. exchange(&a,&b); //Function call
  11. return 0;
  12. }
  13.  
  14. /* Function definition */
  15. void exchange(int* ptrA, int* ptrB)
  16. {
  17. int c;
  18. c = *ptrA;
  19. *ptrA = *ptrB;
  20. *ptrB = c;
  21. return;
  22. }
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. PROBLEM #1: Using Pointers to Doubles
  37.  
  38. 1) Prompt the user to input two floating point (double) values and store them in two double variables.
  39. 2) Then you will declare two double POINTER variables that point to the variables declared
  40. in the previous step.
  41. 3) Create another double variable called "result".
  42. 4) Now add the two inputs together USING THE POINTER VARIABLES. Assign this value to the result variable,
  43. and print it to the screen. (The amount of decimal places or zeroes don't matter).
  44.  
  45. SAMPLE OUTPUT
  46.  
  47. Enter in two values:
  48. 44.5 23.45
  49. 44.50 and 23.45 = 67.95
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. PROBLEM #2: Pass By Reference
  62.  
  63. In this program you will modify the program from problem 1 by adding a function that will
  64. be given pointers as input. Passing a pointer to a function is known as "Pass By Reference".
  65. Passing a normal variable to a function (like you've been doing thus far) is known as "Pass By Value".
  66.  
  67. 1) Using the program from problem 1, create a function called “addNbrs( , , )” that takes
  68. three double pointers and returns nothing to the caller.
  69. 2) KEEP ALL OF the local declarations for the two input variables and the user prompts,
  70. and output in main. REMOVE THE TWO POINTER VARIABLES IN MAIN.
  71. 3) Call the addNbrs() function in main using the addresses of the two input variables and
  72. result variable.
  73. 4) Move the expression (the one that added the pointers) into the new function and assign
  74. the result using the pointers passed to the function from main.
  75.  
  76. SAMPLE OUTPUT
  77.  
  78. Enter in two values:
  79. 44.5 22.43
  80. 44.50 and 22.43 = 66.93
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. PROBLEM #3: Calculating the Area and Perimeter of a Square/Rectangle Using Pointers
  96.  
  97. 1) Create two double variables and prompt the user for input for them. These are
  98. the lengths of 2 adjacent sides of your 4 sided polygon.
  99. 2) Then create two double variables for the area and perimeter and set them to 0.
  100. 3) Create and call a SINGLE function that will compute the area AND perimeter of the polygon using pointers.
  101. 4) This function will take two double variables and two double variable addresses
  102. as input, and return nothing. (4 variables total as parameters).
  103. 5) Compute the area and perimeter in the function and store them in the two pointer values that
  104. were passed to the function
  105. 6) In main, print the area and perimeter.
  106.  
  107. SAMPLE OUTPUT
  108.  
  109. Enter in two values:
  110. 33.5 44.6
  111. Area = 1494.10 and the perimeter is: 156.20
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. PROBLEM #4: Calculating Hours and Minutes from Seconds (You guessed it, using pointers)
  136.  
  137. Honestly trying to explain this problem is hard, and I think seeing the sample output FIRST
  138. is better. Basically, the user enters in seconds, and you convert it into the following format.
  139. Have hours print no higher than 24, minutes no higher than 60, and seconds no higher than 60.
  140.  
  141. SAMPLE OUTPUT 1:
  142.  
  143. Enter in the number of seconds to convert:
  144. 12001
  145. 3 Hours, 20 Minutes, 1 Seconds
  146.  
  147.  
  148. SAMPLE OUTPUT 2:
  149.  
  150. Enter in the number of seconds to convert:
  151. 1234
  152. 0 Hours, 20 Minutes, 34 Seconds
  153.  
  154.  
  155. Here are the steps:
  156.  
  157. 1) Create a program that prompts the user to enter in a value for number of
  158. seconds using a long data type.
  159. 2) Create a function that will take a value representing seconds as input, and will return
  160. the number of hours, minutes and seconds contained in that value of seconds.
  161. 3) The three measures should be returned to the calling function by using pointers.
  162. 4) You may assume that the user will not enter more than 24 hours worth of seconds.
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. PROBLEM #5: Seperating Numbers to the Left and Right of a Decimal Place
  179.  
  180. Write a program that uses a function that receives a pointer to a floating-point
  181. number and two integer variables and sends back the integer and fraction parts as
  182. integers using pointers up to the hundredth place. In your test driver or main
  183. function prompt for the user to input a floating point number, then call the
  184. function and output the parts from main after the function returns them using
  185. pointers.
  186.  
  187. Hint use modulus and division.
  188. 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