Create a trace table to determine the values of the variables in each step of the following program when a value of 3 is entered.
PHP
<?php
$a = trim(fgets(STDIN));
$b = $a + 10;
$a = $b * ($a - 3);
$c = 3 * $b / 6;
$d = $c * $c;
$d--;
echo $d;
?>
Solution
For the input value of 3, the trace table looks like this.
Step | Statement | Notes | $a | $b | $c | $d |
1 | $a = trim(fgets(STDIN)) | User enters value 3 | 3 | ? | ? | ? |
2 | $b = a + 10 | 3 | 13 | ? | ? | |
3 | $a = $b * ($a - 3) | 0 | 13 | ? | ? | |
4 | $c = 3 * $b / 6 | 0 | 13 | 6.5 | ? | |
5 | $d = $c * $c | 0 | 13 | 6.5 | 42.25 | |
6 | $d-- | 0 | 13 | 6.5 | 41.25 | |
7 | echo $d | The value 41.25 is displayed |
Java
public static void main(String[] args) throws java.io.IOException {
java.io.BufferedReader cin = new java.io.
BufferedReader(new java.io.InputStreamReader(System.in));
double a, b, c, d;
a = Double.parseDouble(cin.readLine());
b = a + 10;
a = b * (a - 3);
c = 3 * b / 6;
d = c * c;
d--;
System.out.println(d);
}
Solution
For the input value of 3, the trace table looks like this.
Step | Statement | Notes | a | b | c | d |
1 | a = Double.parseDouble(cin.readLine()) | User enters value 3 | 3 | ? | ? | ? |
2 | b = a + 10 | 3 | 13 | ? | ? | |
3 | a = b * (a - 3) | 0 | 13 | ? | ? | |
4 | c = 3 * b / 6 | 0 | 13 | 6.5 | ? | |
5 | d = c * c | 0 | 13 | 6.5 | 42.25 | |
6 | d-- | 0 | 13 | 6.5 | 41.25 | |
7 | System.out.println(d) | The value 41.25 is displayed |
C++
#include <iostream>
using namespace std;
int main() {
double a, b, c, d;
cin >> a;
b = a + 10;
a = b * (a - 3);
c = 3 * b / 6;
d = c * c;
d--;
cout << d;
return 0;
}
Solution
For the input value of 3, the trace table looks like this.
Step | Statement | Notes | a | b | c | d |
1 | cin >> a | User enters value 3 | 3 | ? | ? | ? |
2 | b = a + 10 | 3 | 13 | ? | ? | |
3 | a = b * (a - 3) | 0 | 13 | ? | ? | |
4 | c = 3 * b / 6 | 0 | 13 | 6.5 | ? | |
5 | d = c * c | 0 | 13 | 6.5 | 42.25 | |
6 | d-- | 0 | 13 | 6.5 | 41.25 | |
7 | cout << d | The value 41.25 is displayed |
C#
static void Main() {
double a, b, c, d;
a = Double.Parse(Console.ReadLine());
b = a + 10;
a = b * (a - 3);
c = 3 * b / 6;
d = c * c;
d--;
Console.Write(d);
Console.ReadKey();
}
Solution
For the input value of 3, the trace table looks like this.
Step | Statement | Notes | a | b | c | d |
1 | a = Double.Parse(Console.ReadLine()) | User enters value 3 | 3 | ? | ? | ? |
2 | b = a + 10 | 3 | 13 | ? | ? | |
3 | a = b * (a - 3) | 0 | 13 | ? | ? | |
4 | c = 3 * b / 6 | 0 | 13 | 6.5 | ? | |
5 | d = c * c | 0 | 13 | 6.5 | 42.25 | |
6 | d-- | 0 | 13 | 6.5 | 41.25 | |
7 | Console.Write(d) | The value 41.25 is displayed |
Visual Basic
Sub Main()
Dim a, b, c, d As Double
a = Console.ReadLine()
b = a + 10
a = b * (a - 3)
c = 3 * b / 6
d = c * c
d -= 1
Console.Write(d)
Console.ReadKey()
End Sub
Solution
For the input value of 3, the trace table looks like this.
Step | Statement | Notes | a | b | c | d |
1 | a = Console.ReadLine() | User enters value 3 | 3 | ? | ? | ? |
2 | b = a + 10 | 3 | 13 | ? | ? | |
3 | a = b * (a - 3) | 0 | 13 | ? | ? | |
4 | c = 3 * b / 6 | 0 | 13 | 6.5 | ? | |
5 | d = c * c | 0 | 13 | 6.5 | 42.25 | |
6 | d -= 1 | 0 | 13 | 6.5 | 41.25 | |
7 | Console.Write(d) | The value 41.25 is displayed |
Python
a = float(input())
b = a + 10
a = b * (a - 3)
c = 3 * b / 6
d = c * c
d -= 1
print(d)
Solution
For the input value of 3, the trace table looks like this.
Step | Statement | Notes | a | b | c | d |
1 | a = float(input()) | User enters value 3 | 3 | ? | ? | ? |
2 | b = a + 10 | 3 | 13 | ? | ? | |
3 | a = b * (a - 3) | 0 | 13 | ? | ? | |
4 | c = 3 * b / 6 | 0 | 13 | 6.5 | ? | |
5 | d = c * c | 0 | 13 | 6.5 | 42.25 | |
6 | d -= 1 | 0 | 13 | 6.5 | 41.25 | |
7 | print(d) | The value 41.25 is displayed |