(d) Consider the following piece of JavaScript code: function sumOfArray(array, l, r) { if (1>r) {...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
(d) Consider the following piece of JavaScript code: function sumOfArray(array, l, r) { if (1>r) { } return 0; } else if (1 ==r) { return array[1]; } var m= Math.floor((1+r)/2): var sumL = sumofArray (l, m); var sumR = sumOfArray (m + 1, r); return sumL + sumR; function sum0fNumbers (n) { if (n == 0) { return 0; } for (var i = 1; i <= n; i++){ array.push(i); } return sum0fArray(array, 8, n-1); } 1. Briefly explain why the function sumOfArray is an example of an implementation of a divide and conquer algorithm. (3 marks) 2. What is the worst-case time complexity of the algorithm being implemented by sum0fArray, written in Big O notation and in terms of n, the length of the input array? Assume that when sumOfArray is called, the input parameters are: the array, 1 is 0 and r is n - 1. Also assume computing the floor takes constant time. (2 marks) 3. Briefly explain your answer to part 2 of question (d). (5 marks) 4. The algorithm implemented by the function sum0fNumbers calculates the sum of all non-negative integers from 0 to n (inclusive), just like RECSUM from question (b). Explain which of the two algorithms you would prefer to use to compute this sum, and why. In particular, compare the worst-case time and space complexities to make your choice. (6 marks) (d) Consider the following piece of JavaScript code: function sumOfArray (array, 1, r) { if (1>r) { return 0; } else if (1 ==r) { return array[1]; } } var m Math.floor((1+r)/2): var sumL = sumofArray (l, m); var sumR = sumOfArray (m + 1, r); return sumL + sumR; function sum0fNumbers (n) { if (n == 0) { return 0; } for (var i = 1; i <= n; i++){ array.push(i); } return sum0fArray(array, 8, n-1); } 1. Briefly explain why the function sumOfArray is an example of an implementation of a divide and conquer algorithm. (3 marks) 2. What is the worst-case time complexity of the algorithm being implemented by sum0fArray, written in Big O notation and in terms of n, the length of the input array? Assume that when sumOfArray is called, the input parameters are: the array, 1 is 0 and r is n - 1. Also assume computing the floor takes constant time. (2 marks) 3. Briefly explain your answer to part 2 of question (d). (5 marks) 4. The algorithm implemented by the function sum0fNumbers calculates the sum of all non-negative integers from 0 to n (inclusive), just like RECSUM from question (b). Explain which of the two algorithms you would prefer to use to compute this sum, and why. In particular, compare the worst-case time and space complexities to make your choice. (6 marks) (d) Consider the following piece of JavaScript code: function sumOfArray(array, l, r) { if (1>r) { } return 0; } else if (1 ==r) { return array[1]; } var m= Math.floor((1+r)/2): var sumL = sumofArray (l, m); var sumR = sumOfArray (m + 1, r); return sumL + sumR; function sum0fNumbers (n) { if (n == 0) { return 0; } for (var i = 1; i <= n; i++){ array.push(i); } return sum0fArray(array, 8, n-1); } 1. Briefly explain why the function sumOfArray is an example of an implementation of a divide and conquer algorithm. (3 marks) 2. What is the worst-case time complexity of the algorithm being implemented by sum0fArray, written in Big O notation and in terms of n, the length of the input array? Assume that when sumOfArray is called, the input parameters are: the array, 1 is 0 and r is n - 1. Also assume computing the floor takes constant time. (2 marks) 3. Briefly explain your answer to part 2 of question (d). (5 marks) 4. The algorithm implemented by the function sum0fNumbers calculates the sum of all non-negative integers from 0 to n (inclusive), just like RECSUM from question (b). Explain which of the two algorithms you would prefer to use to compute this sum, and why. In particular, compare the worst-case time and space complexities to make your choice. (6 marks) (d) Consider the following piece of JavaScript code: function sumOfArray (array, 1, r) { if (1>r) { return 0; } else if (1 ==r) { return array[1]; } } var m Math.floor((1+r)/2): var sumL = sumofArray (l, m); var sumR = sumOfArray (m + 1, r); return sumL + sumR; function sum0fNumbers (n) { if (n == 0) { return 0; } for (var i = 1; i <= n; i++){ array.push(i); } return sum0fArray(array, 8, n-1); } 1. Briefly explain why the function sumOfArray is an example of an implementation of a divide and conquer algorithm. (3 marks) 2. What is the worst-case time complexity of the algorithm being implemented by sum0fArray, written in Big O notation and in terms of n, the length of the input array? Assume that when sumOfArray is called, the input parameters are: the array, 1 is 0 and r is n - 1. Also assume computing the floor takes constant time. (2 marks) 3. Briefly explain your answer to part 2 of question (d). (5 marks) 4. The algorithm implemented by the function sum0fNumbers calculates the sum of all non-negative integers from 0 to n (inclusive), just like RECSUM from question (b). Explain which of the two algorithms you would prefer to use to compute this sum, and why. In particular, compare the worst-case time and space complexities to make your choice. (6 marks)
Expert Answer:
Answer rating: 100% (QA)
1 sumOfArray function uses the divide and conquer algorithm because here we are basically dividing t... View the full answer
Related Book For
Computer Organization and Design The Hardware Software Interface
ISBN: 978-0124077263
5th edition
Authors: David A. Patterson, John L. Hennessy
Posted Date:
Students also viewed these programming questions
-
A researcher selects a sample of n=25 individuals from a population with a mean of u=60 and standard deviation of o=10 and administers a treatment. The researcher predicts that the treatment will...
-
Consider the decision tree for a company deciding whether to continue product development as shown below. If the company achieves technical success on its own, what is the expected monetary value of...
-
Using the cyclic relation and the first Maxwell relation, derive the other three Maxwell relations.
-
A cookbook has a 34% markup rate based on cost. If the markup is $5.27, find the cost of the cookbook. Find the selling price.
-
Mrs. Clarks Foods was an Iowa company engaged in the business of distributing juice beverages. International Suntrade and Miller & Smith Foods were Canadian companies that acted as brokers...
-
The December cash records of Dunlap Insurance follow: Dunlap's Cash account shows a balance of $16,740 at December 31. On December 31, Dunlap Insurance received the following bank statement:...
-
Define a set X of strings in the symbols 0 and 1 as follows. B. 0 and 1 are in X. R. If x and y are in X, so is xxyy. R2. If x and y are in X, so is xyx. Explain why the string 01001011 EX using the...
-
Consider two long put options with strikes E1 and E2 that satisfy E1 E2 and same expiry date T Formulate the conditions on their premiums P1 and P2 so that the PNL graphs of the two puts intersect...
-
Discuss the approaches to corporate governance and audit assurance in China.
-
What are the main features of the corporate reporting system in China?
-
One July, the United States sold aircra worth $1 billion to China and bought aircra worth only $19,000 from China. During the same month, however, the United States bought $83 million worth of mens...
-
Fill in the blanks: The __________ annotation determines whether annotations are retained in generated Javadoc, while the __________ annotation determines the location an annotation can be applied....
-
Discuss the view that the distinctive characteristics of business structures and institutions create the case for distinctively different accounting law and practice in China.
-
2. How many key regulatory requirements in operational risk? Select the correct response: A. Four B. Five O C. Six D. Seven
-
What types of questions can be answered by analyzing financial statements?
-
Calculate the time necessary to perform a multiply using the approach described in the text (31 adders stacked vertically) if an integer is 8 bits wide and an adder takes 4 time units.
-
As described in Section 5.7, virtual memory uses a page table to track the mapping of virtual addresses to physical addresses. This exercise shows how this table must be updated as addresses are...
-
Rewrite the equations on page B-44 for a carry-lookahead logic for a 16-bit adder using a new notation. First, use the names for the CarryIn signals of the individual bits of the adder. That is, use...
-
Fine Furniture Ltd had a balance in its Work in Process Inventory account on 1 October 2019 of $13 600 made up of two jobs: Job 730, $6400; and Job 731, $7200. At the end of October, costs assigned...
-
Refer to the latest financial report of JB Hi-Fi Limited on its website, www.jbhifi.com.au, and answer the following questions. 1. Is it likely that JB Hi-Fi Limited would have to confront such...
-
Imelda Instruments Ltd manufactures two products: missile range instruments and space pressure gauges. During January, 53 range instruments and 360 pressure gauges were produced, and overhead costs...
Study smarter with the SolutionInn App