fork download
  1. //Amari Mosley CSC5 Chapter 9, P. 538 #5
  2.  
  3. /********************************************************************
  4.  *
  5.  * REWRITE POINTER
  6.  * _________________________________________________________________
  7.  *
  8.  * This program rewrites a function that originally uses reference
  9.  * variables so that it uses pointers instead. The function swaps
  10.  * the values of two integers after multiplying each by 10 and
  11.  * returns the sum of the modified values.
  12.  *
  13.  * _________________________________________________________________
  14.  *
  15.  * INPUT
  16.  * x : First integer entered by the user
  17.  * y : Second integer entered by the user
  18.  * OUTPUT
  19.  * result : Sum of the modified integer values
  20.  *
  21.  ********************************************************************/
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. // Function Prototype
  26. int doSomething(int*, int*);
  27.  
  28. int main()
  29. {
  30. // INPUT
  31. int x;
  32. int y;
  33.  
  34. // Get values from the user
  35. cout << "Enter the first integer: ";
  36. cin >> x;
  37.  
  38. cout << "Enter the second integer: ";
  39. cin >> y;
  40.  
  41. // Call the function using pointers
  42. int result = doSomething(&x, &y);
  43.  
  44. // Output Results
  45. cout << endl << "Modified value of x: " << x << endl;
  46. cout << "Modified value of y: " << y << endl;
  47. cout << "Sum of modified values: " << result << endl;
  48.  
  49. return 0;
  50. }
  51.  
  52. // Function using pointers instead of reference variables
  53. int doSomething(int* x, int* y)
  54. {
  55. int temp = *x;
  56. *x = *y * 10;
  57. *y = temp * 10;
  58. return *x + *y;
  59. }
Success #stdin #stdout 0s 5332KB
stdin
5
8
stdout
Enter the first integer: Enter the second integer: 
Modified value of x: 80
Modified value of y: 50
Sum of modified values: 130