fork download
  1. // Andres Guzman CSC5 Chapter 9, P. 538, #5
  2. /******************************************************************************
  3.  *
  4.  * REWRITE FUNCTION
  5.  * ____________________________________________________________________________
  6.  * Program will convert given function to be referenced using pointers
  7.  *
  8.  * Computation:
  9.  *
  10.  * Uses pointers to reference the variables, multiplies each referenced
  11.  * value by 10, and adds the modified values before returning the result.
  12.  * ____________________________________________________________________________
  13.  *
  14.  * INPUT
  15.  *
  16.  * input1 = Holds inputed value
  17.  * input2 = Holds inputed value
  18.  *
  19.  * OUTPUT
  20.  *
  21.  * answer = returned value of called function
  22.  *
  23.  ******************************************************************************/
  24. #include <iostream>
  25. using namespace std;
  26.  
  27. int doSomething(int *x, int *y);
  28.  
  29. int main()
  30. {
  31. int input1; //Inputed Value of user
  32. int input2; //Inputed Value of user
  33. cout << "Enter a Value into Input 1: ";
  34. cin >> input1;
  35. while(input1 < 0) //Input Validation
  36. {
  37. cout << "Please Enter Valid Value." << endl;
  38. cout << "Enter a Value into Input 1: ";
  39. cin >> input1;
  40. }
  41. cout << "\nEnter a Value into Input 2: ";
  42. cin >> input2;
  43. while(input2 < 0) //Input Validation
  44. {
  45. cout << "Please Enter Valid Value." << endl;
  46. cout << "Enter a Value into Input 2: ";
  47. cin >> input2;
  48. }
  49. //
  50. //Function Call and adds returned value to variable
  51. int answer = doSomething(&input1, &input2);
  52. //
  53. //Output results
  54. cout << "\nAnswer: " << answer;
  55. return 0;
  56. }
  57. /******************************************************************************
  58.  * Adding definition to function. Function will use pointers to reference
  59.  * variables and multiply given variables and return their total value.
  60.  ******************************************************************************/
  61. int doSomething(int *x, int *y)
  62. {
  63. int temp = *x; //Temporarily holds x value
  64. *x = *y * 10; //X value holds y value times 10
  65. *y = temp * 10; //Y value holds temporary value times 10
  66. return *x + *y; //Return value of x and y added
  67. }
  68.  
Success #stdin #stdout 0s 544KB
stdin
2 3
stdout
Enter a Value into Input 1: 
Enter a Value into Input 2: 
Answer: 50