Your question, your audience. Choose who sees your identity—and your question—with question security.

Hey all! Here is a problem that my teacher proposed in class. I need to write a template function that takes two arguments: an integer n and a variable v of a parameterized type. The function computes and returns the nth power of v. Also write a driver function (i.e. the main function) to ask the user to enter a positive integer (n). Now use the template function to compute the nth power of 8.5 and displayed the result, then use the template function to compute the nth power of 8 and displayed the result. Continue to ask the user for another integer and repeat the computation until the user gives a negative integer.

To be honest, I have no idea where to start. I know that I have to use an array (because that is the chapter we are in), but other than that I am lost. Please help. Thank you.

To be honest, I have no idea where to start. I know that I have to use an array (because that is the chapter we are in), but other than that I am lost. Please help. Thank you.

No, this does not require arrays at all. To give you a start, here's a really short template class solution (not a template function - this is left as an excercise to the reader *g*)

template<int N> struct NthPower {

double ReturnIt ( double d) { return pow ( d, N);}

};

As we are not supposed to do your homework, you are to proceed from here and get back with some results on your own :o)

JKR -- I am not understanding the notation you are using. I am using Visual C++ 6.0. So, is the notation different?

Well, the more you should be interested in doing most of by yourself :o)

>>JKR -- I am not understanding the notation you are using. I am using Visual C++ 6.0

Me, too - what is so strange about it?

template<int N> struct NthPower {

double ReturnIt ( double d) { return pow ( d, N);}

};

is this just an example of what it should look like?

template<typename TYPE>

TYPE ComputePower( int nPower, TYPE nValue )

{

if ( nPower > 0 )

{

return nValue * ComputePower(nPower-1, nValue);

}

else

{

return 1;

}

}

You can write your own driver function, but it will have the basic form of:

int nUserInput;

int nDisplayValue1;

float nDisplayValue2;

// Get the User Input

// Exit if it is is less than 0

// Compute the values

nDisplayValue1 = ComputePower(nUserInput, 8);

nDisplayValue2 = ComputePower(nUserInput, 8.5f);

// Display the values

// Get the next user input

Hope this helps,

Dex*

#include <iostream.h>

int ComputePower(int nPower, float nValue);

int main ()

{

cout << "Please enter a postive integer or enter a negative integer to end:" << endl;

cin >> nPower;

if (nPower > 0)

{

return nValue * ComputePower(nPower-1, nValue);

cout << "The " << nPower << "th power of 8.5 is " << nDisplayValue1;

cout << "The " << nPower << "th power of 8 is " << nDisplayValue 2;

}

else

{

return 1;

}

}

int ComputePower(int nPower, TYPE nValue )

{

int nUserInput;

int nDisplayValue1;

float nDisplayValue2;

nDisplayValue1 == ComputePower(nUserInput, 8);

nDisplayValue2 == ComputePower(nUserInput, 8.5f);

}

#include <iostream>

using namespace std;

instead of

#include <iostream.h>

The 1st one is the standard. Then, declare the integer variables you are using in your "main()" function (nDisplayValueX, nPower etc :o)

#include <iostream>

using namespace std;

int ComputePower(int nPower, float nValue);

int main ()

{

int nDisplayValue1;

float nDisplayValue2;

int nPower;

int nValue=0;

cout << "Please enter a postive integer or enter a negative integer to end:" << endl;

cin >> nPower;

if (nPower > 0)

{

return nValue * ComputePower(nPower-1, nValue);

cout << "The " << nPower << "th power of 8.5 is " << nDisplayValue1;

cout << "The " << nPower << "th power of 8 is " << nDisplayValue2;

}

else

{

return 1;

}

}

int ComputePower(int nPower, float nValue )

{

int nUserInput;

int nDisplayValue1;

float nDisplayValue2;

nDisplayValue1 == ComputePower(nUserInput, 8);

nDisplayValue2 == ComputePower(nUserInput, 8.5f);

}

#include <iostream>

using namespace std;

float ComputePower(int nPower, float nValue);

int main ()

{

int nDisplayValue1;

float nDisplayValue2;

int nPower;

int nValue=0;

cout << "Please enter a postive integer or enter a negative integer to end:" << endl;

cin >> nPower;

if (nPower > 0)

{

return nValue * ComputePower(nPower-1, nValue);

cout << "The " << nPower << "th power of 8.5 is " << nDisplayValue1;

cout << "The " << nPower << "th power of 8 is " << nDisplayValue2;

}

else

{

return 1;

}

}

float ComputePower(int nPower, float nValue )

{

int nUserInput=0;

int nDisplayValue1;

float nDisplayValue2;

nDisplayValue1 = ComputePower(nUserInput, 8);

nDisplayValue2 = ComputePower(nUserInput, 8.5f);

return nDisplayValue1 && nDisplayValue2;

}

Consider

#include <math.h>

template<int N>

doubleComputePower (double d) {

return pow ( d, N);

}

and think about what templates actually serve for...

Consider

#include <math.h>

doubleComputePower (double d1, d2) {

return pow ( d1, d2);

}

double ReturnDouble() {

double d = 3.141;

return d;

}

int ReturnInt () {

int n = 42;

return n;

}

You should refer back to your teachers original requirements of "to write a template function that takes two arguments: an integer n and a variable v of a parameterized type. The function computes and returns the nth power of v."

None of what you've posted so far meets that requirement. Here is exactly the function template that you need:

template<typename TYPE>

TYPE ComputePower( int nPower, TYPE nValue )

