CS1010E: Programming Methodology: Tutorial 01: Identifiers, Types, and Operations 23 Jan 2017 - 27 Jan 2017
CS1010E: Programming Methodology: Tutorial 01: Identifiers, Types, and Operations 23 Jan 2017 - 27 Jan 2017
1. Discussion Questions
(a) [Identifiers] Which of the following identifier(s) is/are valid?
(b) [Types] Given the following program fragment, what are the values and types of ALL the declared
variables ? (note: give real number up to 6 decimal places)
i. int i = 10; i.
ii. int j = 12.3; ii.
iii. int i = 123, j = 456.789; iii.
iv. double f = 1010; iv.
(c) [Operation Precedence] What is the final value of ans in the code fragment below?
i. int a = 1, b = 2, c = 3, d = 4;
int ans = a + b * c + d; i.
ii. int a = 1, b = 2, c = 3, d = 4;
int ans = a + b - c + d; ii.
iii. int a = 1, b = 2, ans = a++ + b++;
ans = ans + a + b; iii.
iv. int a = 1, b = 2, ans = ++a + ++b;
ans = ans + a + b; iv.
2. Program Analysis
(a) [Division and Modulo] What is the final value of ans in the code fragment below? (note: give real
number up to 3 decimal places)
i. int a = 5, b = 2;
double ans = a / b * 1.0; i.
ii. int a = 5, b = 2;
double ans = a * 1.0 / b; ii.
iii. int a = 5, b = 2;
int ans = a * 1.0 / b; iii.
1
CS1010E Tutorial 01: Identifiers, Types, and Operations 23 Jan 2017 - 27 Jan 2017
iv. int a = 5, b = 2;
int ans = a / b * 1.0; iv.
v. int matric = 0040607,
ans = matric%10
+ (matric/100)%10
+ (matric/10000)%10; v.
(b) [Limit of Values] What is the final value of ans in the code fragment below?
i. short a = 32767, ans = a + 1; i.
ii. unsigned short b = 65535, ans = b + 1; ii.
iii. double c = 9007199254740992,
ans = c + 1; iii.
3. Designing a Solution
(a) [Computation] Consider a bank account with an initial value given in a variable balance and the
annual interest rate of the bank (as a percent value) given in a variable rate. Assuming that the the
calculation of the annual compounded interest for 3 years can be exemplified by the calculation below
involving the initial balance of 1500 and the annual interest rate of 2.5%. The calculations below are
rounded to 2 decimal places.
0-th year balance = $1500.00 (initial balance)
1-st year interest = $1500.00 × 0.025 = $37.50
1-st year balance = $1500.00 + 37.50 = $1537.50
2-nd year interest = $1503.75 × 0.025 = $38.44
2-nd year balance = $1503.75 + 38.44 = $1575.94
3-rd year interest = $1507.51 × 0.025 = $39.40
3-rd year balance = $1507.51 + 39.40 = $1615.34
Write a program to compute the balance at the end of the 3rd year given any positive value of balance
and rate. Write your program below:
int main() {
double balance, rate;
/* Calculate Final Balance Here */
printf("%.2f\n", balance);
return 0;
}
(b) [Computation] We simplify the question regarding bank account above to compute not the amount
after compounded interest but the amount after simple interest. The calculations are now changed to
be:
Page 2
CS1010E Tutorial 01: Identifiers, Types, and Operations 23 Jan 2017 - 27 Jan 2017
printf("%.2f\n", balance);
return 0;
}
4. Challenge
(a) [Design] Lecture Notes 1 includes the algorithm and code for the greatest common divisor (GCD) of
two integer. Using the 4-steps methodology described in the Lecture Notes 1, solve the problem of
finding the least common multiple (LCM).
Page 3
CS1010E Tutorial 01: Identifiers, Types, and Operations 23 Jan 2017 - 27 Jan 2017
Page 4