Advertisement
M4ritimeSeeker

PASS Week 12: Pointers

Apr 12th, 2022 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 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 temp;
  18. temp = *ptrA;
  19. *ptrA = *ptrB;
  20. *ptrB = temp;
  21. return;
  22. }
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. PROBLEM #1: Using Pointers for Double
  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 POINTERS and assign the value to the result variable,
  43. then print it to the screen.
  44.  
  45. SAMPLE OUTPUT
  46.  
  47. Enter in two values:
  48. 44.5
  49. 23.45
  50. 44.50 plus 23.45 = 67.95
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. PROBLEM #2: Pass By Reference
  63.  
  64. In this program you will be passing a pointer to a function, which 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. output in main. REMOVE THE TWO POINTER VARIABLES IN MAIN.
  71. 3) Call the addNbrs() function in main using the ADDRESSES (Remember the '&') of the two input variables and
  72. result variable.
  73. 4) Move the expression to add the values into the new function and assign the result using the
  74. pointers passed to the function from main.
  75.  
  76. SAMPLE OUTPUT
  77.  
  78. Enter in two values:
  79. 44.5
  80. 22.43
  81. 44.50 plus 22.43 = 66.93
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. PROBLEM #3: Calculating the Area and Perimeter of a Square/Rectangle Using Pointers
  97.  
  98. 1) Create two double variables and prompt the user for input for them. These are
  99. the lengths of 2 sides of a 4 sided polygon (rectangle or square).
  100. 2) Then create two double variables for the area and perimeter.
  101. 3) Create and call a SINGLE function that will compute the area AND perimeter of the polygon using pointers.
  102. 4) This function will take two double variables and two memory addresses
  103. as input, and return void. (4 variables total as parameters).
  104. 5) Compute the area and perimeter in the function and store them in the two pointer values that
  105. were passed to the function
  106. 6) In main, print the area and perimeter.
  107.  
  108. SAMPLE OUTPUT
  109.  
  110. Enter in two sides of a 4 sided polygon:
  111. 33.5
  112. 44.6
  113. Area = 1494.10 and the perimeter is: 156.20
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. PROBLEM #4: Calculating Hours and Minutes from Seconds (You guessed it, using pointers)
  138.  
  139. Honestly trying to explain this problem is hard, and I think seeing the sample output FIRST
  140. is better. Basically, the user enters in seconds, and you convert it into the following format.
  141.  
  142. SAMPLE OUTPUT 1:
  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. Enter in the number of seconds to convert:
  150. 1234
  151. 0 Hours, 20 Minutes, 34 Seconds
  152.  
  153.  
  154. Here are the steps:
  155.  
  156. 1) Create a program that prompts the user to enter in a value for number of
  157. seconds using a long data type.
  158. 2) Create a function that will take a value in seconds and three memory addresses
  159. (pointers to your hours, minutes, and remaining 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. Have hours print no higher than 24, minutes no higher than 60, and seconds no higher than 60.
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. PROBLEM #5: Seperating Numbers to the Left and Right of a Decimal Place
  180.  
  181. Write a program that uses a function that receives a pointer to a floating-point
  182. number and two integer variables and sends back the integer and fraction parts as
  183. integers using pointers up to the hundredth place. In your test driver or main
  184. function prompt for the user to input a floating point number, then call the
  185. function and output the parts from main after the function returns them using
  186. pointers.
  187.  
  188. Hint use modulus and division.
  189. 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