Examine the code of recursive functions in Lab10.cpp and see below. Write the fo
ID: 3704566 • 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
(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.
[Bonus -20 points] Use STL vector as your data structure. Generate any number (random) of element (including positive and negative numbers) to the vector before calling the 2 recursive functions. This must be well documented in order to receive the points.
A RECURSIVE FUNCTION is a function that calls itself. Example:
int fn( int n ) {
}
Note that the value of
fn(4) = 3 * fn(3) = 3 * 3 * fn(2) = 3 * 3 * 3 * fn(2) =
3 * 3 * 3 * 3 * fn(1) = 3 * 3 * 3 * 3 * 1 or 81 or 34.
It’s easily seen that if n is bigger then 1, then fn( n ) = 3n.
Read over the examples of using recursive functions on arrays in Lab10.cpp. You will have to add two new functions as described above.
Lab10.cpp
Explanation / Answer
1. Solution
#include <iostream>
using namespace std;
void f(int[], int);
void firstInitialLastNamePrintRecursion(int[], int);
void firstInitialLastNamePrintOut0and99Recursion(int[], int);
int main()
{
int myArray[10] = {111, 2, -444, 299, -55, 98, 21, 34, 43, 12};
cout << "My Array: "<< endl;
f(myArray, 10);
cout << endl;
cout << "Printing the array using firstInitialLastNamePrintRecursion:"<< endl;
firstInitialLastNamePrintRecursion(myArray, 10);
cout << endl;
cout << "Printing the array using firstInitialLastNamePrintOut0and99Recursion:"<< endl;
firstInitialLastNamePrintOut0and99Recursion(myArray, 10);
cout << endl;
return 0;
}
void f(int ar[], int size )
{
for (int i = 0; i < size; i++ )
cout << ar[i] << " ";
}
void firstInitialLastNamePrintRecursion(int ar[], int size)
{
if(size > 0)
{
cout << ar[size - 1]<< " ";
firstInitialLastNamePrintRecursion(ar, size-1);
}
else
{
cout<<endl;
}
}
void firstInitialLastNamePrintOut0and99Recursion(int ar[], int size)
{
if(size > 0)
{ if(ar[size -1] < 0 || ar[size -1] > 99)
{
cout << ar[size -1]<< " ";
}
firstInitialLastNamePrintOut0and99Recursion(ar, size-1);
}
else
{
cout<<endl;
}
}
2. For extra marks(using STL)
#include <iostream>
#include <vector>
#include <algorithm>
#include<ctime>
using namespace std;
void f(vector<int>, int);
void firstInitialLastNamePrintRecursion(vector<int>, int);
void firstInitialLastNamePrintOut0and99Recursion(vector<int>, int);
int main()
{
std::srand((unsigned)time(0));
std::vector<int> myvector;
for(int i=0; i<5;i++)
{
int a = rand() % 500 + 1;
myvector.push_back(i);
}
for(int i=0; i<5;i++)
{
int a = -(rand() % 500 + 1);
myvector.push_back(i);
}
cout << "My Array: "<< endl;
f(myvector, 10);
cout << endl;
cout << "Printing the array using firstInitialLastNamePrintRecursion:"<< endl;
firstInitialLastNamePrintRecursion(myvector, 10);
cout << endl;
cout << "Printing the array using firstInitialLastNamePrintOut0and99Recursion:"<< endl;
firstInitialLastNamePrintOut0and99Recursion(myvector, 10);
cout << endl;
return 0;
}
void f(vector<int> ar, int size )
{
for (int i = 0; i < size; i++ )
cout << ar[i] << " ";
}
void firstInitialLastNamePrintRecursion(vector<int> ar, int size)
{
if(size > 0)
{
cout << ar[size - 1]<< " ";
firstInitialLastNamePrintRecursion(ar, size-1);
}
else
{
cout<<endl;
}
}
void firstInitialLastNamePrintOut0and99Recursion(vector<int> ar, int size)
{
if(size > 0)
{ if(ar[size -1] < 0 || ar[size -1] > 99)
{
cout << ar[size -1]<< " ";
}
firstInitialLastNamePrintOut0and99Recursion(ar, size-1);
}
else
{
cout<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.