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 of9as a digit, except that a9with another9immediately to its left counts double, so9912349yields 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 assigns0tomynumberand evaluates to false.Furthermore, your test
if (mynumber / 10 == 9)does not check if the last digit ofmynumberis a9: you should instead useif (mynumber % 10 == 9)The
elseclause is incorrect too: you should instead check for a second9if the last digit is a9already. Testing this condition in theelsebranch would always fail.The
returnstatement is incorrect: you should addcounter, notmynumberto 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 = 0instead ofmynumber == 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);
}
