123k views
5 votes
A programmer develops the procedure makeNumsList() to generate lists of numbers starting at start and ending with the 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?

User Pixelboy
by
4.6k points

1 Answer

1 vote

Answer:

No

Step-by-step explanation:

Every time you finish a function you should test it with a wide variety of inputs, trying to cover as many logical cases as possible; if you skip this step you could end thinking that your code is correct because it performs well in one test when it is not, that is exactly the case for this code, to understand why you can test it with these inputs:

  1. What happens when you pass a no numerical input: Because the code doesn't have any input check it will crash when performing mathematical operations with strings, arrays, booleans ...
  2. What happens when you use makeNumsList(4, 7): Because on line 4 you are assigning 1 to num the result of this input will be [1,2,3,4] when it should be [4,5,6,7]. One way to solve the problem is to assign start to num
  3. What happens when 'start' is greater than 'end': You need to decide what do you want your code to do in this case
  4. What happens if you pass negative values: Again you need to decide what do you want your code to do in this case

In conclusion, you don't have control over what the user is going to input. For this reason, you should test every logical case and make changes to your code accordingly to the results.

User Robert Synoradzki
by
4.8k points