1.5 More Examples


With the increase of petrol prices, your friend Ahmad wants a program that will calculate how much km per liter of petrol was consumed. For example, when starting a journey, Ahmad will fill up his car’s tank to full and reset his car’s trip meter to zero. After travelling 378 miles, Ahmad stopped at a petrol station to refuel. The car required 25 liters to fill up the tank. The program should calculate the number of km the car can be driven per liter.

Algorithm




1.5 More Examples


Pseudocode

Desk-checking

First choose a set of input values. Then you must compute or calculate the steps manually to get the expected outputs for the set of inputs. For example, in the problem given above, you may choose the following set of input:




1.5 More Examples


For each of the input set, you will then manually calculate the expected outcome. Therefore, the outcome for the input sets are as follows :


Let us check the pseudocode version.




1.5 More Examples


First, create a table that contains a column for each input, output. For the program given above, you will need a table as shown in Table 6. The first instruction in the algorithm is for the user to enter the value of km driven, and the liter of petrol used. Place these values in the corresponding columns as shown in the following table.


The next instruction is to calculate the km per liter value based on the formula given, which is to divide the value of km driven by the value of petrol used. Using the table, you will get the values that are to be calculated. So, if for example, the km driven column or the petrol used column in the table is empty, then you know that your algorithm has missed a step. Now, since you have the values in the respective columns, use those values to calculate the km per liter value. 324 divided by 35 = 9.25, and place that value in the km per liter column as shown in Table 7 below.


1.5 More Examples



The last instruction is to display the km per liter value on the computer screen. In this case, the value 9.25 will be displayed, because that is the value that appears in the table’s RM column. If this value is consistent with the manual calculation you performed earlier, prior to desk-checking the algorithm, then the algorithm appears to be correct. You should however, perform more desk checking using different sets of input values.

For practice, you must now try to conduct desk checking using the second set of values given earlier.