Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 1: Write a recursive function that will compute the sum of the fi rst n integers in an array of at least n integers.

Exercise 1:

Write a recursive function that will compute the sum of the fi rst n integers in an array of at least n integers. Hint: Begin with the n th integer. 4.

Given two integers, start and end , where end is greater than start , write a recursive C++ function that returns the sum of the integers from start through end , inclusive.

Given an integer n > 0, write a recursive C++ function that writes the integers 1, 2, . . ., n. Should be done without trail recursion.

Given an integer n > 0, write a recursive C++ function that returns the sum of the squares of 1 through n

Write a recursive C++ function that writes the digits of a positive decimal integer in reverse order.

Embed all five of these in a driver program to thoroughly test them.

Part 2:

Write a recursive function named reverseWithinBounds that has an argument that is an array of characters and two arguments that are bounds on array indices. The function should reverse the order of those entries in the array whose indices are between the two bounds (including the bounds). For example, if the array is:

a[0] == 'A' a[1] == 'B' a[2] == 'C' a[3] == 'D' a[4] == 'E'

and the bounds are 1 and 4, then after the function is run the array elements should be:

a[0] == 'A' a[1] == 'E' a[2] == 'D' a[3] == 'C' a[4] == 'B'

Embed the function in a program and test it. After you have fully debugged this function, define another function named reverseCstring that takes a single argument that is a C string and modifies the argument so that it is reversed. This function will include a call to the recursive definition you did for the first part of this project, and need not be recursive. Embed this second function in a program and test it. Turn in only this final result (with output, of course).

Part 3:

Write a recursive function to sort an array of integers into ascending order using the following idea: the function must place the smallest element in the first position, then sort the rest of the array by a recursive call. This is a recursive version of the selection sort. (Note: You will probably want to call an auxiliary function that finds the index of the smallest item in the array. Make sure that the sorting function itself is recursive. Any auxiliary function that you use may be either recursive or iterative.) Embed your sort function in a driver program to test it. Turn in the entire program and the output.

Write a recursive function that will compute the sum of the first n integers in an array of at least n integers. Hint: Begin with the n th integer.

Code:

#include

using namespace std;

int sum(int a[], int n) { if(n==1) { return a[0]; } else { return a[n-1]+sum(a,n-1); } }

int main() { int a[10]={1,2,3,4,5,6,7,8,9,10}; int n=4; int result=sum(a,n); cout<

Code Screenshot:

#include using namespace std; int sum(int a[], int n) { if(n==1) { return a[@]; } else { return a[n-1]+sum(a,n-1);

Output:

10

Given two integers, start and end , where end is greater than start , write a recursive C++ function that returns the sum of the integers from start through end , inclusive.

Code:

#include

using namespace std;

int sum(int s, int e) { if(s==e) { return s; } else { return s+sum(s+1,e); } }

int main() { int start,end; cin>>start>>end; int result=sum(start,end); cout<

Code Screenshot:

#include using namespace std; int sum(int s, int e) { if(s=se) { return s; } else { return s+sum(s+1,e); } } int m

Output:

4 7 22

Given an integer n > 0, write a recursive C++ function that writes the integers 1, 2, . . ., n. Should be done without trail recursion.

Code:

#include

using namespace std;

void write(int c, int n) { if(c==n) { cout<

int main() { int n; cin>>n; write(1,n); return 0; }

Code Screenshot:

#include using namespace std; void write(int c, int n) { if(c==n) { cout<

Output:

5 1 2 3 4 5

Given an integer n > 0, write a recursive C++ function that returns the sum of the squares of 1 through n

Code:

#include

using namespace std;

int square(int n) { if(n==1) { return 1; } else { return n*n+square(n-1); } }

int main() { int n; cin>>n; int result=square(n); cout<

Code Screenshot:

#include

using namespace std;

int square(int n) { if(n==1) { return 1; } else { return n*n+square(n-1); } } int ma

Output:

4 30

Embed all five of these in a driver program to thoroughly test them.

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions