Computer Science: An Overview (12th Edition)
12th Edition
ISBN: 9780133760064
Author: Glenn Brookshear, Dennis Brylow
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Textbook Question
Chapter 5, Problem 56CRP
- a. Identity the preconditions for the sequential search as represented in Figure 5.6. Establish a loop invariant for the while structure in that program that, when combined with the termination condition, implies that upon termination of the loop, the
algorithm will report success or failure correctly. - b. Give an argument showing that the while loop in Figure 5.6 does in fact terminate.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Check these two codes and then answer these questions please:
1) Empirically, show the performance curve of the algorithm using time measurements
2) Using the basics of the theoretical analysis, write the complexity of its worst-casetime.
1. Approach: Non recursive:
//include necessary header files#include <iostream>using namespace std;//main functionint main(){ int days,buy_on_this_day ,sell_on_this_day; //get number of days as input from user cout<<"Enter number of days: "; cin>>days; int stock_price[days]; for(int i=0;i<days;i++) { cout<<"Enter stock_price"; cin>>stock_price[i]; } int i=0; for(int i=0;i<days-1;i++) { //comparing current price with next day price and finding the minima while(i<days-1 && stock_price[i+1]<=stock_price[i]) i++;
if(i==days-1) break; buy_on_this_day =i++;
while(i<days && stock_price[i]>=…
Answer the given question with a proper explanation and step-by-step solution.
C++ 11.12 LAB: Fibonacci sequence (recursion)
The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the sum of the previous two, for example: 0, 1, 1, 2, 3, 5, 8, 13. Complete the Fibonacci() function, which takes in an index, n, and returns the nth value in the sequence. Any negative index values should return -1.
Ex: If the input is:
7
the output is:
Fibonacci(7) is 13
Note: Use recursion and DO NOT use any loops.
main.cpp
#include <iostream>using namespace std;
int Fibonacci(int n) {/* Type your code here. */ }
int main() {int startNum;cin >> startNum;cout << "Fibonacci(" << startNum << ") is " << Fibonacci(startNum) << endl;return 0;}
7. The American Museum of Natural History in New York City contains more than 32 million specimens and artifacts in its various collections, including the world’s largest collection of dinosaur fossils. Many of these are in storage away from public view, but all must be carefully inventoried.
a) Suppose the inventory is unordered (!) and a sequential search is done to locate a specific artifact. Given that the search is executed on a computer that can do 12,000 comparisons per second, about how much time on the average would the search require?
b) Assuming the inventory is sorted, about how much time would a binary search require?
Chapter 5 Solutions
Computer Science: An Overview (12th Edition)
Ch. 5.1 - Prob. 1QECh. 5.1 - Prob. 2QECh. 5.1 - Prob. 3QECh. 5.1 - Suppose the insertion sort as presented in Figure...Ch. 5.2 - A primitive in one context might turn out to be a...Ch. 5.2 - Prob. 2QECh. 5.2 - The Euclidean algorithm finds the greatest common...Ch. 5.2 - Describe a collection of primitives that are used...Ch. 5.3 - Prob. 2QECh. 5.3 - Prob. 3QE
Ch. 5.3 - Prob. 4QECh. 5.4 - Modify the sequential search function in Figure...Ch. 5.4 - Prob. 2QECh. 5.4 - Some of the popular programming languages today...Ch. 5.4 - Suppose the insertion sort as presented in Figure...Ch. 5.4 - Prob. 5QECh. 5.4 - Prob. 6QECh. 5.4 - Prob. 7QECh. 5.5 - What names are interrogated by the binary search...Ch. 5.5 - Prob. 2QECh. 5.5 - What sequence of numbers would be printed by the...Ch. 5.5 - What is the termination condition in the recursive...Ch. 5.6 - Prob. 1QECh. 5.6 - Give an example of an algorithm in each of the...Ch. 5.6 - List the classes (n2), (log2n), (n), and (n3) in...Ch. 5.6 - Prob. 4QECh. 5.6 - Prob. 5QECh. 5.6 - Prob. 6QECh. 5.6 - Prob. 7QECh. 5.6 - Suppose that both a program and the hardware that...Ch. 5 - Prob. 1CRPCh. 5 - Prob. 2CRPCh. 5 - Prob. 3CRPCh. 5 - Select a subject with which you are familiar and...Ch. 5 - Does the following program represent an algorithm...Ch. 5 - Prob. 6CRPCh. 5 - Prob. 7CRPCh. 5 - Prob. 8CRPCh. 5 - What must be done to translate a posttest loop...Ch. 5 - Design an algorithm that when given an arrangement...Ch. 5 - Prob. 11CRPCh. 5 - Design an algorithm for determining the day of the...Ch. 5 - What is the difference between a formal...Ch. 5 - Prob. 14CRPCh. 5 - Prob. 15CRPCh. 5 - The following is a multiplication problem in...Ch. 5 - Prob. 17CRPCh. 5 - Four prospectors with only one lantern must walk...Ch. 5 - Starting with a large wine glass and a small wine...Ch. 5 - Two bees, named Romeo and Juliet, live in...Ch. 5 - What letters are interrogated by the binary search...Ch. 5 - The following algorithm is designed to print the...Ch. 5 - What sequence of numbers is printed by the...Ch. 5 - Prob. 24CRPCh. 5 - What letters are interrogated by the binary search...Ch. 5 - Prob. 26CRPCh. 5 - Identity the termination condition in each of the...Ch. 5 - Identity the body of the following loop structure...Ch. 5 - Prob. 29CRPCh. 5 - Design a recursive version of the Euclidean...Ch. 5 - Prob. 31CRPCh. 5 - Identify the important constituents of the control...Ch. 5 - Identify the termination condition in the...Ch. 5 - Call the function MysteryPrint (defined below)...Ch. 5 - Prob. 35CRPCh. 5 - Prob. 36CRPCh. 5 - Prob. 37CRPCh. 5 - The factorial of 0 is defined to be 1. The...Ch. 5 - a. Suppose you must sort a list of five names, and...Ch. 5 - The puzzle called the Towers of Hanoi consists of...Ch. 5 - Prob. 41CRPCh. 5 - Develop two algorithms, one based on a loop...Ch. 5 - Design an algorithm to find the square root of a...Ch. 5 - Prob. 44CRPCh. 5 - Prob. 45CRPCh. 5 - Design an algorithm that, given a list of five or...Ch. 5 - Prob. 47CRPCh. 5 - Prob. 48CRPCh. 5 - Prob. 49CRPCh. 5 - Prob. 50CRPCh. 5 - Prob. 51CRPCh. 5 - Does the loop in the following routine terminate?...Ch. 5 - Prob. 53CRPCh. 5 - Prob. 54CRPCh. 5 - The following program segment is designed to find...Ch. 5 - a. Identity the preconditions for the sequential...Ch. 5 - Prob. 57CRPCh. 5 - Prob. 1SICh. 5 - Prob. 2SICh. 5 - Prob. 3SICh. 5 - Prob. 4SICh. 5 - Prob. 5SICh. 5 - Is it ethical to design an algorithm for...Ch. 5 - Prob. 7SICh. 5 - Prob. 8SI
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Consider the following two relations for Millennium College: STUDENT(StudentID, StudentName, CampusAddress, GPA...
Modern Database Management
Suppose the class F is defined in (a). Let f be an instance of F. Which of the statements in (b) are correct?
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
What error message do you see in the Code Pad if you type the following?
The error message is not actually v...
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Write a program that acts as a simple printing calculator. The program should allow the user to type in express...
Programming in C
Suppose that nl is of type int and n2 is of type long. What is the type of the value returned by Math.min (n1, ...
Java: An Introduction to Problem Solving and Programming (8th Edition)
Write an SQL statement to display the breed, type, and DOB of all pets having the type Dog.
Database Concepts (7th Edition)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Fill out the table for executing the polynomial-time dynamic programming algorithm for deciding whether the string 1001 is in the context-free language generated by the following CFG. Fill the table completely—do not stop the algorithm early. (Note: This CFG is not quite in Chomsky Normal Form since A appears on the right-hand side of a rule, but the same algorithm still works.) A → BC | CC B → BA | 0 C → AB | BB | 1arrow_forwardExercise 2. Give a recursive definition for the factorial operation k! n! for n ≥ 1. (remember that 1! = 0! = 1) Provide an algorithm in pseudo code to evaluate k! n! as one function Provide an algorithm in pseudo code to evaluate k! n! as three functions Evaluate the complexity of the algorithm at point 2 Evaluate the complexity of the algorithm at point 3arrow_forwardc. How many times is the basic operation executed?d. What is the efficiency class of this algorithm?e. Suggest an improvement, or a better algorithm altogether, and indicate itsefficiency class. If you cannot do it, try to prove that, in fact, it cannot bedone.arrow_forward
- 2. Name the algorithms for the searching problem that you already know. Givea good succinct description of each algorithm in English. If you know no suchalgorithms, use this opportunity to design one.arrow_forwardFor part A, how what would the pseudo-code visually be when trying to visualize an efficient algorithm? As for Part B, what would determine the space and complexity?arrow_forward1. Solve the Eight Tiled Puzzle Problem Using Hill-climbing algorithm with the help ofC++ programming language. Initial state and goal state are given below. 4 7 2 5 8 1 3 6 Initial State 1 2 3 4 5 7 8 6 Goal State Expected Outcome.a. The number of steps required to solve the puzzle.b. Print the best state after each iteration of hill-climbing requirements:no pre-defined functionsquee and algorithm library not allowedIostream only no otherarrow_forward
- 20. Suppose a computer program has been initialized such that the following sets have been stored for use in any algorithm: A = {1, 2, 3, ..., 45}B = {-7, -6, -5, ..., 27} Consider the following algorithm, which represents one part of the whole computer program (comments may occur after the # symbol on any line and are not used in computations): #Part 1: computes A - B and its cardinality AminusB = set()for element in A: # this line runs through every element in A if not(element in B): #A - B is the set of elements that are in A and are not in B AminusB.add(element) # Add to AminusB every element in A if the element is also not in B n = len(AminusB) #len() returns the number of elements in the arrayprint(n) What value is printed as a result of executing this algorithm?arrow_forwardPrecisely specify the conditions under which the following algorithm returns True, and then discuss, in detail, the running time of the algorithm. If you think it has different best- and worst-case running times then these should be considered separately, and you should explain the conditions under which best and worst-cases arise.arrow_forwardLet recursive algorithm F be defined as follows: F(int n):if n=1 then return 1else return F(n-1)*n (a) i. Assuming x is a positive integer, what mathematical expression is returned by F(x)?ii. Repeat (i) but assume the line “return 1” is replaced by “return 0”? (b) Which of the following replacements for the last line will yield a function guaranteed to terminate for all valid inputs? i. return F(n-2)+2ii. return F( )*2iii. return F( )*2arrow_forward
- Consider the following snippets of code. Then, identify and describe thesorting algorithm used.num scores[5] = 90,85,65,95,75sort()x = 1while x < SIZEtemp = scores[x]y = x – 1while y >= 0 AND scores[y]>tempscores[y+1] = scores[y]y = y – 1endwhilescores[y+1] = tempx = x + 1endwhilereturnarrow_forwardThe Pythagorean theorem states that . Write a MATLAB program in a script file that finds all the combinations of triples a, b, and c that are positive integers all smaller or equal to 30 that satisfy the Pythagorean theorem. Display the results in a three-column table in which every row element is divisible by 3. solve code in matlabarrow_forwardA word can be changed to another word by 1-letter substitution. Assume that a dictionary of 5-letter words is given. In pseudocode, design an algorithm to determine if a word "X" can be transformed to a word "Y" by a series of 1-letter substitution, and if so, outputs the corresponding sequence of words. Note that all intermediate words must be in the dictionary. As an example, the following is a sequence from “creed” to “great”: creed --> greed --> greet --> greatarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Computational Software for Intelligent System Design; Author: Cadence Design Systems;https://www.youtube.com/watch?v=dLXZ6bM--j0;License: Standard Youtube License