Cash-back offer from April 23rd to 27th, 2024: Get a flat 10% cash-back credited to your account for a minimum transaction of $50.Post Your Questions Today!

Question DetailsNormal
$ 15.00

Recursion and add preconditions and postconditions

Question posted by
Online Tutor Profile
request

1.     Consider the following iterative function:

int square(int n) {   int result = 0;   for (int i = 1; i <= n; i++)     result += 2 * i - 1;   return result; }

Rewrite the function square using recursion and add preconditions and postconditions as comments. Then prove by induction that the recursive function you wrote is correct.

 

2.     Suppose the number of steps required in the worst case for two algorithms are as follows: 

 

•        Algorithm 1: f(n) = 3n2 + 9

•        Algorithm 2: g(n) = 51n + 17

Determine at what integer value of n, algorithm 2 becomes more efficient than algorithm 1.

3.     Given the following function that sorts an array of values:

void bubbleSort(double[] array) {   for (int i = 0; i < array.length; i++)     for (int j = array.length - 1; j > i; j--)       if (array[j] < array[j - 1])         swap(array, j, j - 1); }

Let n be the length of the array. Using summation evaluation, determine the number of swaps that are performed in the worst case as a function of n

4.     Given the following recursive function and it corresponding helper function that returns the sum of all the elements of an array that are located at even subscripts:

int sumEvenElements(int array[], int i) {   if (i >= array.length)     return 0;

  return  array[i] + sumEvenElements(array, i+2);

int sumEvenElements(int array[]) {     return sumEvenElments(array, 0) }

Assume n is the length of the array. Find the initial condition and recurrence equation that expresses the execution time for the worst case of the recursive function and then solve that recurrence.

 

Available Answer
$ 15.00

[Solved] recursion and add preconditions and postconditions

  • This solution is not purchased yet.
  • Submitted On 19 Mar, 2023 06:53:48
Answer posted by
Online Tutor Profile
solution
1. Consider the following iterative function: int square(int n) { int result = 0; for (int i = 1; i <= n; i++) result += 2 * i - 1; return result; } Rewrite the function square using recursion and add preconditions and postconditions as comments. Then prove by induction that the recursive function you wrote is correct. 2. Suppose the number of steps required in the worst case f...
Buy now to view the complete solution
Other Similar Questions
User Profile
Acade...

recursion and add preconditions and postconditions

1. Consider the following iterative function: int square(int n) { int result = 0; for (int i = 1; i <= n; i++) result += 2 * i - 1; return result; } Rewrite the function square using recursion and add preconditi...
User Profile
Homew...

CSC 245 Recursion Lab | Complete Solution

/** * 2.b function * * @param n * @return */ public static String triangle(int n) { if (n == 0) { return " "; } triangle(n - 1); for (int i = 0; i < n; i++) { System.out.print("*"); } System.ou...
User Profile
Exper...

CS3330 Problem Set 1: Recursion and Complexity Analysis | Complete Solution

This Tutorial is rated A+ previously,if you have any questions regarding this tutorial then you can contact me....

The benefits of buying study notes from CourseMerits

homeworkhelptime
Assurance Of Timely Delivery
We value your patience, and to ensure you always receive your homework help within the promised time, our dedicated team of tutors begins their work as soon as the request arrives.
tutoring
Best Price In The Market
All the services that are available on our page cost only a nominal amount of money. In fact, the prices are lower than the industry standards. You can always expect value for money from us.
tutorsupport
Uninterrupted 24/7 Support
Our customer support wing remains online 24x7 to provide you seamless assistance. Also, when you post a query or a request here, you can expect an immediate response from our side.
closebutton

$ 629.35