The problem with your code is with the variable count. First of all count()
is a function alredy defined in c++ library so the error is coming try
changing the count to some other variable.
Even if you change the variable name you may get problems because If you
want to retain the value of the variable count during the recursive calls
you have to declare it as static (like static int var).
So change the name of the variable and make it static to get the expected
result.

-nag.



On 3/5/07, Tomoyo Daidouji <[EMAIL PROTECTED]> wrote:
>
>   --- In [email protected] <c-prog%40yahoogroups.com>, Ray Devore
> <[EMAIL PROTECTED]> wrote:
>
> >
> > --- Tomoyo Daidouji <[EMAIL PROTECTED]> wrote:
> > > Hi all,
> > >
> > > I need to write a recursive function to calculate
> > > a^n. When the
> > > function call ends, it needs to return the number of
> > > multiplications
> > > it did. I've written the code, which calculates a^n,
> > > but I can't get
> > > the count to work properly. Could you tell me where
> > > I'm going wrong.
> > > I'm getting the following error:
> > >
> > > power_alg.cpp:33: error: reference to âcountâ is
> > > ambiguous
> > > power_alg.cpp:5: error: candidates are: int count
> > >
> > > Below is the complete code I'm using.
> > >
> > >
> > > #include <iostream>
> > > using namespace std;
> > >
> > > int count = 0;
> > > float Power(float a, int n)
> > > {
> > > float result;
> > >
> > > if (n == 0)
> > > {
> > > result = 1;
> > > count = 0;
> > > }
> > > else if (n == 1)
> > > {
> > > result = a;
> > > count = 0;
> > > }
> > > else if (n % 2 == 0)
> > > {
> > > result = Power(a*a, n/2);
> > > count++; //count as one multiplication
> > > }
> > > else if (n % 2 == 1)
> > > {
> > > result = Power(a*a, (n-1)/2) * a;
> > > count += 2; //count as two multiplications
> > > }
> > >
> > > return result;
> > >
> > > } //end Power function
> > >
> > > int main()
> > > {
> > > float a;
> > > int n;
> > >
> > > cout << "Please enter a number: ";
> > > cin >> a;
> > >
> > > cout <<"Please enter the power you're raising the
> > > number to: ";
> > > cin >> n;
> > >
> > > float answer = Power(a, n);
> > >
> > > cout << "The result is: " << answer << endl;
> > > cout << "The number of multiplications it takes
> > > is: " << count <<
> > > endl;
> > >
> > > } //end main function
> > >
> > > I would appreciate it very much if someone could
> > > help me out here.
> > >
> > > Thanks,
> > > Chai.
> > >
> > >
> > Chai,
> >
> > I complied your code and didn't have any problems.
> > What compiler are you using? Did you copy your code
> > into the e-mail or did you retype it?
> >
> > Ray
>
> Ray,
>
> I copied the code into the email. I'm not really sure what compiler
> the university is using. The OS I'm compiling it is linux, if that
> makes any difference.
>
> Chai.
>
>  
>


[Non-text portions of this message have been removed]

Reply via email to