-6

Even on passing parameters to the function log, error showing too few arguments to function.

#include <iostream>
using namespace std;

int log(int n, int x){
    return (n>1) ? 1 + log(n/x) : 0;
}

int main() {
    int n,x;
    cin>> n>> x;
    cout<< log(n,x);
}

I expect the output of log10(1000) to be 3, but few arguments error is shown.

Igor R.
  • 14,716
  • 2
  • 49
  • 83
Amol
  • 3
  • 3
  • 7
    How many arguments are you passing to the `log` function, in `log(n/x)`? How many, does it expect? – Algirdas Preidžius Jun 21 '19 at 14:02
  • 2
    Your code doesn't contain `log10(1000)`. – melpomene Jun 21 '19 at 14:03
  • 1
    [Cannot reproduce](http://coliru.stacked-crooked.com/a/1415873c8c4281b6). That said, you really sholuld include `` if you are using the C++ standard functions. Also, [get rid of the `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – NathanOliver Jun 21 '19 at 14:04
  • 1
    @NathanOliver this is a recursion, not a call to a standard function. – Igor R. Jun 21 '19 at 14:06
  • 2
    @NathanOliver When trying to reproduce an issue it helps to [use OP's actual code](http://coliru.stacked-crooked.com/a/08b7389830e2acdb). – melpomene Jun 21 '19 at 14:07
  • 1
    @IgorR. If it is recursion, why aren't they using 2 parameters in the recursive call? – NathanOliver Jun 21 '19 at 14:07
  • 1
    To avoid confusion, better not giving to your function a name (`log`) widely used in existing library – Damien Jun 21 '19 at 14:08
  • 5
    @NathanOliver that's exactly what the compiler says :)) – Igor R. Jun 21 '19 at 14:09
  • @Yunnosch ok, I've removed it. Just thought it would make the question more clear. (As you can see, people thought he attepted to use `log` from std math lib). – Igor R. Jun 21 '19 at 14:16
  • @Igor I agree that those people obviously needed some help with understanding the question. Still, to me the tag seemed the wrong way. Note however that I did not delete it either. For that it was still too applicable... if you know what I mean. I also feel with those who misunderstood, avoiding that kind of wrong assumptions is very hard.... – Yunnosch Jun 21 '19 at 16:53

1 Answers1

3

You forgot the second argument to your log function in the recursive step.

return (n>1) ? 1 + log(n/x, x) : 0;

By the way, you should name your variables something descriptive. For instance instead of using n perhaps use base.

FailureGod
  • 332
  • 1
  • 12