Preconditions and postconditions

Assignment Help Basic Computer Science
Reference no: EM131626742

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);

}

Reference no: EM131626742

Questions Cloud

Define what would the manufacturer have to prove : The manufacturer of garden furniture markets its product under the well-known brand name of Lazeabout
Rarely is a one size fits all solution possible : ?Rarely is a "one size fits all" solution possible. As media types evolve (think cables vs. fiber vs. wireless), companies must understand the capabilities.
Incident response plan for home computer : Design an incident response plan for your home computer. Include actions to be taken if each of the following events occur:
Post a description of your vision for social change : Describe a social change effort that is currently going on in your community or an effort you think needs to take place in your community.
Preconditions and postconditions : Rewrite the function pentagonal using recursion and add preconditions and postconditions as comments.
Discuss consequences should directors contravene the law : Explain the consequences should directors contravene the law and the remedies available.
Consider how you might define social science research : Consider how you might define social science research and what it means to you as a human and social services professional.
What is the hexadecimal equivalent : What is the hexadecimal equivalent for the following decimal number: 93?
What are the interests of the stakeholders : Who are the stakeholders in this case, What are the interests of the stakeholders

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Identifies the cost of computer

identifies the cost of computer components to configure a computer system (including all peripheral devices where needed) for use in one of the following four situations:

  Input devices

Compare how the gestures data is generated and represented for interpretation in each of the following input devices. In your comparison, consider the data formats (radio waves, electrical signal, sound, etc.), device drivers, operating systems suppo..

  Cores on computer systems

Assignment : Cores on Computer Systems:  Differentiate between multiprocessor systems and many-core systems in terms of power efficiency, cost benefit analysis, instructions processing efficiency, and packaging form factors.

  Prepare an annual budget in an excel spreadsheet

Prepare working solutions in Excel that will manage the annual budget

  Write a research paper in relation to a software design

Research paper in relation to a Software Design related topic

  Describe the forest, domain, ou, and trust configuration

Describe the forest, domain, OU, and trust configuration for Bluesky. Include a chart or diagram of the current configuration. Currently Bluesky has a single domain and default OU structure.

  Construct a truth table for the boolean expression

Construct a truth table for the Boolean expressions ABC + A'B'C' ABC + AB'C' + A'B'C' A(BC' + B'C)

  Evaluate the cost of materials

Evaluate the cost of materials

  The marie simulator

Depending on how comfortable you are with using the MARIE simulator after reading

  What is the main advantage of using master pages

What is the main advantage of using master pages. Explain the purpose and advantage of using styles.

  Describe the three fundamental models of distributed systems

Explain the two approaches to packet delivery by the network layer in Distributed Systems. Describe the three fundamental models of Distributed Systems

  Distinguish between caching and buffering

Distinguish between caching and buffering The failure model defines the ways in which failure may occur in order to provide an understanding of the effects of failure. Give one type of failure with a brief description of the failure

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd