Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

[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 ) {

 if ( n < 1 ) return 1; 
 else return 3 * fn(n - 1); 

}

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.

Its 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

#include  using namespace std; void f( int[],int ); void fr (int[], int, int); double q( double[], int); double qr( double[], int, int); int main() { int myArray[5] = { 66, -100, 5, 0, -14 }; cout << "1 -- Print the array out: " << endl; f(myArray, 5); cout << endl; cout << "2 -- Print the array out (Recursive Version): " << endl; fr( myArray, 0, 5 ); cout << endl; double profit[4] = { 6.6, -10, 0.5, -1.4 }; double sum; cout << "3 -- Culculate Print the profit out: " << endl; sum = q( profit, 4 ); cout << sum << endl; cout << "4 -- Culculate Print the profit out (Recursive Version): " << endl; sum = qr( profit, 0, 4 ); cout << sum << endl; return 0; } //prints contents of integer array void f( int ar[],int size ) { for (int i = 0; i < size; i++ ) cout << ar[i] << endl; } //recursive version of f void fr (int ar[], int current, int size) { if ( current < size ) { cout << ar[current] << endl; fr( ar, current+1, size ); } } //calculates the sum of elements in double type array pr double q( double pr[], int size ) { double sum = 0; for ( int i = 0; i < size; i++ ) if (pr[i] > 0) sum = sum + pr[i]; return sum; } //recursive version of q double qr( double pr[], int current, int size ) { if ( current < size ) { if ( pr[current] > 0 ) return pr[current] + qr(pr,current+1,size); else return qr(pr,current+1,size); } else return 0; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

4. Describe the factors that influence self-disclosure

Answered: 1 week ago

Question

1. Explain key aspects of interpersonal relationships

Answered: 1 week ago