fork download
  1. //Timothy Price CS1A Ch 9 Challenge 5
  2. /******************************************************************************
  3.  * Replace values
  4.  *
  5.  * This program receives two integers, swaps them, and multiplies them by a
  6.  * determined value.
  7.  *
  8.  * Inputs:
  9.  * two integers
  10.  *
  11.  * Output:
  12.  * two integers swapped and manipulated
  13.  *****************************************************************************/
  14.  
  15. #include <iostream>
  16.  
  17. using namespace std;
  18.  
  19. // Function prototype using pointers
  20. int doSomething(int* x, int* y);
  21.  
  22. int main() {
  23. int a;
  24. int b;
  25.  
  26. cout << "Enter two integers separated by the enter key.";
  27. cin >> a;
  28. cin >> b;
  29.  
  30. cout << "Before doSomething:" << endl;
  31. cout << "a = " << a << ", b = " << b << endl;
  32.  
  33. // Pass the addresses of a and b using the address-of operator (&)
  34. int result = doSomething(&a, &b);
  35.  
  36. cout << endl;
  37. cout << "After doSomething:" << endl;
  38. cout << "a = " << a << ", b = " << b << endl;
  39. cout << "Returned sum (x + y): " << result << endl;
  40.  
  41. return 0;
  42. }
  43.  
  44. // Function definition using pointers
  45. int doSomething(int* x, int* y) {
  46. if (x == nullptr || y == nullptr) {
  47. return 0;
  48. }
  49.  
  50. int temp = *x;
  51. *x = (*y) * 10;
  52. *y = temp * 10;
  53. return (*x) + (*y);
  54. }
Success #stdin #stdout 0s 544KB
stdin
3
5
stdout
Enter two integers separated by the enter key.Before doSomething:
a = 3, b = 5

After doSomething:
a = 50, b = 30
Returned sum (x + y): 80