Information Technology (CSEC)
Term 1 Week 9
Problem Solving and Program Design Lecture 2
Recap of Last Class
SPECIFIC OBJECTIVES
Students should be able to:
Review Mid Term Exam Paper with Solution
1. outline the steps in problem-solving;
2. use the divide-and-conquer approach to decompose large
everyday problems into smaller tasks;
3. define a problem by decomposing it into its significant
components;
4. explain the concept of algorithms;
Today`s Objective
Review of IPO charts and Algorithms
Understanding algorithms using
Blockly Programming
Programming using Csharp
• Solving a problem on the computer
-Introduction to Variables
Programming – Lecture 1 3
WHAT IS A COMPUTER
PROGRAM?
– A computer program is a series of
instructions which the computer follows.
These instructions indicate how the
processing of data is carried out.
– Where is data stored?
– What is RAM vs HARD DRIVE?
– If multiple data items are to be stored,
how is one data item distinguished from
another?
Programming – Lecture 1 4
INTRODUCTION TO PROBLEM
SOLVING
Steps in problem-solving:
(a) define the problem;
(b) propose and evaluate solutions;
(c) determine the most efficient solution;
(d) develop the algorithm; and,
(e) test and validate the solution.
PAST PAPER: Jan 17 P1 – Q42
SOLUTION: (B) DEFINE
6
THE PROBLEM
What is an Algorithm?
An algorithm is a series of precise
steps that leads to the solution of a
problem.
Characteristics of an Algorithm
– finite number of steps
– logical order
– precise
– unambiguous
Characteristics of Algorithms
The following are characteristics of a good
algorithm:
Precise – the steps are correctly stated
Finite number of steps – the algorithm should
always terminate after a finite number of steps
Unambiguous – the algorithm should be easy to
read and understand
Flow of control from one process to another
Terminate – algorithms should have a clear
stopping point.
PAST PAPER – Jan 2017 P1 Q36
• Answer: D – It is a graphical representation of the solution
Ways of Writing an Algorithm
Narrative:
– Each step is written as an informative
statement
Pseudocode:
– a language that mimics real
programming language
Flow chart:
– a diagram that arrange the components
of problem in a logical sequence
Real Life Algorithms
11
Programming with Blockly
12
Activity 1- Programming with Angry Birds
Click on the hyperlink below:
Activity 1- Programming with Angry
Birds
13
Activity 2- Collecting Treasure with Laurel
Click on the hyperlink bleow:
Activity 2 - Collecting treasure with
Laurel
14
INTRODUCTION TO PROBLEM
SOLVING
Steps in problem-solving:
(a) define the problem;
(b) propose and evaluate solutions;
(c) determine the most efficient solution;
(d) develop the algorithm; and,
(e) test and validate the solution.
Variables & Constants
Data types
Variable
– an entity that is used to store values
that may change e.g. number, name
Constant
– a value that is fixed e.g. 6 “Cindy”
Data type
– indicates the nature of the data that is
stored in a variable e.g. integer, real,
character, string
VARIABLES
Used for storing data items in a computer
Exist in RAM (destroyed when program is
closed)
In C#, there are different types of
variables:
Programming – Lecture 1 17
THINGS TO NOTE:
VARIABLES:
– Must begin with an alphabetic letter (a..z)
– May contain digits (0..9)
– May contain the underscore (_)
– Must NOT contain spaces or other characters
– E.g. Valid Variable Names: Name, Num1,
num2, num23sy, Hours_Worked, HoursWorked
– Invalid Variable Names: 1Num, _Num, Name$,
Hours Worked, Rate of Pay
Programming – Lecture 1 18
VARIABLES (cont’d)
QU: You need to store a person’s name and age
in a program. How many variables are required
and what type of data they store?
ANS: Two variables are required, say NAME
(String) and AGE (Integer).
QU: You need to store the sum of two numbers.
How many variables are required and what type
of data they will store?
ANS: Three variables required; two for the
numbers and one for the sum. Let’s call them
NUM1 (Integer or DOUBLE) and NUM2 (Integer or
DOUBLE) and SUM (Integer or REAL).
Programming – Lecture 1 19
Class Exercises
Click on the hyperlinks below and do
the try it yourself exercises
C# Variables (w3schools.com)
Sololearn: Learn to Code
Programming – Lecture 1 20
PAST PAPER: May/Jun 2014 Q9b
State the most appropriate data type for
each of the following types of values.
(i) The number of family members
(ii) A person’s height in metres
(iii) A person’s gender (M or F)
(iv) A person’s first name (4 marks)
PAST PAPER: May/Jun 2014 Q9b
SOLUTION:
State the most appropriate data type for each of the
following types of values.
(i)The number of family members
This is a whole number (e.g. 3, 5 etc) - INTEGER
(ii) A person’s height in metres
This can include decimal places (e.g. 1.7, 1.3) -
DOUBLE
(iii) A person’s gender (M or F)
This is a single character (e.g. M, F) - CHARACTER
(iv) A person’s first name (4 marks)
This is a series of letters (e.g. Kaleem) - STRING
STEP 1: Defining the Problem
Write a program to read two
numbers and output the sum of
the two numbers
Programming – Lecture 1 23
STEP 2: Analyzing the Problem
using an IPO chart
Input Processing Output
How do we get the output The sum (Sum)
Two numbers after we have gotten our
(Num1 and inputs (i.e. how do we get
Num2) a value for SUM after we
have read NUM1 and
NUM2?)
SUM := NUM1 + NUM2;
24
Statements
• Statements may be written using = or The value on the
RIGHT is computed first and stored in the variable on the LEFT
• Example:
Algorithm Pseudocode Comment
Find the sum of Num1 Sum = Num1 + Num2 Add Num1 and Num2 then
and Num2 store the result in Sum
Sum Num1 + Num2
Add 1 to A A=A+1 Add 1 to A and then store
the result in A
AA+1
Categorizing Statements is IPO
Keywords Category Reason
Read, Input, Accept Input Input statement are used to read the value
the user has entered
Prompt, Display Output Used for outputting messages on the
screen (often used in conjunction with
Read or Input to obtain values from user)
Write, Print Output Used for outputting the final result or the
result of a calculation on the screen
=,:=, Calculate Processing These are used for computing / calculating
a value
Add, Subtract, Processing These are used for calculating values
Multiply, Divide, DIV,
MOD
PAST PAPER – Jun 2012 P2 Q9
S1 – Input
S2 – Processing
S3 – Processing
S4 – Output
S5 – Input
S6 - Output
STEP 3: Develop an Algorithm
1: Prompt the user for the first number
2: Read the user’s response and store it in
variable NUM1
3: Prompt the user for the second number
4: Read the user’s response and store it in
variable NUM2.
5: Compute SUM using the formulae:
SUM := NUM1 + NUM2;
6: Output SUM
Does the order of these steps matter? If I
changed the order randomly, would I still get the
same results? Why should step 1 be done before
step 2? Or step 3 before step 4? Or step 5 before
steps 1-4? Or step 6 before steps 1-5?
Programming – Lecture 1 28
STEP 4: WRITE A PROGRAM
To write program we need a
programming language. We will use
Csharp. Why Csharp?
Each Programming language has its
own rules just as each spoken
language has rules too. These are
known as syntax e.g. In Csharp each
statement ends with a semicolon (;)
Programming – Lecture 1 29
Step 4: CONT`D
Csharp Program:
Programming – Lecture 1 30
Step 4: cont’d
Executing the Program:
Link to your online Csharp
compiler:
Online C# Compiler - Online C#
Editor - Run C# Online - Online
C# Runner - Execute C Sharp
(jdoodle.com)
Programming – Lecture 1 31
Displaying Values
• To display a value stored in a variable you may print the
variable or the variable with a message.
• Example: Print the value stored in Sum (assume Sum = 10)
Example What is Outputted
Print Sum 10
Print “The sum is : ”, sum The sum is : 10
Recap
– Write an algorithm that allows a
user to read three integers and
calculate and output their
average.
Class Ex1 IPO chart
Input Processing Output
Num 1 Avg = (num1 +num 2 +num 3)/3 Avg
Num 2
Num 3
Class Ex1- Algorithm
1. Start
2. Prompt the user for three numbers
3. Read user`s response and store in the
variables num1, num2 and num3
respectively.
4. Calculate the average using the formulae:
Average = (num1 +num2 +num3) / 3
5. Print /Display Average
6. End.
Class Ex1- Csharp Program
Class Exercise 1
Write a set of instructions to calculate and
output the perimeter of an equilateral
triangle when the length of the side is
supplied.
Class Ex1- IPO
Input Processing Output
3 sides of Peri = side1 + side2 + side3 Perimeter
a triangle
(side)
Class Ex1- Algorithm
1. Start
2. Prompt the user to enter 3 sides of a
triangle
3. Read user`s response and store in the
variables side1, side2 and side3
respectively.
4. Calculate Perimeter using the
formulae:
Perimeter = side1 + side2 + side3
5. Print Perimeter
6. Stop
Class Ex1- Csharp Program
Class Exercise 2
Write a program to read a person’s
name, hours worked and hourly rate
of pay.
Calculate the person’s salary (salary
= hours worked x hourly rate of
pay).
Output the person’s name and salary.
Programming – Lecture 1 41
Solving Class Ex 2:IPO Chart
Input Processing Output
Salary := HoursWorked x Name and Salary
Name, Hours RateofPay
Worked
Rate of Pay
42
SOLVING CLASS EXERCISE 2 – Algorithm
Step 3: Algorithm
START
1:Prompt the user for the person’s name
2: Read the user’s response and store it in
variable NAME
3: Prompt the user for the Hours Worked
4: Read the user’s response and store it in
variable HOURSWORKED.
5: Prompt the user for the Hourly Rate of Pay
6: Read the user’s response and store it in
variable RATEOFPAY.
7: Compute SALARY using the formulae:
SALARY := HOURSWORKED * RATEOFPAY;
8: Output NAME and SALARY
End
43
SOLVING EXERCISE 2
Csharp PROGRAM
Programming – Lecture 1 44
MORE THINGS TO NOTE
The name of the variable does not have to
be in the prompt.
E.g. if variable is RATE
DON’T USE
WRITE(‘Enter RATE : ‘);
Instead use:
WRITE(‘Enter Hourly Rate of Pay : ‘);
The user does not need to know the name of
the variable you are using; they only need
to know what they should enter!
Programming – Lecture 1 45
Write vs WriteLine
Write puts the cursor on the same
line whereas WriteLine advances the
cursor to the next line.
Programming – Lecture 1 46
Homework
Review the entire set of slides
1 Draw an IPO chart and an algorithm and C#
program to read two numbers. Calculate and
output the product of the two numbers [product
is the result when you multiply two numbers.
2. Draw an IPO chart and write an algorithm and
C# program to read a Celsius temperature.
Compute the corresponding Fahrenheit value
using F:= 32 + 9*C/5. Output both the Celsius
and Fahrenheit values.
47