{

if ( nPower > 0 )

{

// Because X^Y = X * (X^(Y-1))

return nValue * ComputePower(nPower-1, nValue);

}

else

{

// Because X^0 = 1

return 1;

}

}

This function template solves all of your problems. You see where it says "typename TYPE", that is your way of telling the compiler that sometimes you want to call this function with an int, sometimes you want to call it with a float, and sometimes you want to call it with a double. You can just use this function, and use whatever datatype you want in place of TYPE, and the compiler will do the rest for you. This is the solution that your teacher wanted.

Now all you have to do is show that it works (it does, I tested it) by calling it with an int (the example your teacher wanted you to use is 8) and then calling it again with a float or double (e.g, 8.5).

For instance,

double x = ComputePower( 2, 8.5 ); // x = 72.25

int i = ComputePower( 2, 8 ); // i = 64

See? You can just that one function template for BOTH types (double and int), and the compiler works it out for you.

Hope this helps,

Dex*

Well, you can't use pow() because that requires doubles, which can't meet the requirement of "a template function that takes two arguments: an integer n and a variable v of a parameterized type" without a lot of casting. But here is a solution that meets the requirements, but is not recursive:

template<typename TYPE>

TYPE ComputePower( int nPower, TYPE nValue )

{

TYPE nReturnValue = 1;

while ( nPower-- > 0 )

{

nReturnValue *= nValue;

}

return nReturnValue;

}

Is that better, jkr? :)

Dex*

I just think that templates as C++ understands them seem to be a *too* advanced topic given the questions Marcy was asking...

template<typename TYPE>

TYPE ComputePower( int nPower, TYPE nValue) is. In my class, we have not learned anything like this. So, I am confused about what I am suppose to do. Please help so I can figure this thing out.

You would understand if I had something like this, right?

int ComputePowerInt( int nPower, int nValue )

{

int nReturnValue = 1;

while ( nPower-- > 0 )

{

nReturnValue *= nValue;

}

return nReturnValue;

}

Then you can just use code like this:

int nX = ComputePowerInt( 2, 8 ); // Find 8 squared

int nY = ComputePowerInt( 3, 9 ); // Find 9 to the 3rd power

That would be great for working with ints. But if you also wanted to do floats, you would need something like this:

int ComputePowerFloat( int nPower, float nValue )

{

float nReturnValue = 1;

while ( nPower-- > 0 )

{

nReturnValue *= nValue;

}

return nReturnValue;

}

And for doubles, you would need something like this:

int ComputePowerDouble( int nPower, double nValue )

{

double nReturnValue = 1;

while ( nPower-- > 0 )

{

nReturnValue *= nValue;

}

return nReturnValue;

}

Isn't that a big hassle? To have 3 versions of the exact same thing, with the only difference being the datatype? Wouldn't it be nice if I could write that one time and use it with different datatypes? Well, in C++, you can. It is called a function template!

So take a look at this:

template<typename TYPE>

TYPE ComputePower( int nPower, TYPE nValue )

{

TYPE nReturnValue = 1;

while ( nPower-- > 0 )

{

nReturnValue *= nValue;

}

return nReturnValue;

}

The "template<typename TYPE>" means "I want to use different datatypes with this function. So, I'm just going to use the word TYPE in place of the datatype name to be used". If you do it like that, then you can use code like this:

double x = ComputePower( 2, 8.5 ); // x = 72.25

int i = ComputePower( 2, 8 ); // i = 64

See? I call the same function, sometimes with Floats, and sometimes with Ints, but it still works!

Okay, that's the best explanation I can give you for Function Templates. Any good C++ book will have a chapter on them. If your book doesn't, try this link instead:

http://cplus.about.com/library/weekly/aa070202a.htm

Hope this helps,

Dex*

#include <iostream>

#include <math.h>

using namespace std;

int ComputePower (int nPower, int nValue);

int main ()

{

int nDisplayValue1=0;

float nDisplayValue2=0;

int nPower;

int nReturnValue=0;

int nX=0;

int nY=0;

cout << "Please enter a postive integer or enter a negative integer to end:" << endl;

cin >> nPower;

if (nPower > 0)

{

cout << "The " << nPower << "th power of 8.5 is " << nX<< endl;

cout << "The " << nPower << "th power of 8 is " << nY << endl;

//return nValue * ComputePower(nPower-1, nValue);

}

else

{

return 1;

}

return 0;

}

int ComputePowerInt( int nPower, int nValue )

{

int nReturnValue = 1;

while ( nPower-- > 0 )

{

nReturnValue *= nValue;

int nX = ComputePowerInt( 2, 8 ); // Find 8 squared

int nY = ComputePowerInt( 3, 9 );

}

return nReturnValue;

}

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Try this as a solution:

#include <iostream>

#include <math.h>

using namespace std;

template<typename TYPE>

TYPE ComputePower( int nPower, TYPE nValue )

{

TYPE nReturnValue = 1;

while ( nPower-- > 0 )

{

nReturnValue *= nValue;

}

return nReturnValue;

}

int main ()

{

int nPower;

do

{

cout << "Please enter a postive integer or enter a negative integer to end: " << endl;

cin >> nPower;

if ( nPower >= 0 )

{

cout << "The " << nPower << "th power of 8 is " << ComputePower(nPower, 8) << endl;

cout << "The " << nPower << "th power of 8.5 is " << ComputePower(nPower, 8.5) << endl;

}

}

while ( nPower >= 0 );

return 0;

}

Does that make more sense?

Dex*