Home > Back-end >  Write a function that for a given a non-negative int n, returns the count of the occurrences of 9 as
Write a function that for a given a non-negative int n, returns the count of the occurrences of 9 as

Time:06-22

I can't solve this problem and I've been trying to wrap my head around it for a couple of days now. Here's the full text of the problem:

Write a function that for a given a non-negative int n, returns the count of the occurrences of 9 as a digit, except that a 9 with another 9 immediately to its left counts double, so 9912349 yields 4.

The problem has two parts, a) and b).

a) part requires that this problem be solved using recursion, and

b) part requires iteration.

I'm mostly having trouble with the recursive problem. Here's my code for a) part:

#include <stdio.h>
#include <stdlib.h>

/* Write a function that for a given a non-negative int n, computes the count of the 
occurrences of 9 as a digit, except
that an 9 with another 9 immediately to its left counts double, so 9914329 yields 4. */

int recursiveNines(int mynumber) {
    int counter = 0;
    if (mynumber = 0) {
        return 0;
    }
    if (mynumber / 10 == 9) {
        counter  ;
    }
    else if (mynumber / 10 == 9 && mynumber / 100 == 9) {
        counter  = 2;
    }
    return mynumber   recursiveNines(mynumber / 10);
}

I think the function is okay, but I'm unsure how to return the final counter and therefore test my function.

Here's my code for the iteration of this problem:

int iterateNines(int mynumber) {
    int counter = 0;
    do {
        if (mynumber / 10 == 9) {
            counter  ;
        }
        else if (mynumber / 10 == 9 && mynumber / 100 == 9) {
            counter =2;
        }
    } while (mynumber != 0);

    return counter;
}

CodePudding user response:

There are multiple problems:

  • There is a typo in if (mynumber = 0): this assigns 0 to mynumber and evaluates to false.

  • Furthermore, your test if (mynumber / 10 == 9) does not check if the last digit of mynumber is a 9: you should instead use

    if (mynumber % 10 == 9)
    
  • The else clause is incorrect too: you should instead check for a second 9 if the last digit is a 9 already. Testing this condition in the else branch would always fail.

  • The return statement is incorrect: you should add counter, not mynumber to the recursive call.

Here are modified versions:

int recursiveNines(int mynumber) {
    int counter = 0;
    if (mynumber == 0) {
        return 0;
    }
    if (mynumber % 10 == 9) {
        counter  ;
        if (mynumber / 10 % 10 == 9)
            counter  ;
    }
    return counter   recursiveNines(mynumber / 10);
}

int iterateNines(int mynumber) {
    int counter = 0;
    while (mynumber != 0) {
        if (mynumber % 10 == 9) {
            counter  ;
            if (mynumber / 10 % 10 == 9) {
                counter  ;
        }
        mynumber /= 10;
    }
    return counter;
}

Here is an alternative for the recursive version using a single expression:

int recursiveNines(int n) {
    return (n == 0) ? 0 :
           (n % 10 == 9)   (n % 100 == 99)   recursiveNines(n / 10);
}

CodePudding user response:

Several issues:

  • mynumber = 0 instead of mynumber == 0
  • The else if (mynumber / 10 == 9 && mynumber / 100 == 9) branch will never be taken, by construction, because the first branch will be taken first.
  • You use the division (/) instead of the modulo (%).

Try:

int recursiveNines(int mynumber) {
  int counter = 0;

  if (mynumber == 0) {
    return 0;
  }
  if (mynumber % 100 == 99) {
    counter = 2;
  } else if (mynumber % 10 == 9) {
    counter = 1;
  }
  return counter   recursiveNines(mynumber / 10);
}
  • Related