Verifying an algorithm

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

The two algorithms below are both intended to calculate the product of squares from 1 to n, where n is any positive integer. For example, if n is 333, the algorithms should calculate a product of 363636, from 1^2 \times 2^2 \times 3^21 2 ×2 2 ×3 2 1, squared, times, 2, squared, times, 3, squared. Algorithm 1: i ← n product ← 0 REPEAT n TIMES { product ← product * (i * i) i ← i - 1 } Algorithm 2: i ← 0 product ← 1 REPEAT n TIMES { product ← product * (i * i) i ← i + 1 } Which statement best describes the behavior of the algorithms?

Neither algorithm calculates the correct product.

An algorithm will be used to count how many numbers in a list are multiples of either 5 or 10. For the list of [5, 3, 2, 10, 20, 7], it should count 3 numbers (5, 10, 20). There are two proposals for the algorithm: Algorithm 1: Set value of count to 0. Iterate through each number in the list. If the current number is a multiple of 5 and a multiple of 10, increment count by 1. Algorithm 2: Set value of count to 0. Iterate through each number in the list. If the current number is a multiple of 5, increment count by 1. Then, if the current number is a multiple of 10, increment count by 1. Which of the following best describes the behavior of these algorithms?

Neither algorithms will output the correct count on all inputs, due to issues with under-counting or over-counting multiples.

A programmer develops the procedure makeNumsList() to generate lists of numbers starting at start and ending with end. For example, makeNumsList(4, 7) should return [4, 5, 6, 7]. PROCEDURE makeNumsList(start, end) { numsList ← [] numTimes ← (end - start) + 1 num ← 1 REPEAT numTimes TIMES { APPEND(numsList, num) num ← num + 1 } RETURN numsList } The programmer tests it with makeNumsList(1, 4) and sees a return value of [1, 2, 3, 4]. Can the programmer conclude that the procedure works correctly for all inputs? Choose 1 answer:

No, they've only verified that it works for that test case. Additional analysis and reasoning is necessary.

The goal of the procedure reportDisallowed is to iterate through a list of strings and report how many of them contain disallowed characters ("!" and "&"). For example, if given the list ["ahoy", "mateys!", "fish&chips!"], it should return the number 2 since two of the three strings contain disallowed characters. PROCEDURE reportDisallowed(strings) { numWithDisallowed ← 0 FOR str IN strings { IF (CONTAINS(str, "!") OR CONTAINS(str, "&")) { numWithDisallowed ← numWithDisallowed + 1 RETURN numWithDisallowed } } RETURN numWithDisallowed } A programmer tests the procedure with various inputs and finds multiple cases where it does not produce the expected output. Which calls to reportDisallowed() return an incorrect number? 👁️Note that there are 2 answers to this question. Choose 2 answers:

reportDisallowed(["c@lculus", "prob&stats", "factorials!"]) reportDisallowed(["computer-sc!ence", "html&css", "javascr1pt"])


Set pelajaran terkait

Chapter 21,22 and 24 Test Review

View Set

Life Insurance License Study Set (FULL) Indiana

View Set

ULL FNAN 320 Justin Callais Exam 2

View Set

ENSP Test 3 Review Clemson: Chapter 14

View Set