Please follow ALL the instructions and solve it by C++. Please and thank you!
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.
What to Submit in Canvas
Submit both a test driver main() function (that tests at least all the below examples) as well as your writeArrayNthBackward function. You can submit the entire lab in a single file if you prefer (the function and a driver main()) together in one .cpp file.
Example inputs and outputs:
Input string | N | Output |
abc123 | 2 | 2ca (sum of numbers removed: 4) |
hello | 1 | olleh (sum of numbers removed: 0) |
hello | 3 | l (sum of numbers removed: 0) |
c1c1c1 | 2 | ccc (sum of numbers removed: 3) |
Thanks for the question.
Below is the code you will be needing Let me know if you have
any doubts or if you need anything to change.
Thank You !!
==============================================================================================
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
void writeArrayNthBackward(const char [], int, int, int);
void writeArrayNthBackward(const char array[], int first,
int last, int N){
if(last<first)return;
if ((last-first)%N==0)
cout<<array[last];
writeArrayNthBackward(array,first,last-1,N); // recursively call
the same function
}
int main() {
string input;
int N;
cout<<"Enter a string: ";cin>>input;
cout<<"Enter the value of N: "; cin>>N;
const char *s = input.c_str();
writeArrayNthBackward(s, 0, strlen(s) - 1,N);
Get Answers For Free
Most questions answered within 1 hours.