Examine the code of recursive functions in Lab10.cpp and see below. Write the fo
ID: 3704502 • Letter: E
Question
Examine the code of recursive functions in Lab10.cpp and see below.
Write the following two functions in the Lab10.cpp file and run each on the array int arr[10] = { 400, 3, -66, 7, -6, 201, 199, -4, 0, 77 };
(1) Write a recursive function that prints out the values of and integer element function from index size-1 to 0.
EXAMPLE: if the array in the parameter to the function were int a[4] = (9, 5, 33, 2}, the output would be 2 33 5 9. Name this function as firstInitialLastNamePrintRecursion.
(2) Write a value-returned recursive function to return the number of integer array elements that are either < 0 or > 99. (50 points each) Name this function as firstInitialLastNamePrintOut0and99Recursion. Write a test program (with main()) to test your two functions out. Be sure that your output is self-explained with nice descriptive sentences to show what will be printed out. For example, you can print out the original array first, then you can print out the list by using recursive algorithm, and last you can print out the numbers that are either <0 or >99.
//Lab10.cpp
Explanation / Answer
#include <iostream>
using namespace std;
// Recursive function to print the array contents recursively
void reverseRecursionReverse(int arrayNum[], int length)
{
// checks if the length is zero
if (length == 0)
// returns from the function
return;
// Otherwise
else
{
// Subtracts one from the current length.
// Displays the array's length index position data
cout<<arrayNum[--length]<<", ";
// Recursively calls the function
reverseRecursionReverse(arrayNum, length);
}// End of else
}// End of function
// Recursive function to display the array contents in the original order
void originalOrderRecursion(int arrayNum[], int currentPos, int length)
{
// Checks if the current position is less than the length
if(currentPos < length)
{
// Displays the array's current index position data
cout<<arrayNum[currentPos]<<", ";
// Recursively calls the function by adding one currentPos, which is the second parameter
originalOrderRecursion(arrayNum, currentPos + 1, length);
}// End of if condition
}// End of function
// Recursive function to return number of elements are either < 0 or > 99.
int firstInitialLastNamePrintOut0and99Recursion(int arrayNum[], int length)
{
// Creates a static counter and assigns zero to it
static int counter = 0;
// Checks if the length is negative
if (length < 0)
// Returns the counter
return counter;
// Otherwise, length is positive
else
{
// Checks if the array's current position is less than zero or greater than 99
if(arrayNum[length] < 0 || arrayNum[length] > 99)
// Increase the counter by one
counter++;
// Recursively calls the function by subtracting one from the length, which is the second parameter
firstInitialLastNamePrintOut0and99Recursion(arrayNum, --length);
}// End of else
}// End of function
// main function definition
int main()
{
// Creates an array
int myArray[10] = { 400, 3, -66, 7, -6, 201, 199, -4, 0, 77 };
// Calls the function to display the original order or the array
cout<<" -- Print the array original order: " << endl;
originalOrderRecursion(myArray, 0, 10);
// Calls the function to display the reverse order or the array
cout<<" -- Print the array reverse order: " << endl;
reverseRecursionReverse(myArray, 10);
// Calls the function to display the the number of elements are either < 0 or > 99
cout<<" -- Print the number of elements are either < 0 or > 99: "<<firstInitialLastNamePrintOut0and99Recursion(myArray, 9);
return 0;
}// End of function
Sample Output:
-- Print the array original order:
400, 3, -66, 7, -6, 201, 199, -4, 0, 77,
-- Print the array reverse order:
77, 0, -4, 199, 201, -6, 7, -66, 3, 400,
-- Print the number of elements are either < 0 or > 99: 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.