TransWikia.com

problem writing a recursive function in C (digits order in a nunber)

Stack Overflow Asked on January 7, 2021

I am trying to write a function that reads two numbers and checks if the digits of ‘n’ are in descending or ascending order and return a different value (0/1) for different scenarios of ‘f’.

  • if f=0 and digits are descending (example: 4321) return 1
  • if f=1 and digits are ascending (example: 1234) return 1
  • if f=2 and digits are ascending at first and then stop and start descending
    until end of number return 1 (example: 1234321)

for some reason I get ‘1’ even when I input f=0, n=5321
‘5321’ shouldn’t return 1 since 5 doesn’t follow 3.
i deleted the f=1, f=2 parts because I’m focusing on fixing the problem with f=0 for now.

I would really appreciate the help.

updated code:

void Ex2()
{
    int n, f, ans;
    printf("Enter a number: ");
    scanf_s("%d", &n);
    printf("Enter a number betwen 0-3 : ");
    scanf_s("%d", &f);
    ans = zeroORone(n, f);
    printf("ans= %dn", ans);
}

int zeroORone(int n, int f)
{
    int result = 1;
    printf("n= %dn", n);
    int num1, num2, i = 10;
    num1 = n % 10;
    num2 = n / 10 % 10;
    if (f == 0)
    {
        if (num1 != (num2 - 1))
        {
            result = 0;
        }
    }
    if (result == 0) return result;
    if (result == 1)
    {
        n /= 10;
        if (n > 9)
            zeroORone(n, f);
    }
    printf("res = %dn", result);
    return result;
}

One Answer

The main issue with your code (ignoring that it doesnt check for f ==1 or 2, is that it also never get the result from the recusion call - you pass in result to the recursion from the last call, but because result is an int, it is passed by copy so does not magically update the result of result in past calls. You could return the value back.

result = zeroORone(n, f, result);

Perhaps something like this? I dont use the result input so didnt include it in the code)

To make it easier, it works from the unit digits first and works its way up, so when it is checking its decending, its testing for accending instead from right most digits and going left.

Same for ascending - checks for descending instead.

int zeroORone(int n, int f)
{
  // single digit
  if (n <= 9)
  {
    if(f == 2) return 0;
    else return 1;
  }
  else
  {
    // get curr and next digit (from right)
    int curr = n%10;
    int next = (n/10)%10;

    // check for decending (so check ascending since we are working backwards)
    if(f == 0)
    {
      if (curr + 1 != next) return 0;
    }
    // ascending (so check for descending)
    if(f == 1)
    {
      if (curr - 1 != next) return 0;
    }
    // ascend then descend (so check opposite)
    if (f == 2)
    {
      if (curr - 1 != next)
      {
        // checking if starting other direction
        if (curr + 1 != next)
          return 0;
        // from now check as  decending
        f = 0;
      }
    }
  }
  return zeroORone(n/10, f);
}

Answered by lostbard on January 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP