# Quiz 9: Recursion and Algorithm Analysis

Template program to find the average of two numbers which is declared as integer data type: / / Required header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find the average of two numbers ValueType average(ValueType a, ValueType b) { / / return the average return (a+b) / 2; } / / main function int main() { / / declare the two numbers a and b as integer data type int a, b; / / display the two numbers cout "Enter the two numbers:\n"; / / get the two numbers cin a b; / / display the average of two numbers cout "The average of two numbers " a " and " b " is: "; / / call the function cout average(a, b) endl; } Sample Output: Enter the two numbers: 4 2 The average of two numbers 4 and 2: 3 Explanation: • From the given code, "template " means function template with type parameter. In this template, ValueType is the identifier. • In the "main()" function, o Declare the two numbers as "int" data type. o Get the two numbers. o Call the "average (a, b)" function to find the average of two numbers. • The function "average(ValueType a, ValueType b)" is used to return the average of two numbers. Template program for find the average of two numbers which is declared as float data type: / / Required header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find the average of two numbers ValueType average(ValueType a, ValueType b) { / / return the average return (a+b) / 2; } / / main function int main() { / / declare the two numbers a and b as float data type float a, b; / / display the two numbers cout "Enter the two numbers:\n"; / / get the two numbers cin a b; / / display the average of two numbers cout "The average of two numbers " a " and " b " is: "; / / call the function cout average(a, b) endl; } Sample Output: Enter the two numbers: 4 5 The average of two numbers 4 and 5: 4.5 Explanation: • From the given code, "template " means function template with type parameter. In this template, ValueType is the identifier. • In "main()" function, o Declare the two numbers as "float" data type. o Get the two numbers. o Call the "average(a, b)" function to find the average of two numbers. • The function "average(ValueType a, ValueType b)" is used to return the average of two numbers. Template program for find the average of two numbers which is declared as double data type: / / Required header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find the average of two numbers ValueType average(ValueType a, ValueType b) { / / return the average return (a+b) / 2; } / / main function int main() { / / declare the two numbers a and b as double data type double a, b; / / display the two numbers cout "Enter the 2 numbers:\n"; / / get the two numbers cin a b; / / display the average of two numbers cout "The average of two numbers " a " and " b " is: "; / / call the function cout average(a, b) endl; } Explanation: • From the given code, "template " means function template with type parameter. In this template, ValueType is the identifier. • In "main()" function, o Declare the two numbers as "double" data type. o Get the two numbers. o Call the "average (a, b)" function to find the average of two numbers. • The function "average(ValueType a, ValueType b)" is used to return the average of two numbers.

