The Student Room Group

GCSE Computer Science

ive gone through the entire cs course but still cant go through an algorithm. flowcharts are fine, but the god damn pseudocode is confusing. how am i supposed to do a trace table if i dont get what the algorithm is trying to do?
any tips?
Are you talking about reading / tracing pseudocode or writing pseudocode?

If you're trying to write a trace table from some existing pseudocode, start out by writing column(s) for each of the variables you can see, then add columns for any important expressions. If applicable, include column(s) which describe any output too.

For example, consider the following short loop:




LET arr = [ 4, 8, 15 ]
LET i = 0
LET num = 0
REPEAT WHILE i < LENGTH(arr)
BEGIN
num = arr
INCREMENT i
END




The variables are 'i' and 'num', and there's a boolean expression for 'i < LENGTH(arr)'

As far as filling in the trace table is concerned - start at the beginning of the loop and enter the initial state in each column - this will give you the first row of the table.

Then step through the loop one line at a time and look at how the values of the variables change - this is the first "pass" of the loop - by the end of the loop, the variable(s), expression(s) and output(s) will be different compared to the initial state - Fill in the next row of the trace table.

Then go back to the beginning of the loop, and repeat the process - looking at how the variables/expresions/outputs change. Fill in a row of the trace table. Keep on repeating this process until you're done.

e.g. Trace Table Initial state:




+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 | 0 | true |
+---+-----+-----------------+





Trace table after first pass:




+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 | 0 | true |
| 1 | 4 | true |
+---+-----+-----------------+





Trace table after second pass:




+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 | 0 | true |
| 1 | 4 | true |
| 2 | 8 | true |
+---+-----+-----------------+





Trace table after third pass:




+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 | 0 | true |
| 1 | 4 | true |
| 2 | 8 | true |
| 3 | 15 | false |
+---+-----+-----------------+




You don't need to understand what the algorithm is supposed to do in order to write a trace table - indeed, the whole point of a trace table is to allow you to visualise the patterns, so that you can see how it works.
(edited 5 years ago)
same i dont get trace tables
Original post by winterscoming
Are you talking about reading / tracing pseudocode or writing pseudocode?

If you're trying to write a trace table from some existing pseudocode, start out by writing column(s) for each of the variables you can see, then add columns for any important expressions. If applicable, include column(s) which describe any output too.

For example, consider the following short loop:





LET arr = [ 4, 8, 15 ]
LET i = 0
LET num = 0
REPEAT WHILE i < LENGTH(arr)
BEGIN
num = arr
INCREMENT i
END





The variables are 'i' and 'num', and there's a boolean expression for 'i < LENGTH(arr)'

As far as filling in the trace table is concerned - start at the beginning of the loop and enter the initial state in each column - this will give you the first row of the table.

Then step through the loop one line at a time and look at how the values of the variables change - this is the first "pass" of the loop - by the end of the loop, the variable(s), expression(s) and output(s) will be different compared to the initial state - Fill in the next row of the trace table.

Then go back to the beginning of the loop, and repeat the process - looking at how the variables/expresions/outputs change. Fill in a row of the trace table. Keep on repeating this process until you're done.

e.g. Trace Table Initial state:





+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 | 0 | true |
+---+-----+-----------------+






Trace table after first pass:





+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 | 0 | true |
| 1 | 4 | true |
+---+-----+-----------------+






Trace table after second pass:





+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 | 0 | true |
| 1 | 4 | true |
| 2 | 8 | true |
+---+-----+-----------------+






Trace table after third pass:





+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 | 0 | true |
| 1 | 4 | true |
| 2 | 8 | true |
| 3 | 15 | false |
+---+-----+-----------------+





You don't need to understand what the algorithm is supposed to do in order to write a trace table - indeed, the whole point of a trace table is to allow you to visualise the patterns, so that you can see how it works.


thanks dude.
a question
when you write 'While i < LENGTH(arr)'
what does that mean, i know while is a loop, but what is LENGTH(arr)?
Original post by ZainulAli1234
thanks dude.
a question
when you write 'While i < LENGTH(arr)'
what does that mean, i know while is a loop, but what is LENGTH(arr)?


I'm referring to the length of the array called 'arr' - at the top of the pseudocode there's a line:
LET arr = [ 4, 8, 15 ]
The array has got 3 items in it, so its length is 3.
Original post by winterscoming
I'm referring to the length of the array called 'arr' - at the top of the pseudocode there's a line:

LET arr = [ 4, 8, 15 ]

The array has got 3 items in it, so its length is 3.


so then im guessing the while loop only works if i is less than 3?
Original post by ZainulAli1234
so then im guessing the while loop only works if i is less than 3?


That's right. If you're using a trace table which has a column for 'i < LENGTH(arr)', then that column would start as 'true', and stay as 'true' at every pass, then once the loop is finished, it would show as 'false' in the final row of the trace table.

To put it another way, If this were a real program, then each time the loop starts, the interpreter or compiler would translate that 'while' loop into something like this:


IF i < LENGTH(arr) THEN
GOTO Next Line
ELSE
BREAK Out of Loop
END IF


In a 'while' or 'for' loop, this check is always done before executing the body (the code in the loop).
Original post by winterscoming
That's right. If you're using a trace table which has a column for 'i < LENGTH(arr)', then that column would start as 'true', and stay as 'true' at every pass, then once the loop is finished, it would show as 'false' in the final row of the trace table.

To put it another way, If this were a real program, then each time the loop starts, the interpreter or compiler would translate that 'while' loop into something like this:



IF i < LENGTH(arr) THEN
GOTO Next Line
ELSE
BREAK Out of Loop
END IF



In a 'while' or 'for' loop, this check is always done before executing the body (the code in the loop).


Thank youuu, i get it know.
thanks for the help!

Quick Reply

Latest

Trending

Trending