fork download
  1.  
  2. //Dione Torrevillas CS1A Chapter 9, P. 538, #5
  3. /*
  4. ********************************************************************
  5. POINTER REWRITE
  6. Description: Rewrite the textbook's doSomething function so that
  7.   it uses pointer parameters instead of references.
  8. INPUT: Two integer values.
  9. OUTPUT: The modified values and the sum returned by the function.
  10. ********************************************************************
  11. */
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. // Change two integers through pointers and return their sum.
  16. int doSomething(int *x, int *y)
  17. {
  18. int temp = *x;
  19. *x = *y * 10;
  20. *y = temp * 10;
  21. return *x + *y;
  22. }
  23.  
  24. int main()
  25. {
  26. int first, second;
  27. cout << "Enter the first integer: ";
  28. cin >> first;
  29. cout << "Enter the second integer: ";
  30. cin >> second;
  31.  
  32. // Pass the addresses of both integers to the function.
  33. int total = doSomething(&first, &second);
  34.  
  35. cout << "First value after the function: " << first << endl;
  36. cout << "Second value after the function: " << second << endl;
  37. cout << "Returned sum: " << total << endl;
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 544KB
stdin
2 3
stdout
Enter the first integer: Enter the second integer: First value after the function: 30
Second value after the function: 20
Returned sum: 50