# GCSE Computer ScienceWatch

Announcements
#1
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?
0
1 year ago
#2

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:
Code:
```LET arr = [ 4, 8, 15 ]
LET i = 0
LET num = 0
REPEAT WHILE i < LENGTH(arr)
BEGIN
num = arr[i]
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:
Code:
```+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 |  0  |      true       |
+---+-----+-----------------+```
Trace table after first pass:
Code:
```+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 |  0  |       true      |
| 1 |  4  |       true      |
+---+-----+-----------------+```
Trace table after second pass:
Code:
```+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 |  0  |      true       |
| 1 |  4  |      true       |
| 2 |  8  |      true       |
+---+-----+-----------------+```
Trace table after third pass:
Code:
```+---+-----+-----------------+
| 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.
0
1 year ago
#3
same i dont get trace tables
0
#4
(Original post by winterscoming)

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:
Code:
```LET arr = [ 4, 8, 15 ]
LET i = 0
LET num = 0
REPEAT WHILE i < LENGTH(arr)
BEGIN
num = arr[i]
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:
Code:
```+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 |  0  |      true       |
+---+-----+-----------------+```
Trace table after first pass:
Code:
```+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 |  0  |       true      |
| 1 |  4  |       true      |
+---+-----+-----------------+```
Trace table after second pass:
Code:
```+---+-----+-----------------+
| i | num | i < LENGTH(arr) |
+---+-----+-----------------+
| 0 |  0  |      true       |
| 1 |  4  |      true       |
| 2 |  8  |      true       |
+---+-----+-----------------+```
Trace table after third pass:
Code:
```+---+-----+-----------------+
| 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)?
0
1 year ago
#5
(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:
Code:
`LET arr = [ 4, 8, 15 ]`
The array has got 3 items in it, so its length is 3.
0
#6
(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:
Code:
`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?
0
1 year ago
#7
(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:
Code:
```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).
0
#8
(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:
Code:
```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!
0
X

new posts Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### University open days

• Sheffield Hallam University
Get into Teaching in South Yorkshire Undergraduate
Wed, 26 Feb '20
• The University of Law
Solicitor Series: Assessing Trainee Skills – LPC, GDL and MA Law - London Moorgate campus Postgraduate
Wed, 26 Feb '20
• University of East Anglia
Sat, 29 Feb '20

### Poll

Join the discussion

Yes (433)
67.45%
No (209)
32.55%