0% found this document useful (0 votes)
47 views4 pages

CS1010E: Programming Methodology: Tutorial 01: Identifiers, Types, and Operations 23 Jan 2017 - 27 Jan 2017

This document provides an overview of identifiers, types, operations, and some programming challenges for a programming methodology tutorial. It includes: 1. Discussion questions on valid identifiers, variable types and values, and operation precedence. 2. Program analysis questions on division/modulo and value limits. 3. Designing solutions to calculate bank account balances over time with compound or simple interest. 4. A challenge to find the least common multiple of two integers following the methodology used for greatest common divisor algorithms.

Uploaded by

Kyle Sangma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views4 pages

CS1010E: Programming Methodology: Tutorial 01: Identifiers, Types, and Operations 23 Jan 2017 - 27 Jan 2017

This document provides an overview of identifiers, types, operations, and some programming challenges for a programming methodology tutorial. It includes: 1. Discussion questions on valid identifiers, variable types and values, and operation precedence. 2. Program analysis questions on division/modulo and value limits. 3. Designing solutions to calculate bank account balances over time with compound or simple interest. 4. A challenge to find the least common multiple of two integers following the methodology used for greatest common divisor algorithms.

Uploaded by

Kyle Sangma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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?

A. A_Long_Identifier E. year#2 I. valid identifier


B. module code F. 1010E J. _invalid
C. SG$ G. CS1010E K. x
D. int_val H. $USD L. _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

0-th year balance = $1500.00 (initial balance)


interest = $1500.00 × 0.025 = $37.50
1-st year balance = $1500.00 + 37.50 = $1537.50
2-nd year balance = $1537.50 + 37.50 = $1575.00
3-rd year balance = $1575.00 + 37.50 = $1612.50
However, besides the initial balance and the interest rate (now called simple interest rate), we will
also include the number of months the account have been active. If there are no additional balance
added to the account, the interest rate remains constant throughout the year, and the interest is only
added at the end of the year (ignoring partial year ), calculate the balance at the end of the month.
Write your program below:
int main() {
double balance, rate; int month;
/* Calculate Final Balance Here */

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).

i. Understanding the Problem:

ii. Devising a Plan:

Page 3
CS1010E Tutorial 01: Identifiers, Types, and Operations 23 Jan 2017 - 27 Jan 2017

iii. Carrying Out the Plan:

iv. Looking Back:

Page 4

You might also like