Question
I have to (without changing any code within the main function): 1) Remove the prototype, header and body of the findMax() function containung only two formal parameters. 2) Include default values of -1 for three of the five parameters of the remaining findMax() 3) Which three parameters should receive default values? Why? 4) Should the default values be placed in the function prototype, in the function header or both? #include using namespace std; int findMax_1(int pos1, int pos2); int getPositive(); int findMax_2(int num1, int num2, int num3, int num4, int num5); int main() { int pos1, pos2, maxPos, num1, num2, num3, num4, num5, pos; pos1 = findMax_2(pos); pos2 = findMax_2(pos); maxPos = findMax_1(pos1, pos2); pos2 = findMax_2(pos); maxPos = findMax_1(pos1, pos2); pos2 = findMax_2(pos); maxPos = findMax_1(pos1, pos2); pos2 = findMax_2(pos); maxPos = findMax_1(pos1, pos2); cout << endl << "The largest of " << num1 << " and " << num2 << " and " << num3 << " and " << num4 << " and " << num5 << " is " << maxPos << endl; return 0; } int findMax_1(int pos1, int pos2){ int maxPos; if (pos1 > pos2) maxPos=pos1; else maxPos=pos2; return maxPos; } int findMax_2(int num1, int num2, int num3, int num4, int num5) { 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; }
Explanation / Answer
/* This is not possible with your current main because 1. you never input number 2. you are using findMax_1 in main 3. getPositve() is not defined 4. you would at least need to have default values for the last 4 parameters because you used 1 parameter to call it in main If we were to change main and rename findMax_1 and findMax_2 to findMax , define getPositive and use it.... */ #include using namespace std; int getPositive(); int findMax(int num1, int num2, int num3=-1, int num4=-1, int num5=-1); int main() { int num1, num2, num3, num4, num5; int pos2, pos3,pos4,pos5; num1=getPositive(); num2=getPositive(); num3=getPositive(); num4=getPositive(); num5=getPositive(); //call findMax with different parameters pos2 = findMax(num1,num2); pos3 = findMax(num1,num2,num3); pos4 = findMax(num1,num2,num3,num4); pos5 = findMax(num1,num2,num3,num4,num5); //display results cout