Template program for find the maximum of two numbers which is declared as integer data type: / / Required header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find the maximum of two numbers ValueType maximum(ValueType p, ValueType q) { / / return the maximum return p q ? p : q; } / / main function int main() { / / declare the two numbers p and q as integer data type int p, q; / / display the two numbers cout "Enter the two numbers:\n"; / / get the two numbers cin p q; / / display the maximum of two numbers cout "The maximum of two numbers " p " and " q " is: "; / / call the function cout maximum(p, q) endl; } Sample Output: Enter the two numbers: 4 2 The maximum of two numbers 4 and 2: 4 Explanation: • From the given code, "template " means function template with type parameter. In this template, ValueType is the identifier. • In the "main()" function, o Declare the two numbers as "int" data type. o Get the two numbers. o Call the "max (p, q)" function to find the maximum of two numbers. • The function "max(ValueType p, ValueType q)" is used to return the maximum of two numbers. Template program for find the maximum of two numbers which is declared as float data type: / / Required header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find the maximum of two numbers ValueType maximum(ValueType p, ValueType q) { / / return the maximum return p q ? p : q; } / / main function int main() { / / declare the two numbers p and q as float data type float p, q; / / display the two numbers cout "Enter the two numbers:\n"; / / get the two numbers cin p q; / / display the maximum of two numbers cout "The maximum of two numbers " p " and " q " is: "; / / call the function cout maximum(p, q) endl; } Sample Output: Enter the two numbers: 4.6 5.8 The maximum of two numbers 4.6 and 5.8: 5.8 Explanation: • From the given code, template means function template with type parameter. In this template, ValueType is the identifier. • In main () function, o Declare the two numbers as float data type o Get the two numbers o Call the maximum (p, q) function to find the maximum of two numbers. • The function maximum(ValueType p, ValueType q) is used to return the maximum of two numbers. Template program for find the maximum of two numbers which is declared as double data type: / / Required header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find the maximum of two numbers ValueType maximum(ValueType p, ValueType q) { / / return the maximum return p q ? p : q; } / / main function int main() { / / declare the two numbers p and q as integer data type double p, q; / / display the two numbers cout "Enter the two numbers:\n"; / / get the two numbers cin p q; / / display the maximum of two numbers cout "The maximum of two numbers " p " and " q " is: "; / / call the function cout maximum(p, q) endl; } Explanation: • From the given code, template means function template with type parameter. In this template, ValueType is the identifier. • In main () function o Declare the two numbers as double data type o Get the two numbers o Call the maximum (p, q) function to find the maximum of two numbers. • The function maximum(ValueType p, ValueType q) is used to return the maximum of two numbers. Template function for complex values: / / Required header files #include / / header files for complex #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find the maximum of two numbers ValueType maximum(ValueType p, ValueType q) { / / return the maximum return p q ? p : q; } / / main function int main() { / / declare the two numbers p and q as integer data type complex p, q; / / display the two numbers cout "Enter the two numbers:\n"; / / get the two numbers cin p q; / / display the maximum of two numbers cout "The maximum of two numbers " p " and " q " is: "; / / call the function cout maximum(p, q) endl; } Explanation: From the above program, the complier cannot produce a "complex " in the "maximum" function. So the error occurs in the given program; because for complex values, the symbol " " is not defined in the program.

