Recursion

Apr 29, 2015 at 6:16pm
Hello, I need some help with this problem. I am not proficient in C++ and need some advice

Here are the instructions:

The Power function calculates the power of a base "a" raised to an exponent "n". Write a class which that you will call Power class in a header file called APower.h with a method "Apowerfun(a,n)" that prints the corresponding power value. Remember to use "float" for a and "long" for n and the returned value to be a "float" because the number is a real number

Example:

APowerfun(5.0,2)=(5.0)2= 25.0

APowerfun(5.0,-2)=(5.0)-2 = (1/25)=0.04.

Remember the values of n can be postive or negative. Your code should be able to deal with both cases. Remember to limit the value of n to the value 40 maximum.

The Assignment will require you to create 2 files:

1- APower.h which contain the details of creating the Power class and the method Apowerfun() which should calculate the power of any number a raised to the exponent n. Remember that you are writing a recursive function for Apowerfun().

2- APower.cpp which reads in a long number n, and a float number a, and prints the corresponding power result which is a float.


Here are my files:

APower.h:

#ifndef APOWER_H
#define APOWER_H

#include <iostream>
#include <iomanip>

using namespace std;


class Apower
{
public:

float apowerfun(float a, long n)
{
if (a == 0)
{
return (0);
}

if (n == 0)
{
return (1);
}
else if (n > 0)
{
return (a * apowerfun(a, n - 1));
}

}

};
#endif


Here is my APower.cpp:

#include <iostream>
#include <iomanip>
#include "APower.h"

using namespace std;


void main()
{
Apower ap1(float a, long n);

float a;
long n;

cout << "Enter base as an integer: ";
cin >> a;


cout << "Enter exponent as an integer: ";
cin >> n;


Apower* apowerfun = new Apower(ap1(a, n));


cout << "\nResult: " << apowerfun << endl;

cin.get();
cin.get();


And here are the errors I get:

APower.obj : error LNK2019: unresolved external symbol "class Apower __cdecl ap1(float,long)" (?ap1@@YA?AVApower@@MJ@Z) referenced in function _main
1>C:\Users\alpha\documents\visual studio 2013\Projects\Assign7\Debug\Assign7.exe : fatal error LNK1120: 1 unresolved externals

If you have any suggestions, please let me know, thanks
Apr 29, 2015 at 6:26pm
You should use the code tags to make the posted code more readable.


In function main() you do have a line:
Apower ap1(float a, long n);

What do you think that is does?
Apr 30, 2015 at 3:26pm
I don't know exactly what it does, but if I wrote that part of the code wrong, could you tell me what to replace it with?
Apr 30, 2015 at 3:34pm
Apr 30, 2015 at 5:05pm
Ok, I changed the code and got it down to one error.

Here is my new code:

#include <iostream>
#include <iomanip>
#include "APower.h"


using namespace std;


void main()
{
Apower ap1(float apower);

float a;
long n;


cout << "Enter base as an integer: ";
cin >> a;


cout << "Enter exponent as an integer: ";
cin >> n;


Apower* apowerfun = new Apower(ap1(a, n));


cout << "\nResult: " << apowerfun << endl;

cin.get();

};


Here is the error:

c:\users\alpha\documents\visual studio 2013\projects\assign5\assign5\apower.cpp(25): error C2660: 'ap1' : function does not take 2 arguments


I know it say to have two arguments, but I don't know how to write the code for these. Any suggestions?
May 1, 2015 at 4:54pm
Ok, I figured it out. Thanks for your help.
Topic archived. No new replies allowed.