Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 22.3, Problem 22.3.4CP
Program Plan Intro
The following are the steps which need to followed to compute the sum of two number such as “n1” and “n2”,
Step 1:Initialize the variable “sum” as 0.
Step 2:Next use the for loop to itertae the numbers from “n1” to “n2”.
Step 3:Then sum up the values and produce the result.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
This function uses a curious mix of iteration and recursion:
function F(n)
if n < 1
t <- O
return 1
for i <- 0 to n
for j <- i to n
t<- t+j
return t + F(n-1)
The number of basic operations (additions and subtractions)
performed is:
○ O(n)
Ⓒ (n²)
(n² log n)
Ⓒ (n³)
Ө
Ⓒ (n4)
Exercise: Find the function s(n) that indicates the number of sums performed by the
following segment of an algorithm:
for i = 2 to n+1 do
for j = 1 to i+2 do
p = p + n +j
Assume that n is a positive integer.
for k:= 3 to n
for j :=1 to 6n
x := a[k] = b[j]
next j
next k
(a) Compute the actual number of elementary operations (additions, subtractions,
multiplications, divisions, and comparisons) that are performed when the algorithm segment is
executed. For simplicity, however, count only comparisons that occur within if-then
statements; ignore those implied by for-next loops.
(b) what is the order of the algorithm?
0(nb)
where b =
Chapter 22 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 22.2 - Prob. 22.2.1CPCh. 22.2 - What is the order of each of the following...Ch. 22.3 - Count the number of iterations in the following...Ch. 22.3 - How many stars are displayed in the following code...Ch. 22.3 - Prob. 22.3.3CPCh. 22.3 - Prob. 22.3.4CPCh. 22.3 - Example 7 in Section 22.3 assumes n = 2k. Revise...Ch. 22.4 - Prob. 22.4.1CPCh. 22.4 - Prob. 22.4.2CPCh. 22.4 - Prob. 22.4.3CP
Ch. 22.4 - Prob. 22.4.4CPCh. 22.4 - Prob. 22.4.5CPCh. 22.4 - Prob. 22.4.6CPCh. 22.5 - Prob. 22.5.1CPCh. 22.5 - Why is the recursive Fibonacci algorithm...Ch. 22.6 - Prob. 22.6.1CPCh. 22.7 - Prob. 22.7.1CPCh. 22.7 - Prob. 22.7.2CPCh. 22.8 - Prob. 22.8.1CPCh. 22.8 - What is the difference between divide-and-conquer...Ch. 22.8 - Prob. 22.8.3CPCh. 22.9 - Prob. 22.9.1CPCh. 22.9 - Prob. 22.9.2CPCh. 22.10 - Prob. 22.10.1CPCh. 22.10 - Prob. 22.10.2CPCh. 22.10 - Prob. 22.10.3CPCh. 22 - Program to display maximum consecutive...Ch. 22 - (Maximum increasingly ordered subsequence) Write a...Ch. 22 - (Pattern matching) Write an 0(n) time program that...Ch. 22 - (Pattern matching) Write a program that prompts...Ch. 22 - (Same-number subsequence) Write an O(n) time...Ch. 22 - (Execution time for GCD) Write a program that...Ch. 22 - (Geometry: gift-wrapping algorithm for finding a...Ch. 22 - (Geometry: Grahams algorithm for finding a convex...Ch. 22 - Prob. 22.13PECh. 22 - (Execution time for prime numbers) Write a program...Ch. 22 - (Geometry: noncrossed polygon) Write a program...Ch. 22 - (Linear search animation) Write a program that...Ch. 22 - (Binary search animation) Write a program that...Ch. 22 - (Find the smallest number) Write a method that...Ch. 22 - (Game: Sudoku) Revise Programming Exercise 22.21...Ch. 22 - (Bin packing with smallest object first) The bin...Ch. 22 - Prob. 22.27PE
Knowledge Booster
Similar questions
- The following recursive procedure computes the n-th Fibonacci number. Fib(n) 1 ifn<= 1 2 return n 3 else return Fib(n - 1) + Fib(n - 2) Use memoization technique to develop an efficient version of Fib(n). Give your solution in pseudocode, similar to what is given above.arrow_forwardDetermine the order of growth of the following snippet of code:arrow_forwardDetermine the order of growth of the following snippet of code:arrow_forward
- Suppose the runtime efficiency of an algorithm is presented by the function f(n) = 10n + 10². Which of the following statements are true? Indicate every statement that is true. A. The algorithm is O(n log n) B. The algorithm is O(n) and O(log n). C. The algorithm is O(log n) and 80(n). D. The algorithm is (n) and (log n). E. All the options above are false.arrow_forwardif n = 1 S(n) S(n//2) S(n – 1) if n is even; // is Python integer division if n is odd and n >1 Does Master Theorem apply to the recursive function S? Answer by writing either "yes" or "no" There is no need for extra explanations in this part.arrow_forwardThe performance of a binary searching method is logarithmic. O(logn)finding a particular item from a list of 1,000 items takes three seconds. How long do you think it will take to search through a list with 100,000,000 items? How did you come to your conclusion?arrow_forward
- A certain computer algorithm executes twice as many operations when it is run with an input of size k as when it is run with an input of size k - 1 (where k is an integer that is greater than 1). When the algorithm is run with an input of size 1, it executes seven operations. How many operations does it execute when it is run with an input of size 24? For each integernz 1, let s,-1 be the number of operations the algorithm executes when it is run with an input of size n. Then for each integer 2 1. Therefore, So, S3. Sz. is -Select- and s,= with constant Select- ,which is . So, for every integer n 2 0, s, = It follows that for an input of size 24, the number of operations executed by the algorithm is s -Select-v which equals Need Heln? Desdarrow_forwardPractice using recursive definitions 14. For an n digit positive integer with digits did2d3...dn-1dn, give an informal recursive algorithm to find the sum of the digits. As an example, 247 would have d₁ = 2, d2 = 4, d3 = 7 Rubric: Correct recursion Correct calculation to get digit Correct sum calculationarrow_forwardint BinDig (int n){ if (n == 1) return 1;else return (1 + BinDig(n/2)); } Find T(n) = number of arithmetic operations. Calculate the complexity of this algorithm.arrow_forward
- The n-th harmonic is the sum of the reciprocals of the first n natural numbers given by: 1 1 1 1 1 H₁₁ = Σ k=1 - =1+=+=+ 2 3 4 + + n (ii) Give a recurrence relation for the number of divisions the recursive algorithm calculates for an input n & N.arrow_forwardFind the complexity of the following blocks of code or algorithm’s description. your answer must show the steps that lead to your final answerarrow_forwardint fun1(int n) Algorithm 3 Analyze the following Algorithms. Find their running time and asymptotic { if (n <= 1) return n; return 2*fun1(n-1); notations.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education