Preview

19 - Predicting Output

 1. You are presented with a pseudo-code algorithm that does not work. Can you name two ways in which you could go about trying to fix the errors?
1. Dry run = going through an algorithm with sample data, running each step manually.
2. Trace Table= a table that follows the values of variables to check for accuracy.
3. Printing = printing out the algorithm and analysing the ink very carefully
4. Pseudocoding = this is a method that involves re-writing code and comparing it
5. Running the code = executing the pseudocode using an IDE to see if it works

  None of the given options are valid ways of checking for errors/checking logic

  1 and 2 are valid ways of checking for errors and following the logic

  1 and 4 are valid ways of checking for errors and following the logic

  2 and 3 are valid ways of checking for errors and following the logic

 2. An algorithm should take two numbers as input, and then output the smallest number. There are four errors below
num1 = input("Enter the first number")
num1 = input("Enter the second number")

if num1 < num2 then
	print(num2)
elseif num2 < num1 then
	print(num2)
else
	print("Same")
endif

  Line 2 should be num2=input("Enter the second number") and line 5: print(num1)

  There are no errors at all

  Line 1 should be num0 = input("Enter the first number") and line 8: while num2<1

  Line 2 should be num2>2=input and line 5: print(num1

 3. An algorithm should ask the user to input 10 numbers, which are stored in the array. The program then outputs the total of these numbers (added together). The program then outputs the average number. Can you fill in the blanks?
count = ......
while count <= .....
    num[count] = input("Enter number")       
    .....................
endwhile
total = 0
for x = 0 to 9
	total = total + ..........
next x
average = total/10
print ("The total is" & total)
print ("The average is" & average)

  0 / 10 / count=count + 1 / num[count]

  0 / 1 / count= count + 9 / num[x]

  9 / 0 /count=0 /10

  0 / 9 / count= count + 1 / num[x]

 4. An algorithm is written to read ten numbers from the user. It adds all ten numbers together and works out the average. The average is then output. Which algorithm is correct?
ALGORITHM 1 

	total = 1
	for x = 0 to 10
		number = input("Enter a number")
		total = number
	next x
	average = total / number
	print(average)

	
ALGORITHM 2

	total = 0
	for x = 1 to 10
		number=input("Enter a number")
		total =total + number
	next x
	average=total/10
	print(average)
	
ALGORITHM 3

	total = 10
	for x = 1 to 10
		total = input("Enter a number")
		number = number + number
	next x
	average = total / 10
	print(average)

	

  ALGORITHM 3

  ALGORITHM 1

  ALGORITHM 2

  Both ALGORITHM 2 and 3 would produce the correct output

 5. An algorithm asks the user to enter 20 numbers into an array. It then asks the user to enter a number to search for. The algorithm uses a serial search to count how many times that number appears in the array. Point out two errors.
for x = 0 to 19 
	numbers[x] = input("Enter a number")
next x
	
numberSearch = input("What number are you looking for?")
quantity = 20

for count = 0 to 19
	if numbers[count] == numberSearch then
		quantity = quantity + 20
	endif
next x

print (numberSearch & " appears " & quantity & " times")

  Line 6 should be : quantity = 19 Line 10 should be: quantity = quantity + 19

  Line 10 should be: quantity = quantity + 2 Line 12 should be: next count

  Line 6 should be: quantity = 0 Line 10 should be: quantity = quantity + 1

  Line 1 should be: for x = 0 to 10 Line 8 should be: for count = 1 to 20

 6. An algorithm has been written that asks the user to enter the cost of a meal. The algorithm calculates 10% of the total for the tip. It then outputs the total cost of the meal, plus the tip. Fill in the blanks.
mealCost = input("Enter the cost of the meal")
tip = mealCost * 0.1
______________________________
print("The total cost is " & totalCost)


  totalCost = mealCost + 10% * tip

  mealCost = totalCost + tip

  totalCost = tip + 10%

  totalCost = mealCost + tip

 7. Read the below and fill in the blanks for the algorithm.
An algorithm has been written that lets the user enter their yearly salary 
and it outputs approximately how much money they would receive after tax. 
They pay no tax on the first £10600.  The next £21185 is taxed at 20%.  
The next £118215 is taxed at 40%.  
Any higher earnings than this are taxed at 45%.



salary = input("Enter your salary")
_____________ = 0
if salary > 10600 then
	moneyReceived = 10600
	salary = salary “ 10600
else
	_____________ = salary
	salary = 0
endif
if salary > 21185 then
	______________ = _____________ + (21185 * 0.8)
	salary = salary “ 21185
else
	_____________ = ______________ + (salary * 0.8)
	salary = 0
endif

  salary

  moneyReceived

  input

  40%

 8. A sat nav uses the following sub program to calculate the average speed of a car during a journey. What value will be returned from averageSpeed(110.0,2.0)?
FUNCTION averageSpeed(distance as REAL, time as REAL) as REAL
	IF distance > 0 AND time > 0 THEN
		return(distance/time)
	ELSE
		return(0.0)
	ENDIF
END FUNCTION

  22.0

  55 (whole number)

  55.0

  110

 9. Predict the output:
a = True
b = False
c = False
 
if a or b and c: #note this is asking 
#if a is true or b and c are true ....

    print "TEACHYOURSELFPYTHON"
else:
    print "teachyourselfpython"

  teachyourselfpython

  TEACHYOURSELFPYTHON

  False

  True

 10. Predict the output:
x = [1,2,3]
for i in x:
    print(i*i)

  1,4,9

  2,4,9

  1,2,3

  Error