Tutorial 5
Data Types (p.2) and Sequence Control
Question 1:
Generate the result tables for the corresponding programs
a)
int *p, *q, n, m;
Instruction
n = 1; //1
1
m=2; //2
2
p = &m; //3
3
q = &n; //4
4
5
n--; //5
6
m++; //6
7
p = q; //7
8
q = &m; //8
b)
int *p, *q, n, m;
p = new int; //1
*p = 1; //2
q = p; //3
p = &m; //4
m = *q; //5
n = *p; //6
delete q; //7
m = *q+*p; //8
Result table:
Instruction
1
2
3
4
5
6
7
8
Question 2:
a) Draw the tree expresses evaluation order for:
i.
(a – b) / c & (d * e / a – 3)
&
/
-
*
a
b
3
/
c
-
d
a
e
*p
N/A
*q
N/A
n
1
1
0
0
m
N/A
2
2
3
0
3
3
*p
N/A
1
*q
N/A
n
N/A
m
N/A
1
N/A
1
1
1
N/A
N/A
N/A
ii.
a > b xor c or d <= 17
or
xor
>
a
<=
c
d
17
b
b) Convert above expressions to Polish prefix and postfix forms.
In Polish prefix notation:
& / - a b c - / * d e a 3
or xor > a b c <= d 17
In Polish postfix notation:
a b – c / d e * a / 3 - &
c) a b > c xor d 17 <= or
Question 3:
if (a>0) {
if (sqrt(a) > b) {
return 1;
}
}
else if (a==0) return 2;
else if (b/a > 1) return 2;
Question 4:
What is the value of x after the assignment statement in main, assuming
a. Operands are evaluated left to right.7
b. Operands are evaluated right to left.12
Question 5:
There are 2 variables subject to changes inside the loop: i, b, and a, which hold the value
of i, first and lastrespectively. So the number of cases to analyze is 23 = 8.
Case
i
b
a
Loop times
1
Infinite
2
Protected
Infinite
3
Protected
2
4
Protected
protected
2
5
Protected
Infinite
6
Protected
Protected
Infinite
7
Protected
Protected
4
8
Protected
Protected
Protected
2
Question 6:
a. Passed by value:
Init
After 1st swap
After 2nd swap
After 3rd swap
value
2
2
2
2
list
{1, 3, 5, 7, 9}
{1, 3, 5, 7, 9}
{1, 3, 5, 7, 9}
{1, 3, 5, 7, 9}
value
2
1
1
2
list
{1, 3, 5, 7, 9}
{2, 3, 5, 7, 9}
{3, 2, 5, 7, 9}
{3, 1, 5, 7, 9}
value
2
1
1
2
list
{1, 3, 5, 7, 9}
{2, 3, 5, 7, 9}
{3, 2, 5, 7, 9}
{3, 1, 5, 7, 9}
value
2
1
1
2
list
{1, 3, 5, 7, 9}
{2, 3, 5, 7, 9}
{3, 2, 5, 7, 9}
{3, 2, 1, 7, 9}
b. Passed by reference:
Init
After 1st swap
After 2nd swap
After 3rd swap
c. Passed by value-result:
Init
After 1st swap
After 2nd swap
After 3rd swap
d. Passed by name:
Init
After 1st swap
After 2nd swap
After 3rd swap