fork download
  1. //Ryan Shahriyarpour CS1A Chapter 9 Question 5
  2. /*****************************************************************************
  3.  *
  4.  * Exchange and Scale Two Values
  5.  *
  6.  *********************
  7.  *
  8.  * This program takes two values, multiplies each one by 10, and swaps their
  9.  * positions. It displays the two values before and after, and also displays
  10.  * the sum of the two new values.
  11.  *
  12.  * INPUT
  13.  * None (a and b are set in the program)
  14.  *
  15.  * OUTPUT
  16.  * a and b before the call, a and b after the call, and the returned value
  17.  *
  18.  ****************************************************************************/
  19.  
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. // Function prototype
  24. int doSomething(int *x, int *y);
  25.  
  26. int main()
  27. {
  28. // DATA DICTIONARY
  29. // a - first test value
  30. // b - second test value
  31. // result - value returned by doSomething
  32. // temp - holds the original value of x inside doSomething
  33. int a = 3;
  34. int b = 7;
  35. int result;
  36.  
  37. cout << "Before the call: a = " << a << ", b = " << b << endl;
  38.  
  39. result = doSomething(&a, &b);
  40.  
  41. cout << "After the call: a = " << a << ", b = " << b << endl;
  42. cout << "Value returned: " << result << endl;
  43.  
  44. return 0;
  45. }
  46.  
  47. // Pointer version of the original reference-variable function
  48. int doSomething(int *x, int *y)
  49. {
  50. int temp = *x;
  51.  
  52. *x = *y * 10;
  53. *y = temp * 10;
  54.  
  55. return *x + *y;
  56. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
Before the call: a = 3, b = 7
After the call:  a = 70, b = 30
Value returned:  100