Consider the definition of the following function template: (8, 9) template <cla
ID: 3822184 • Letter: C
Question
Consider the definition of the following function template: (8, 9)
template <class type>
type funcExp(type list[], int size)
{
type x = list[0];
type y = list[size - 1];
for (int j = 1; j < size - 1; j++)
{
if (x < list[j])
x = list[j];
if (y > list[size - 1 - j])
y = list[size - 1 - j];
}
return y + x;
}
Further suppose that you have the following declarations:
double sales[7] = {280.50, 320.00, 56.00, 78.90, 300.00,
100.00, 250.00};
string names[] = {"Mike", "Lisa", "Nancy", "Robinson",
"Miller", "Sam"};
What is the output of the following statements?
a. cout << funcExp(sales, 7) << endl;
b. cout << funcExp(names, 6) << endl;
Explanation / Answer
template <class type>
type funcExp(type list[], int size)
{
type x = list[0]; // first element of list
type y = list[size - 1]; // last element of list
for (int j = 1; j < size - 1; j++)
{
if (x < list[j])
x = list[j];
if (y > list[size - 1 - j])
y = list[size - 1 - j];
}
return y + x;
}
The above function calculating the sum of min and maximum value in list. x stores the minimum and y stores the maximum
Further suppose that you have the following declarations:
double sales[7] = {280.50, 320.00, 56.00, 78.90, 300.00,100.00, 250.00};
string names[] = {"Mike", "Lisa", "Nancy", "Robinson","Miller", "Sam"};
What is the output of the following statements?
a. cout << funcExp(sales, 7) << endl;
Ans: 56.00 + 320.00 = 376.00
b. cout << funcExp(names, 6) << endl;
Ans: Lisa + Sam = lisaSam