Search code examples
crecursion

Struggling to build an ascending and alternating recursive function identifier in C


I am trying to build an algorithm by C programming that gets any positive integer and says if it's an ascending and alternating series of even and odd digits (9876 or 8765 from right to left) or not.

  • The getNumber function is supposed to ensure the integer is positive.
  • The isAscendingOrder function is supposed to check if the digits in the integer ascend - Returning 1 if they do or return 0 if not.
  • The isAlternateNumber function is supposed to check if the series alternates between even and odd numbers by recursion - Return 1 if it does (even, odd, even, odd,...,0) or return 0 if it doesn't.

My issue here is that I don't know how to build a recursion function that will detect if the integer is a series that alternates, or even how to start. It would be very helpful if someone could guide me on how I should start or how to find the pattern.

Code:

#include <stdio.h>

int getNumber();
int isAscendingOrder(int num);
int isAlternateNumber(int num);

void main()
{
    //Note: Maybe using long long declaration!
    int num = getNumber();
    
    if (isAscendingOrder(num) && isAlternateNumber(num))
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }
}

int isAlternateNumber(int num)
{
    printf("Starting test with: %d\n", num);

    if (num <= 0)
        return 1;
    else
        return num / isAlternateNumber((num % 2) - 1);
}

int isAscendingOrder(int num)
{
    int ascendingNum = 0, editedNum = 0, alteraningNum, a, b;

    alteraningNum = num;
    editedNum = num;

    while (num > 0)
    {
        //Fix for 0
        if (num <= editedNum)
        {
            a = (num % 10);
            num /= 10;
            /*printf("a = %d\n", a);*/
        }

        if (num < editedNum)
        {
            b = (num % 10);
            editedNum = num;
            /*printf("b = %d\n", b);*/
        }
        
        if ((b < a) && (b != 0)) {
            return 0;
        }
    }

    isAlternateNumber(alteraningNum);

    return 1;
}

int getNumber(int validNum)
{
    do
    {
        printf("Please enter a positive integer: ");
        scanf_s("%d", &validNum);
    } while (validNum < 0);

    return validNum;
}

Note: I'm only allowed to use arithmetic operations, condition statements, loops and functions (including recursion). I can't use structures, methods or anything else that I didn't mention here.


Solution

  • Note: recursion here is not an efficient approach, yet a reasonable learning exercise.

    Use %10 to isolate the least significant digit.


    If isAscendingOrderfunction() is to take only 1 argument, it can test the order of the least 2 significant digits.

    // For positive `x`.
    bool isAscendingOrderfunction(int x) {
      int ones_place = x % 10;
      x /= 10;
      int tens_place = x % 10;
      return x == 0 //
          || (tens_place > ones_place && isAscendingOrderfunction(x));
    }
    

    Like-wise isAlternateNumber() can also compare the even/oddness of the 2 least significant digits. A value is even if the remainder, after division by 2, is 0, otherwise the integer is odd.

    // For all `x`.
    bool isAlternateNumber(int x) {
      bool ones_place_is_even = (x % 2) == 0;
      x /= 10;
      bool tens_place_is_even = (x % 2) == 0;
      return x == 0 || //
          ((ones_place_is_even != tens_place_is_even) && isAlternateNumber(x));
    }
    

    Simplifications exsits, but something to get OP started.