Answer to Question #40125 in C++ for Mary

Question #40125
Write, compile, execute, and test a program that uses the function templates
Square(),Power(),and Find() of Exercises 1,2,and 3.
1. Write a function template Square()that returns the square of its only argument.
2. Write a function template Power() that takes two arguments.The first argument
should be of arbitrary type. The second argument is an integer. The function
should return the first argument raised to the power indicated by its second argu-
ment.The return type should be that of its first argument.
3. Write a function template Find() that takes two arguments and returns an inte-
ger.The first argument is an array of arbitrary type.The second argument is an
ordinary variable of the same type as the first argument.The function should re-
turn the array subscript of the first array element that exceeds the second argu-
ment.If no such element is found,the function should return –1.
Expert's answer
#include <cstdlib> // system

template <typename T>
T Square(T value)
return value*value;

template <typename T>
T Power(T value, int i)
int const_value = value;
for (int j = 0; j < i - 1; j++)
value *= const_value;

return value;

template <typename T>
int Find(T array[], T element, int size_of_array)
bool exceeds_or_no = false;
int i = 0;
for (i = 0; i < size_of_array; i++)
if (array[i] > element) { exceeds_or_no = true; break; }

if (exceeds_or_no) return i;
return -1;


using namespace std;

int main()
int size_of_array = 0;
int value; int power;

cout<<"Enter value"<<endl;
cout<<"value ^2 = "<<Square(value)<<endl;
cout<<"Enter power"<<endl;
cout<<"value ^3 = "<<Power(value, power)<<endl;
cout<<"Enter size of array : "<<endl;
int *array = new int[size_of_array];
cout<<"Enter elements of arrays : "<<endl;
for (int i = 0; i < size_of_array; i++)
cout<<"Enter element"<<endl;
cout<<"Array subscript : "<<Find(array, value , size_of_array)<<endl;
return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


Assignment Expert
18.03.14, 16:31

Questions in this section are answered for free. We work on all questions and publish answers after verification. There is no guarantee of answering certain question but we are doing our best. Although if you have serious assignment that requires large amount of work and hence cannot be done for free you can submit it as assignment and our experts will surely assist you.

18.03.14, 12:57

Why not??

Leave a comment

Ask Your question

New on Blog