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 va
Trang 1Tutorial 5 Data Types (p.2) and Sequence Control
Question 1:
Generate the result tables for the corresponding programs
a)
int *p, *q, n, m;
n = 1; //1
m=2; //2
p = &m; //3
q = &n; //4
n ; //5
m++; //6
p = q; //7
q = &m; //8
Instruction *p *q n m
1
2
3
4
5
6
7
8
N/A
2
3
0
N/A
1
0
3
1
0
N/A
2
3
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 *p *q n m
1
2
3
4
5
6
7
8
N/A
1
N/A
1 N/A
N/A
1
N/A
N/A
1
N/A
1 N/A
Question 2:
a) Draw the tree expresses evaluation order for:
i (a – b) / c & (d * e / a – 3)
/
&
d e
*
/
a
-
3
Trang 2ii a > b xor c or d <= 17
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
6 Protected Protected Infinite
8 Protected Protected Protected 2
>
c
xor
or
d
<=
17
Trang 3Question 6:
a Passed by value:
After 1st swap 2 {1, 3, 5, 7, 9} After 2nd swap 2 {1, 3, 5, 7, 9} After 3rd swap 2 {1, 3, 5, 7, 9}
b Passed by reference:
After 1st swap 1 { , 3, 5, 7, 9} After 2nd swap 1 { , 2, 5, 7, 9} After 3rd swap 2 {3, 1, 5, 7, 9}
c Passed by value-result:
After 1st swap 1 { , 3, 5, 7, 9} After 2nd swap 1 { , 2, 5, 7, 9} After 3rd swap 2 {3, 1, 5, 7, 9}
d Passed by name:
After 1st swap 1 { , 3, 5, 7, 9} After 2nd swap 1 { , 2, 5, 7, 9} After 3rd swap 2 {3, 2, 1, 7, 9}