1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
/* Write a C++ function with inputs equal to four integers and outputs equal to
the max two of the four integers. Also write a main program to prompt
the user for four integer inputs, call the function, and display the
outputs. Test the program with the following 4 integers: -4, 32, 1, 8. */
#include <iostream> // contains cout/cin functions
using namespace std;
//______________________________________________________________________________
// Prototypes:
void calc_maximum(int, int, int, int, int&, int&);
//______________________________________________________________________________
// Main function:
int main() {
int val1, val2, val3, val4, max1, max2; // values to assess
cout << "Please enter 4 integers:";
cin >> val1 >> val2 >> val3 >> val4;
calc_maximum(val1, val2, val3, val4, max1, max2); // function to calculate highest values
cout << "The maximum integers are " << max1 << " and " << max2 << endl;
system("PAUSE");
return 0;
}
//______________________________________________________________________________
// Function definitions:
void calc_maximum(int Val1, int Val2, int Val3, int Val4, int& Max1, int& Max2) {
Max1 = Val1; // initially set Max1 for comparison throughout the function
if (Val2 > Max1) { // if Val2 is greater than Max1, move the value of Max1
Max1 = Val2; // down to Max2, and replace with the value of Val2
Max2 = Val1;
} else {
Max2 = Val2;
}
if (Val3 > Max1) {
Max2 = Max1;
Max1 = Val3;
} else if (Val3 > Max2) {
Max2 = Val3;
}
if (Val4 > Max1) {
Max2 = Max1;
Max1 = Val4;
} else if (Val4 > Max2) {
Max2 = Val4;
}
}
//______________________________________________________________________________
|