The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 2 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I need help with my homework, here are the questions below:
Â
1. Consider the following iterative function:
Â
int pentagonal(int n) {
Â
int result = 0;
Â
for (int i = 1; i <= n; i++)
Â
result += 3 * i - 2;
Â
return result;
Â
}
Rewrite the function pentagonal 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) = 10n 2 + 6
Algorithm 2: g(n) = 21n + 7
Â
Determine at what point algorithm 2 becomes more efficient than algorithm 1.
Â
3. Given the following function that evaluates a polynomial whose coefficients are stored in an array:
Â
double evaluate(double[] coefficients, double x) {
double result = coefficients[0];
Â
double power = 1;
Â
for (int i = 1; i < array.length; i++)
Â
{ power = power * x;
Â
result = result + coefficients[i] * power;
Â
}
Â
return result;
Â
}
Â
Let n be the length of the array. Determine the number of additions and multiplications that are performed in the worst case as a function of n.
Â
4. Given the following recursive binary search algorithm for finding an element in a sorted array of integers:
Â
int recursiveBinarySearch(int[] array, int target, int left, int right) {
Â
if (left > right) return -1; int middle = (left + right) / 2;
Â
if (array[middle] == target) return middle;
Â
if (array[middle] > target) return recursiveBinarySearch(array, target, left, middle - 1);
Â
return recursiveBinarySearch(array, target, middle + 1, right);
Â
}
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly