ZainulAli1234
Badges: 10
Rep:
?
#1
Report Thread starter 1 year ago
#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
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 1 year ago
#2
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:
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
reply
cutiecorn
Badges: 11
Rep:
?
#3
Report 1 year ago
#3
same i dont get trace tables
0
reply
ZainulAli1234
Badges: 10
Rep:
?
#4
Report Thread starter 1 year ago
#4
(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:
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
reply
winterscoming
Badges: 19
Rep:
?
#5
Report 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
reply
ZainulAli1234
Badges: 10
Rep:
?
#6
Report Thread starter 1 year ago
#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
reply
winterscoming
Badges: 19
Rep:
?
#7
Report 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
reply
ZainulAli1234
Badges: 10
Rep:
?
#8
Report Thread starter 1 year ago
#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
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

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

Personalise

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
    PGCE Open day Postgraduate
    Sat, 29 Feb '20

People at uni: do initiations (like heavy drinking) put you off joining sports societies?

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

Watched Threads

View All