fork download
  1. /*******************************************************************************
  2.  * Aiden Stannard // CS1A // Chapter 9 Homework: Challange 5# // 11/21/2026
  3.  * _____________________________________________________________________________
  4.  *
  5.  * SWAP FUNCTION
  6.  * _____________________________________________________________________________
  7.  *
  8.  * This program accepts two different values for x and y, swaps the values of x
  9.  * and y, and than outputs the new swapped values of x and y, aswell the the sum
  10.  * x and y.
  11.  *
  12.  * Inputs: Enter a value for x:__
  13.  * Enter a value for y:__
  14.  *
  15.  * Output: The value of x is now__
  16.  * The value of y is now__
  17.  * The sum of x and y is__
  18.  *
  19.  ******************************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. int doSomething(int*, int*); // swap function prototype
  24.  
  25. int main() {
  26. int x;
  27. int y;
  28. int z; // Stores value returned by the function
  29.  
  30. cout << "Enter a value for x: " << endl;
  31. cin >> x;
  32. cout << "Enter a value for y: " << endl;
  33. cin >> y; // assigned console input into x and y
  34.  
  35. z = doSomething(&x,&y); // passes the value of x and y into a swap function
  36.  
  37. cout << "The value of x is now " << x << "\n The value of y is now " << y
  38. << endl; // Displays the swapped values of x and y
  39. cout << "The sum of x and y is " << z << endl;
  40. return 0;
  41. }
  42.  
  43. int doSomething(int *x, int *y){
  44. int temp;
  45. temp = *y;
  46. *y = *x;
  47. *x = temp;
  48. return *x + *y;
  49. }
Success #stdin #stdout 0s 544KB
stdin
5
12
stdout
Enter a value for x: 
Enter a value for y: 
The value of x is now 12
 The value of y is now 5
The sum of x and y is 17