Home > front end >  C recursive function to find common digits between 2 long numbers
C recursive function to find common digits between 2 long numbers

Time:12-24

I got a task for hw to make a recursive function that checks between 2 unsigned long long numbers and check for common digits if there is its will print the common digits (its starts checking from right to left- when a match found we stop and print that digit) if there is no same digits its will return -1

Example:

22222446, 113355578889 => function will give back -1

13259438, 2 => function will give back 2

112233445, 112233445 => function will give back 5.

The problem is I could make it work for the first and last example but I am having trouble dealing with the second example. any hints will be appreciated :)

int findCommonDigit(unsigned long long n1, unsigned long long n2) {

    int temp1 = 0, temp2 = 0;
    if (n1 == 0 || n2 == 0)
        return -1;
    temp1 = n1 % 10;
    temp2 = n2 % 10;
    if (temp1 == temp2)
        return temp1;
    return findCommonDigit(n1 / 10, n2 / 10);

}

CodePudding user response:

You can use a loop based function instead like following:

int findCommonDigit(unsigned long long n1, unsigned long long n2) {
    int digitN1[11] = {0}, digitN2[11] = {0};
    while(n1) {
        int x = n1 % 10;
        n1 /= 10;
        digitN1[x] = 1;
    }
    while(n2) {
        int x = n2 % 10;
        n2 /= 10;
        digitN2[x] = 1;
    }
    for(int i=0; i<10; i  ) {
        if(digitN1[i] && digitN2[i]) {
            return i;
        }
    }
    return -1;
}

Here digitN1 and digitN2 are two flag arrays used to store whether digits[0..9] are set on numbers n1 and n2 respectively. Then we use a for loop to check whether any digit is on both numbers or not. If no common digits found, -1 is returned.

CodePudding user response:

You can write a while loop until "n2 > 0" in main function. For every digit you will call "findCommonDigit" function and check is the return value is -1 or not. If you get a digit that is not -1 then print it and break the While loop. After the while loop you can write printf("-1"). For that you have to change this return findCommonDigit(n1 / 10, n2 / 10); to this return findCommonDigit(n1 / 10, n2);

Otherwise you can change your function to this,

int findCommonDigit(unsigned long long n1, unsigned long long n2) {
    int temp1 = 0, temp2 = 0;
    if (n1 == 0 || n2 == 0){
        return -1;
    }
    temp1 = n1 % 10;
    temp2 = n2 % 10;
    if (temp1 == temp2){
        return temp1;
    }
    n1 /= 10;
    return findCommonDigit(n1, n2);
    n2 /= 10;
    return findCommonDigit(n1, n2);
}

CodePudding user response:

This is what I came up with based on your test cases.

#include <stdio.h>

#define DEBUG_FUNCTION 0

int findCommonDigit(unsigned long long n1, unsigned long long n2)
{
    int n1RightDigit = n1 % 10;
    if(DEBUG_FUNCTION) printf("n1 right most digit : %d\n", n1RightDigit);
    int n2RightDigit = n2 % 10;
    if(DEBUG_FUNCTION) printf("n2 right most digit : %d\n", n2RightDigit);
    
    if (n1RightDigit != n2RightDigit)
    {
        if(DEBUG_FUNCTION) printf("Not a match.\n");
        if (n1 > 10)
        {
            findCommonDigit(n1 / 10, n2);
        }
        else
        {
            if (n2 > 10)
            findCommonDigit(n1, n2 / 10);
            return -1;
        }
    }
    else
    {
        return n1RightDigit;
    }
}
    
int main()
{
    int result;
    
    // Test Case 1
    // 22222446, 113355578889 => function will give back -1
    result = findCommonDigit(22222446, 113355578889);
    if (result == -1)
    {
        printf("Corrent Result\n\n");
    }
    else
    {
        printf("Result NOT corrent!\n\n");
    }
    
    // Test Case 2
    // 13259438, 2 => function will give back 2
    result = findCommonDigit(13259438, 2);
    if (result == 2)
    {
        printf("Corrent Result\n\n");
    }
    else
    {
        printf("Result NOT corrent!\n\n");
    }
    
    // Test Case 3
    // 112233445, 112233445 => function will give back 5.
    result = findCommonDigit(112233445, 112233445);
    if (result == 5)
    {
        printf("Corrent Result\n\n");
    }
    else
    {
        printf("Result NOT corrent!\n\n");
    }

    return 0;
}
  •  Tags:  
  • c
  • Related