There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops.
Consider the following iterative function that prints an array of characters backward:
#include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); }
Iterative loops have one set of local variables that come and go with each cycle -- the previous cycles' values for these variables are completely forgotten and only the present matters (in the above case, this refers to the counter variable i). The cycles are executed in series. But in recursive loops, the cycles all coexist at the same time, each with their own set of local variables. The cycles are executed in parallel. Parallel in this case means simultaneously, not independently, unless parallelism is explicitly used.
We will be using this property of recursion later in the course on more complex data structures. This exercise serves to reinforce your understanding of the simplest form of recursion -- functions that call themselves.
Assignment
Using the above iterative version of writeArrayBackward as a reference, write a recursive version of this function, but with a couple of twists:
1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below).
void writeArrayNthBackward(const char [], int, int, int);
The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n == 1, then the string is printed normally in reverse (no characters are dropped). If n == 2, then every "other" character is printed, and so on.
You can constrain N to be 1 <= N <= 3.
Your code does NOT need to output a newline ('endl') within the recursive function itself (as the above iterative version does), you can output the newline after the return of the function.
2) If any of the removed characters in the string were numbers (not letters), output their sum. For example, if your function removed the numbers 3 and 1 from the string 'abc123', the result would be 3 plus 1, which is 4. See the below table for more examples..
Converting a String Digit to an Integer
If you have a string and want to convert a character in the string to an int, there are a few ways to do it. The easiest is probably this one:
char *a = "123"; int n; n = (int)a[0] - '0';
Notice we can't simply cast a[0] to an int, because that will give us the ASCII code for the character '1', which is 49. We want the value of 'n' to be 1, so by subtracting the ASCII code for 0 (which is 48), it "translates" the character to an integer value.
Hello there. Here's the function that answers your question.
int writeArrayNthBackward(const char anArray[], int first, int
last, int n)
{
if(last<first) //base case
return 0;
int sum=0;
if((strlen(anArray)-last)%n==0) //checking Nth character
{
cout<<anArray[last];
sum+=writeArrayNthBackward(anArray,first,last-1,n);
}
else
{
if(anArray[last]>='0' && anArray[last]<='9')
//checking if its an integer
sum+=(anArray[last]-'0');
sum+=writeArrayNthBackward(anArray,first,last-1,n);
}
return sum; //returning the sum as mentioned in part 2.
}
Hope it serves you well.
Get Answers For Free
Most questions answered within 1 hours.