Trace Tablesx

Download Report

Transcript Trace Tablesx

Trace Tables
In the exam…
The exam will normally ask you to create a trace table of some sort so
you need to be very confident with them.
The exam will usually give you the headings but just in case, there are
several steps in making a trace table, the first one is to note the table
headings, this involves the following:
• VARIABLES: note all the variables in the piece of code you are
looking at (this includes arrays). Note each variable as a heading
• OUTPUTS: note if there is an output and put this as a heading
• INPUTS: if there are inputs specified, put an inputs column and be
prepared to fill it in.
An Example
Dim y as integer = 3
For x = 1 to 4
y=y+x
Next
Console.writeline(y)
x
y
1
3
2
4
3
6
4
9
4
13
output
13
Averages Program
Dim num As Integer = 0
Dim sum As Integer = 0
Dim avg As Decimal = 0.0
For i = 0 To 2
Console.WriteLine("Enter Number " & i + 1)
num = Console.ReadLine()
sum = sum + num
avg = sum / (i + 1)
Next
Console.WriteLine("sum =" & sum)
Console.WriteLine("average =" & avg)
Questions
1. Complete a trace table for the following input:
–
11, 22, 33 (NOTE: The first line has been completed for you)
2. Can you see any way to make the program more efficient?
–
Write up the code on paper and test your change by creating another trace
table for the edited program.
3. Can you edit the program so that the user can choose how many numbers
are used (i.e. the user is asked how many numbers they will be entering) ?
–
–
–
Write up the code on paper and test your theory by creating another trace
table for the edited program. Use the input 3 for amount of numbers and
11, 22 and 33 as the number values.
Write up your successful solution in VB.NET
Print out your annotated code.
Trace table for original program
num
sum
avg
i
output
0
0
0.0
0
Enter Number 1
Input: 11, 22, 33
Trace table for question 2
num
sum
avg
i
output
0
0
0.0
0
Enter Number 1
Input: 11, 22, 33
Trace table for question 3
num
sum
avg
i
howMany
output
0
0
0.0
0
0
How Many
Numbers?
Input: 3,11, 22, 33