Template program for find the median of three numbers which is declared as integer data type: / / include required header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find median of three values ValueType median(ValueType p, ValueType q, ValueType r) { / / assign the maximum as p (first number) and minimum as q(second number) using template identifier ValueType maximum= p, minimum = q; / / check if p is less than q if (p q) { / / assign the maximum as q maximum = q; / / assign the minimum as p minimum = p; } / / check the r (third number) greater than maximum, then returns maximum is median if (r maximum) return maximum; / / if r is less than minimum then return the minimum is median else if (r minimum) return minimum; / / otherwise returns the r is median else return r; } / / main function int main() { / / declare the three numbers p, q, and r as integer data type int p,q,r; / / display the three numbers cout "Enter the three numbers:\n"; / / get the three numbers cin p q r; / / display the median of three numbers cout "The median of three numbers " p "," q "and " r " is: "; / / call the function cout median(p, q, r) endl; } Sample Output: Enter the three numbers: 6 8 5 The median of three numbers 6, 8, and 5 is: 6 Explanation: • From the given code, template means function template with type parameter. In this template, ValueType is the identifier. • In main () function o Declare the three numbers as integer data type o Get the three numbers o Call the median (p, q, r) function to find the median of three numbers. • The function median(ValueType p, ValueType q, ValueType r) is used to return the median of three numbers. o In this function, using template identifier we can set maximum as p and minimum as q. Here we can consider the first number as p and second number as q. o Then check whether the p is less than q. if the condition is satisfied, then set the maximum as q and minimum as p. o After that check whether r (consider r is the third number) is greater than maximum, then returns the maximum is the median or r is less than minimum, then returns the minimum is median. o Otherwise return the r is median. Template program for find the median of three numbers which is declared as float data type: / / include required header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find median of three values ValueType median(ValueType p, ValueType q, ValueType r) { / / assign the maximum as p (first number) and minimum as q(second number) using template identifier ValueType maximum= p, minimum = q; / / check if p is less than q if (p q) { / / assign the maximum as q maximum = q; / / assign the minimum as p minimum = p; } / / check the r (third number) greater than maximum, then returns maximum is median if (r maximum) return maximum; / / if r is less than minimum then return the minimum is median else if (r minimum) return minimum; / / otherwise returns the r is median else return r; } / / main function int main() { / * declare the three numbers p, q, and r as float data type * / float p,q,r; / / display the three numbers cout "Enter the three numbers:\n"; / / get the three numbers cin p q r; / / display the median of three numbers cout "The median of three numbers " p "," q "and " r " is: "; / / call the function cout median(p, q, r) endl; } Sample Output: Enter the three numbers: 6.5 8.9 5.6 The median of three numbers 6.5, 8.9, and 5.6 is: 6.5 Explanation: • From the given code, template means function template with type parameter. In this template, ValueType is the identifier. • In main () function o Declare the three numbers as float data type o Get the three numbers o Call the median (p, q, r) function to find the median of three numbers. • The function median(ValueType p, ValueType q, ValueType r) is used to return the median of three numbers. o In this function, using template identifier we can set maximum as p and minimum as q. Here we can consider the first number as p and second number as q. o Then check whether the p is less than q. if the condition is satisfied, then set the maximum as q and minimum as p. o After that check whether r (consider r is the third number) is greater than maximum, then returns the maximum is the median or r is less than minimum, then returns the minimum is median. o Otherwise return the r is median. Template program for find the median of three numbers which is declared as double data type: / / include required header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find median of three values ValueType median(ValueType p, ValueType q, ValueType r) { / / assign the maximum as p (first number) and minimum as q(second number) using template identifier ValueType maximum= p, minimum = q; / / check if p is less than q if (p q) { / / assign the maximum as q maximum = q; / / assign the minimum as p minimum = p; } / / check the r (third number) greater than maximum, then returns maximum is median if (r maximum) return maximum; / / if r is less than minimum then return the minimum is median else if (r minimum) return minimum; / / otherwise returns the r is median else return r; } / / main function int main() { / * declare the three numbers p, q, and r as double data type * / double p,q,r; / / display the three numbers cout "Enter the three numbers:\n"; / / get the three numbers cin p q r; / / display the median of three numbers cout "The median of three numbers " p "," q "and " r " is: "; / / call the function cout median(p, q, r) endl; } Sample Output: Enter the three numbers: 6.1234 8.4556 2.35678 The median of three numbers 6.1234, 8.4556, and 2.35678 is: 6.1234 Explanation: • From the given code, "template " means function template with type parameter. In this template, "ValueType" is the identifier. • In "main()" function o Declare the three numbers as double data type o Get the three numbers o Call the "median (p, q, r)" function to find the median of three numbers. • The function "median(ValueType p, ValueType q, ValueType r)" is used to return the median of three numbers. o In this function, using template identifier we can set maximum as p and minimum as q. Here we can consider the first number as p and second number as q. o Then check whether the p is less than q. if the condition is satisfied, then set the maximum as q and minimum as p. o After that check whether r (consider r is the third number) is greater than maximum, then returns the maximum is the median or r is less than minimum, then returns the minimum is median. o Otherwise return the r is median. Template program for find the median of three numbers which is declared as complex: / / include required header files #include / / include complex header files #include / / for cin and cout statements using namespace std; / / type parameter for template template / / function definition for find median of three values ValueType median(ValueType p, ValueType q, ValueType r) { / / assign the maximum as p (first number) and minimum as q(second number) using template identifier ValueType maximum= p, minimum = q; / / check if p is less than q if (p q) { / / assign the maximum as q maximum = q; / / assign the minimum as p minimum = p; } / / check the r (third number) greater than maximum, then returns maximum is median if (r maximum) return maximum; / / if r is less than minimum then return the minimum is median else if (r minimum) return minimum; / / otherwise returns the r is median else return r; } / / main function int main() { / * declare the three numbers p, q, and r as float data type * / complex p,q,r; / / display the three numbers cout "Enter the three numbers:\n"; / / get the three numbers cin p q r; / / display the median of three numbers cout "The median of three numbers " p "," q "and " r " is: "; / / call the function cout median(p, q, r) endl; } Explanation: From the above program, the complier cannot produce a "complex " in the "median ()" function. So the error occurs in the given program; because for complex values, the symbol " " is not defined in the program.