Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include using namespace std; int getPositive(); int main() { int pos1, pos2, ma

ID: 3584518 • Letter: #

Question

#include using namespace std; int getPositive(); int main() { int pos1, pos2, maxPos, pos; pos1 = getPositive(); pos2 = getPositive(); // call function to find max of pos1 and pos2 cout << endl << "The larger of " << pos1 << " and " << pos2 << " is " << maxPos << endl; return 0; } //------------------------------------------ // findMax() // // Pre-Condition: both parameters are positive integers // Post-Condition: the larger of the two parameters is returned to the calling // function //------------------------------------------ // fill in function header for findMaxPositive() to agree with prototype // ... need to choose names for formal parameters int getPositive(){ int pos, maxPos; cout << "Enter a positive integer: "; cin >> pos; while (pos <= 0) { cout << "Integer must be positive (> 0) --- Please re-enter: "; cin >> pos; } return pos; } // fill in code to determine the larger of the two formal parameters and // cause maxPos to contain the larger of the two formal parameters // fill in the code to return maxPos to the calling function

Explanation / Answer

#include<iostream>

using namespace std;

int getPositive();
int findMax(int a, int b);
int findMaxPositive(int a, int b);

int main()
{
    int pos1, pos2, maxPos;
    pos1 = getPositive();
    pos2 = getPositive(); // call function to find max of pos1 and
                          // pos2
    maxPos = findMaxPositive(pos1, pos2);
    cout << endl << "The larger of " << pos1 << " and " << pos2
         << " is " << maxPos << "." << endl;
    return 0;
}

int getPositive()
{
    int pos;
    cout << "Enter a positive integer: ";
    cin >> pos;
    while (pos <= 0)
    {
        cout << "Integer must be positive (> 0) --- Please re-enter: ";
        cin >> pos;
    }
    return pos;
}
// fill in code to determine the larger of the two formal parameters
// and
// cause maxPos to contain the larger of the two formal parameters
// fill in the code to return maxPos to the calling function

//------------------------------------------
// findMax()
//
// Pre-Condition: both parameters are positive integers
// Post-Condition: the larger of the two parameters is returned to the
// calling function
//------------------------------------------
// fill in function header for findMaxPositive() to agree with
// prototype ... need to choose names for formal parameters
int findMax(int a, int b)
{
    if (a < b)
    {
        return b;
    }
    else
    {
        return a;
    }
}

int findMaxPositive(int a, int b)
{
    return findMax(a,b);
}