简体   繁体   中英

Counting number of digits in an integer through recursion

My code is following:

/counting number of digits in an integer

#include <iostream>
using namespace std;

int countNum(int n,int d){
    if(n==0)
    return d;
    else
    return (n/10,d++);
}
int main(){
    int n;
    int d;
    cout<<"Enter number"<<endl;
    cin>>n;
   int x=countNum();
    cout<<x;
    return 0;
}

i cannot figure out the error,it says that : too few arguments to function `int countNum(int, int)' what is issue?

Because you declared the function to take two arguments:

int countNum(int n,int d){

and you are passing none in:

int x = countNum();

You probably meant to call it like this, instead:

int x = countNum(n, d);

Also this:

return (n/10,d++);

should probably be this:

return countNum(n/10,d++);

Also you are not initializing your n and d variables:

int n;
int d;

Finally you don't need the d argument at all. Here's a better version:

int countNum(int n){
    return (n >= 10)
        ? 1 + countNum(n/10)
        : 1;
}

and here 's the working example.

int x=countNum(); the caller function should pass actual arguments to calling function. You have defined function countNum(int, int) which means it will receive two ints as arguments from the calling function, so the caller should pass them which are missing in your case. Thats the reason of error too few arguments.

Your code here:

int x=countNum();

countNum needs to be called with two integers. eg

int x=countNum(n, d);

Because you haven't passed parameters to the countNum function. Use it like int x=countNum(n,d);

Change it to:

int x=countNum(n,0);

You don't need to pass d in, you can just pass 0 as the seed.

Also change countNum to this:

int countNum(int n,int d){
    if(n==0)
    return d;
    else
    return coutNum(n/10,d+1); // NOTE: This is the recursive bit!
}
#include <iostream>
using namespace std;

int countNum(int n,int d){
    if(n<10)
        return d;
    else
        return countNum(n/10, d+1);
}
int main(){
    int n;
    cout<<"Enter number"<<endl;
    cin>>n;
    int x=countNum(n, 1);
    cout<<x;
    return 0;
}

Assuming this is not for an assignment, there are better ways to do this (just a couple of examples):

Convert to string

unsigned int count_digits(unsigned int n)
{
    std::string sValue = std::to_string(n);
    return sValue.length();
}

Loop

unsigned int count_digits(unsigned int n)
{
    unsigned int cnt = 1;
    if (n > 0)
    {
        for (n = n/10; n > 0; n /= 10, ++cnt);
    }
    return cnt;
}

Tail End Recursion

unsigned int count_digits(unsigned int n, unsigned int cnt = 1)
{
    if (n < 10)
        return cnt;
    else
        return count_digits(n / 10, cnt + 1);
}

Note: With tail-end recursion optimizations turned on, your compiler will transform this into a loop for you - preventing the unnecessary flooding of the call stack.

Your function is written incorrectly. For example it is not clear why it has two parameters or where it calls recursively itself.

I would write it the following way

int countNum( int n )
{
   return 1 + ( ( n /= 10 ) ? countNum( n ) : 0 );
}

Or even it would be better to define it as

constexpr int countNum( int n )
{
   return 1 + ( ( n / 10 ) ? countNum( n/10 ) : 0 );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM