QuickLearner ProblemSolvingProgramDesign
QuickLearner ProblemSolvingProgramDesign
QuickLearner ProblemSolvingProgramDesign
eISBN 978-967-2240-20-4
All rights reserved. No part of this book may be reproduced or transmitted in any form or
by any means, electronic, including photocopying, recording or by any information storage
or retrieval system, without prior written permission from the publisher, Politeknik Kuala
Terengganu
Author :
Yusnita Halim
Napisah Harun
Rosmayati Ismail
Published by :
Praise Allah the Almighty for the knowledge and strength that He has granted upon us in
writing this book. We would like to express our deepest appreciation to our team
members for their unparalleled support towards the completion of this book.
To our families, the completion of this book would not have been possible without their
encouragement, care and support.
Our appreciation also goes out for our friend and colleagues. Thank you for your advice,
support, and constructive critics, etc. We are really grateful for your support and
friendship.
To our former students, thank you for your comments and feedback during the process
for producing this book. For our students and others who will be using this book, we
hope that this book will provide a better understanding in problem solving and program
design.
Finally, to everyone that have helped us in the production of this book, please accept our
deepest thanks. May Allah bless you.
Yusnita Halim
Napisah Harun
Rosmayati Ismail
PREFACE
This book is written specifically for the use of students who are studying Problem
Solving and Program Design course in the form of a simpler infographic. The syllabus is
written for polytechnic and foundation level for problem solving using C++ language,
however, it can also be a useful guide for undergraduates.
For each topic in this book, there are examples of activities and solutions provided. In
addition, exercise questions were provided for each topics to enhance student’s
understanding. The book has been organized by chapter.
This book is written with the intention of providing a quick reference for
educators and students in problem solving and program design. Infographic
concepts are applied to simplify and enhance readers understanding of the
subject.
This book provides quick reference and information for problem solving
concepts with various activities and exercises. Upon completion of this book,
students will be able to solve a programming problem for a different
scenarios.
Disclaimer:
Images used is modified from the Internet and free PowerPoint template.
The originals are solely held by their owner.
Table of Contents
Acknowledgements
Preface
Introduction
Page
1 Chapter 1
Introduction to Programming Language
15 Chapter 2
Problem Solving Methods
43 Chapter 3
Fundamentals of Programming Language
Data and Identifier
Operator
Control Structure
82 Chapter 4
Basic of Programming Language
References
CHAPTER 1
Introduction to
Programming
Language
Contents :
History of programming language and approaches.
Types of programming languages : Machine, Assembly, High Level
Generations of programming language.
Definitions of programmer, program and programming.
Explain the language translators : Assembler, Compiler, Interpreter
Translator
Generations
Assembler
Machine
Compiler
Assembly
Interpreter
High Level
2
Chapter 1 | Introduction to Programming Language
1.1 Fundamentals
How it Works?
There are variety electronic device now that act as computer such as smartphone, tablet, laptop
that comes in variety of brand.
3
Chapter 1 | Introduction to Programming Language
1.2 Language
How it Works?
Current programming language around the world are Python, Javascript, Java, PHP, C#, C++,
Ruby, Go, Dart Language, Visual Basic, Haskell, SQL and so on.
4
Chapter 1 | Introduction to Programming Language
1.2 Language
Interaction
Interaction
5
Chapter 1 | Introduction to Programming Language
6
Chapter 1 | Introduction to Programming Language
1.4
Advantage & Disadvantage
o Advantage :
most efficient in terms of
storage area and speed.
o Disadvantage :-
instruction and syntax in
a program is not 1st Gen. Machine Language (1950-1960)
standardized.
o Machine-dependent. o Advantages:-
Example: a program written Program can run faster.
in Apple PC cannot be run in An efficient use of the
IBM PC. memory.
o Disadvantages:-
Assembly Language (1950s) 2nd Gen. Errors can be made
easily.
Difficult to understand.
o Advantage:- It takes a long time to
Machine-Independent. write the program.
The program can be
written and executed on 3rd Gen. High Level Language (start 1960s)
any computer.
Easy to be developed o Advantage:-
and maintained. Machine-Independent.
Some text here The program can be
written and executed on
High Level Language (start 1960s) 4th Gen. any computer.
Easy to be developed
and maintained.
It is simpler and easier to
o Advantage:- code.
Machine-Independent.
The program can be 5th Gen. High Level Language (start 1960s)
written and executed on
any computer.
Easy to be developed
and maintained.
7
Chapter 1 | Introduction to Programming Language
8
Chapter 1 | Introduction to Programming Language
Program
o A set of step-by-step instructions that directs a computer
to perform a specific task and to produce the required
results.
o It’s written in a programming language and is converted
into the computer’s machine language.
Programmer
o Person who writes the program.
Programming
o Programming is a process of
designing / creating a program.
9
Chapter 1 | Introduction to Programming Language
1.7
o Example:
Message displayed on the screen is “please enter a number”.
o Example in C++:
Statement : system("pause")
10
Chapter 1 | Introduction to Programming Language
1.8
ASSEMBLER
Sseg segment
stack
db 256 dup (?) 11101 100000
Some text here 100000
Sseg ends 11110
10111000
dseg segment
data db “2 x 4 = ” 11101000
Assembler
dseg ends 11111100
cseg segment 10111000
Assume 10001110 11011000
10111000
cs:cseg:ds:sdeg:ss:sseg 10001110 11000000
es:nothing
Start proc far
Machine Language
Assembly
Language
11
Chapter 1 | Introduction to Programming Language
1.8
COMPILER
o Translates the whole program
at one time.
o Translates the source code,
for COBOL language to
machine example language.
o Used to translate C, COBOL
and other language.
o A program that translates
from a low level language to a
higher level one is a
decompiler.
# include <stdio.h>
int main( )
{ 11101 100000 100000
Some text here
int x, y, z, sum; 11110
double avg; 10111000
printf(" Enter 3 numbers:"); 11101000
scanf("%d %d %d",&x, &y, &z); Compiler 11111100
sum = x + y + z; 10111000
avg = sum / 3;
10001110 11011000
printf("x = %d y = %d z = %d",
x, y, z);
10111000
printf("the average is = %f", 10001110 11000000
avg);
return 0; Machine Language
}
C Language
12
Chapter 1 | Introduction to Programming Language
1.8
INTERPRETER
10001110 11000000
‘BASIC PROGRAM 10111010
‘AVERAGING THREE 10111011
Some text here
INTEGERS 10001101 110110
PRINT “Mengira Purata” 10111111
Interpreter
PRINT “---------------------“ 10110000 000000010
PRINT “Nilai A = “; 20 10110011 000001000
PRINT “Nilai B = “; 30 11100011 111111000
PRINT “Nilai C = “; 40 00001110 110000
PRINT “Purata tiga nombor 10100000
ialah “; (20+30+40)/3 10111001
END 10100100
Basic Language
Machine Language
13
Chapter 1 | Introduction to Programming Language
1.
1. List and explain generations of
programming language.
2. State translator that translates high level
language
Some text here to machine language.
3. Differentiate between compiler and 2.
interpreter.
3.
Compiler Interpreter
14
CHAPTER 2
Problem Solving
Methods
15
Chapter 2 | Problem Solving Methods
Contents :
Phase in Programming Life Cycle (PLC)
Problem solving concept.
Input, process and output.
Identify the input, process and output based on problem statement.
Different types and pattern in algorithm to solve problem.
Flowchart symbols
Patterns in algorithm
Terminator
Sequential
Connector Lines
Conditional
Process
Iterational
Input / Output
Decision
On and Off Page
Reference
16
Chapter 2 | Problem Solving Methods
Documentation
7
Review and if necessary, revise internal documentation; formalize and
complete end-user (external) documentation
17
Chapter 2 | Problem Solving Methods
1.
Identify the phase involve for the activities
below:-
1. Develop a detailed logic plan.
2. This step is also known as programming 2.
phase.
3. Syntax error, logic error and runtime
3.
error always occur in this phase.
18
Chapter 2 | Problem Solving Methods
2.2
Thinking process and is part of the larger problem
process that includes problem finding and problem
shaping.
Notes :
19
Chapter 2 | Problem Solving Methods
20
Chapter 2 | Problem Solving Methods
Input :
Discuss on steps needed to deposit cash on
Cash Deposit Machine (CDM). Identify
input, process and output.
Some text here
Process :
Output :
21
Chapter 2 | Problem Solving Methods
22
Chapter 2 | Problem Solving Methods
Answer Process:
Calculate speed using formula :
IPO Chart
speed= distance / timeHour
Input Process Output
Output:
distance, speed = speed speed
timeHour distance/time
Hour
Process:
Answer
1. Set fine = 0.20
IPO Chart
2. Calculate total fine charge using formula :
Input Process Output total_fine= day x fine
day total_fine= total_fine
23
Chapter 2 | Problem Solving Methods
Problem Analysis:
Uncle Ahmad wants to buy 5 cans of paint
to paint his house. The price for a can of Input :
paint is RM 25.50. Calculate the price to be
paid for 5 cans of paint that bought.
Process:
Answer
IPO Chart
Output:
Problem Analysis:
Calculate and display the total mark of three
quizzes, where the users have to enter the Input :
mark of each quiz.
Process:
Answer
IPO Chart
Output:
24
Chapter 2 | Problem Solving Methods
LOGIC
PROGRAM DESIGN
25
Chapter 2 | Problem Solving Methods
2.4
01 02
A sequence of
instructions to solve a A step-by-step procedure
problem, written in to solve a given problem.
human language.
04
Programmer writes the
problem solving in the form 03
of an algorithm before
coding it into computer An algorithm should
language. always have a clear
stopping point.
05
26
Chapter 2 | Problem Solving Methods
2.5 of good
27
Chapter 2 | Problem Solving Methods
28
Chapter 2 | Problem Solving Methods
29
Chapter 2 | Problem Solving Methods
2.6 in
A B C
1. step_1 repeat
if condition then
2. step_2 part to repeat
code block true
3. step_3 until condition
else
:
code block is not true
n. step_n
End if
7. Display Average
30
Chapter 2 | Problem Solving Methods
2.7
Definition
31
Chapter 2 | Problem Solving Methods
2.8 symbol
End
32
Chapter 2 | Problem Solving Methods
Notes :
START
Process
Task performing operation / formula
END
33
Chapter 2 | Problem Solving Methods
1 All boxes of the flowchart are connected with Arrows. (Not lines)
Flowchart symbols have an entry point on the top of the symbol with no
2 other entry points. The exit point for all flowchart symbols is on the
bottom except for the Decision symbol.
The Decision symbol has two exit points; these can be on the sides or the
3 bottom and one side.
7 All flow charts start with a Terminal or Predefined Process (for interrupt
programs or subroutines) symbol.
34
Chapter 2 | Problem Solving Methods
Total= num1+num2+num3
Average= Total / 3
4. Calculate average
Average=Total/3 Display num1, num2, num3
Display Average
5. Display num1, num2, num3 and the
average END
Algorithm
1. Set Pi=3.142
2. Input a radius value.
3. Calculate area using formula:
area = Pi * radius * radius
4.Print the area of the circle.
35
Chapter 2 | Problem Solving Methods
2.11
Definition
Format
pseudocode
START
Input
Statement
Output
END
36
Chapter 2 | Problem Solving Methods
1 2 3
37
Chapter 2 | Problem Solving Methods
2.13 to write
38
Chapter 2 | Problem Solving Methods
Pi = 3.142
Algorithm Input radius
Algorithm
1. Set Total=0, Average=0
2. Input a, b, c
3. Total up the 3 numbers
Total= a+b+c
4. Calculate average
Average=Total/3
4. Display the average
39
Chapter 2 | Problem Solving Methods
40
Chapter 2 | Problem Solving Methods
2.15 VS
Pseudocode Flowchart
• Steps in problem solving that is written • A graphical representation of
half in programming code and half in instructions done in problem using
human language certain symbols that are connected to
flow lines
START
START
Pi = 3.142
Pi = 3.142
Input radius
Input radius
area = Pi * radius *radius area= Pi * radius * radius
Output area
Display area
END
END
41
CHAPTER 3
Fundamentals of
Programming
Language
42
Contents :
Data and Data Types
Identifier
Variable and Constant
Data
Data is an array of facts that can be modified by computer into useful form for
human.
Users deal with data in ways that let them know what kind of information they are
managed. Data is managed by the instructions in computer language.
Examples of data are number, text, currency and others.
Data Types
A data type is a classification of the type of data that a variable or object can hold in
computer programming.
A data storage format that can contain a specific type or range of values.
When computer programs store data in variables, each variable must be assigned a
specific data type.
Examples of data types include integers, floating point numbers, characters, strings,
and arrays.
Identifier
Identifiers are used to name constants, variables, function names and labels.
Identifiers are formed by combining letters (both upper and lower case letters)
digits and the underscore ( _ ).
Identifiers in C++ are case sensitive.
43
Chapter 3 | Fundamentals of Programming Language
Variable
A variable is any measured characteristic or attribute that differs for different
subjects.
Variables also can be defined as identifiers whose value may change during the
course of execution of a program. Each data used for computer program is stored in a
variable.
Constant
Constants are used to store values that never change during the program execution.
Using constants makes programs more readable and maintainable. Constants are
expressions with a fixed value.
44
Chapter 3 | Fundamentals of Programming Language
3.1
NUMERIC
-Type of number
NON-NUMERIC
-Type of text
NAMING VARIABLE
-Rules for naming variable
CONSTANT VARIABLE
-Values that never change
DECLARE VARIABLE
-Variable with data type
in code
45
Chapter 3 | Fundamentals of Programming Language
3.2
NUMERIC NON-NUMERIC
03 - CHARACTER
01 - INTEGER
Consists of all letters, numbers
o Positive and negative numbers and special symbols.
including zero and no decimal Characters are surrounded by
place. single quotation mark (‘ ‘).
o Keyword : int Example: ‘A’, ‘m’,’=’, ‘#’, ‘1’ or ‘ ‘
o Example: 0, +1, -10.
04 - BOOLEAN
02 – REAL NUMBER
Used in making yes-or-no
decisions (TRUE-or-FALSE).
o The number will be stored in Assume a = 2 and b = 5
floating point. If (a < b)
o Keyword : float Decision is TRUE
o Example: 36.7C, 234.55kg, EndIf
3.142 Keyword : double
o Example: RM12.0019,
RM1020.0009 05 - STRING
46
Chapter 3 | Fundamentals of Programming Language
3.3
The image part with relationship ID rId2 was not found in the file.
The image part with relationship ID rId2 was not found in the file.
1 3
RULES
Naming Variable
6
Special character
such as :
5
*,@,#,!,$,%,^,&,(,)
are not allowed.
47
Chapter 3 | Fundamentals of Programming Language
3.4
Reserve Word
a word in a programming language that has a fixed meaning and
cannot be redefined by the programmer.
a word that cannot be used as an identifier, such as the name of
a variable, function, or label, it is "reserved from use“
A special word reserved by a programming language or by a
program.
case
xor if sizeof ststic
48
Chapter 3 | Fundamentals of Programming Language
3.5
Valid Valid
Invalid
‘float’ is a
Valid name Valid name reserved word
Valid name Cannot start Special symbol @
with a digit
Valid Invalid
Invalid
Cannot has
Valid name Special symbol $ space
49
Chapter 3 | Fundamentals of Programming Language
3.6
Declare a Variable
char
int
char Grade;
int age; char name[10];
int number;
50
Chapter 3 | Fundamentals of Programming Language
Activity 3.1A
1. Identify and write either valid or invalid for
the variables below.
a) float hasilBahagi;
b) int 2jumlah;
c) double Jalan#2;
d) int pertama;
e) float hasil_tambah;
Activity 3.1B
1. Declare variables for the statements below.
a) Keep the average mark of students in a class.
b) Keep the grade of students occupying for a computer course.
c) Keep the CGPA results for the students.
d) Store the value of PI to 3.142.
e) Keep the age of worker in the system.
Answer Declaration
a) Keep the average mark of students in a class. float mark;
b) Keep the grade of students occupying for a computer Char Grade;
course.
c) Keep the CGPA results for the students. float CGPA;
d) Store the value of PI to 3.142. const double PI=3.142;
e) Keep the age of worker in the system. int worker_age;
51
Chapter 3 | Fundamentals of Programming Language
Exercise 3.1A
1. Identify and write either valid or invalid for
the variables below.
a) return
b) _balance
c) my_politeknik
d) average,mark1
e) Stud Name
f) $Salary
Exercise 3.1B
1. Declare variables for the statements below.
a) Total salary per month.
b) Keep the temperature reading.
c) Interview result either pass or not.
d) Permanent value for color is a red.
e) Count the number of catch fish.
Answer Declaration
a) Total salary per month.
b) Keep the temperature reading.
c) Interview result either pass or not.
52
Chapter 3 | Fundamentals of Programming Language
Exercise 3.1C
Problem 1 :
A simple program is created to store personal data for employee. The information
that needs to be store includes full name, phone number and employee number.
State the suitable variable, data type and complete declaration for the program.
Problem 2 :
You are creating a simple program to identify if a student passed or fail for their
marks in the mid semester test. State the suitable variable, data type and complete
declaration for the program.
Problem 3 :
A simple program is created to calculate area of a circle using a given radius. State
the suitable variable, data type and complete declaration for the program.
53
Chapter 3 | Fundamentals of Programming Language
Contents :
Assignment
Logical
Increment & Decrement
Arithmetic
Relational
Assignment
Operator
1
Logical
2
Increment/Decrement
3
Arithmetic
4
Relational
5
Operator Operand
An operator is a symbol or Operand is the object that are
character that causes the compiler manipulated;
to take an action. example:
Example : ‘+’ is an arithmetic 2+y/x
operator that represent addition. explain:
Operators act on operands and all 2, y and x are operands.
operands are expressions.
54
Chapter 3 | Fundamentals of Programming Language
3.7
highest
x += y Assignment by x=x+y
sum Result=30
lowest
x /= y Assignment by x= x / y + Addition Result =x + y;
quotient Result=13
x |= y Assignment by
bitwise OR x=x | y 5 Relational Meaning Expression
== equals 100 = =100
x ^= y Assignment by x= x ^ y
bitwise XOR > Greater than 100 > 50
< Lower than 50 < 100
Logical Meaning Expression
2 x=1, y=0 >= Greater or 100 >=90
equals
&& AND x && y (False)
<= Lower or 90 <=100
|| OR x || y (True) equals
! NOT !x (False) != Not equals 90 !=100
55
Chapter 3 | Fundamentals of Programming Language
56
Chapter 3 | Fundamentals of Programming Language
57
Chapter 3 | Fundamentals of Programming Language
Exercise 3.2A
1. Find the value for the following expression.
Given x = 10, y = 7 and z = 2
a) (x * y) % z
b) y (x + z) (x – y)
c) x / z – (x * x + y)
d) 5 (x + y + z) – x / z
Answer
a)
b)
c)
d)
Exercise 3.2B
1. Given the value a = 0, b = 6 and c = 3. Write TRUE or FALSE for the expression. Show
all the steps clearly.
a) !(a==b) && (a>b)
b) ! ( a == 0) && (b != 2)
c) ( (a == c) && ( b == c) ) || (a < 1)
d) ((a + b > b) && (a == a)) || (b == a)
Answer
a)
b)
c)
d)
58
Chapter 3 | Fundamentals of Programming Language
Exercise 3.2C
Find the value for the following expression.
Given h = 15, k = 8;
a) h -= k
b) h /= k
c) h %= k
d) h=k
Answer
a)
b)
c)
d)
Exercise 3.2D
Assume i, j and k is an integer variables with value i = 5 and j=3. What is the value of this
expression?
a) k = j++
b) k = i * j--
c) k = j + i * j++
d) k = 25 / j++ - 18 % i
Answer
a)
b)
c)
d)
59
Chapter 3 | Fundamentals of Programming Language
Exercise 3.2E
Given the following scenario. Identify the formula needed that combine operators to solve the
problems.
a) You want to buy 2 different types of magazines. Given the price of the magazine and the
number of magazines purchased, determine and print the total price to be paid.
b) A-ONE supermarket provides 10% discount from actual price. Find a solution to calculate
the new price after discount given.
c) Find area of rectangle and user have to enter length and width of rectangle.
Answer
a)
b)
c)
60
Chapter 3 | Fundamentals of Programming Language
Contents :
Sequence
Selection
IF, IF-ELSE, NESTED IF-ELSE and SWITCH CASE
Looping
FOR, WHILE and DO-WHILE
Sequence Looping
Simplest control structure. A programming control structure that
In this control, the program instructions allows a series of instructions to be
are executed one by one (from top to executed more than once.
bottom) starting from the first and The similar statement is repeated
ending in the last instruction. several times until the conditions are
Every box in control structure is a fulfilled.
process. Every process is done Looping : FOR, WHILE and DO-WHILE
sequentially.
Selection
This control structure will execute an instruction based on result of a condition either TRUE
or FALSE.
If the condition result is true, the control program will execute the instruction within the
TRUE loop operation.
Otherwise, it will execute the instruction within the FALSE loop operation.
Selection : IF, IF-ELSE, NESTED IF-ELSE and SWITCH CASE
61
Chapter 3 | Fundamentals of Programming Language
3.8
SEQUENCE SELECTION
Problem Solving
Control Structure
Pseudocode : Pseudocode :
Start Start
Statement A If (condition)
Statement B Is the problem True statement
End solving depends Endif
No
on the options End
Flowchart : (true or false)?
Flowchart :
Start
Yes Start
Statement A
Is the problem True
Condition
solving need to
Statement B be repeated No False Statement
until the
problem solved? End
End
Yes
LOOPING
Pseudocode : Flowchart :
While Do While
Initialize; Initialize; Start
while (condition) do
{ Statement True { Statement True
counter; counter; False
} Condition A End
}
while (condition); True
For Statement A
for (initialize; condition; counter) {
Statement True }
62
Chapter 3 | Fundamentals of Programming Language
3.9
Rule of Types of
Selection Selection
1 Option
If … EndIf
Only do the instruction
if condition is TRUE
Rule of Types of
Selection Selection
2
If … Else
Options CONCEPT OF
SELECTION
Only do the instruction
either condition is TRUE
or condition is FALSE
Rule of Types of
Selection Selection
If … Else If
Multiple
Switch Case
options Nested If
63
Chapter 3 | Fundamentals of Programming Language
If … EndIf
Rules: Flowchart:
If (condition) START
Instruction (do this instruction if condition is true)
Endif
true
If condition is not true, no instruction will be condition statement
executed
Pseudocode :
false
If (condition)
True statement END
EndIf
Output: END
Salary
64
Chapter 3 | Fundamentals of Programming Language
If … Else
Pseudocode : Flowchart:
If (condition) START
statement 1
true
false
Else statement 2 condition statement 1
statement 2
EndIf False
END
Output:
Salary
END
65
Chapter 3 | Fundamentals of Programming Language
If … ElseIf
Flowchart:
Pseudocode :
START
If (condition 1)
statement 1 true
statement 1 condition
1 false
ElseIf (condition 2)
statement 2
false
statement 2 true
condition
Else 2
statement 3
statement 3
Endif END
Output “Fail”
3. Print status
66
Chapter 3 | Fundamentals of Programming Language
Switch Case
Flowchart:
Pseudocode :
START
Switch (case)
case 1 :
statement 1 true
case 1 statement 1
break
case 2:
statement 2 false
break true
case2 statement 2
default :
statement 3 false
End END
statement 3
END
67
Chapter 3 | Fundamentals of Programming Language
Nested If
Type 1 Type 2
If (condition1) If (condition1)
If (condition2) If (condition2)
If (condition3) If (condition3)
Statement that will be executed if
Statement True condition1, condition2 and condition3
are true.
Endif Else
Statement that will be executed if
Endif condition1, and condition2 are true but
Endif condition2 is false.
Endif
Else
Statement that will be executed if
condition1 is true but condition2 and
condition3 is false.
Endif
Else
Statement that will be executed if condition1 is false
Endif
Type 3
If (condition1)
Statement that will be executed if condition 1 is true
Else
If (condition 2)
Statement that will be executed if condition2 is
true but condition1 is false
Else
If (condition3)
Statement that will be executed if
condition3 is true but condition1 and
condition2 are false.
Else
Statement that will be executed if
condition1,
condition and condition3 are false
Endif
Endif
End if
68
Chapter 3 | Fundamentals of Programming Language
PROBLEM
To determine whether a candidate is qualified or not to get a scholarship based on his / her
study years, guardian’s salary and student CGPA. If the study year is more than 1, student’s
CGPA is not less than 3.00 and guardian’s salary is below than RM500, student will be
considered for a scholarship.
PSEUDOCODE FLOWCHART
START
START
69
Chapter 3 | Fundamentals of Programming Language
Exercise 3.3A
Question :
Write a pseudocode and flowchart that ask a user to enter an age. If the age is equal or more
than 18 years old, print the statement “18SX Category”.
Answer
a) Pseudocode
b) Flowchart
70
Chapter 3 | Fundamentals of Programming Language
Exercise 3.3B
Question :
Program that receives the current temperature as input. If the temperature is 80 degrees or
more, output a message telling the user to go swimming, otherwise, if the temperature is 50
degrees or more, output a message to go running, otherwise stay inside. Based on problem
given, write a pseudocode and flowchart.
Answer
a) Pseudocode
b) Flowchart
71
Chapter 3 | Fundamentals of Programming Language
Exercise 3.3C
Question :
The following ID’s number will determine level of user for security system. Write a pseudocode
and flowchart based on table below to identify level of user for the system.
ID Level of User
001 Super Admin
002 Admin
003 Staff
004 Member
Answer
a) Pseudocode
b) Flowchart
72
Chapter 3 | Fundamentals of Programming Language
Exercise 3.3D
Algorithm Answer
1. Set x = 1
2. Compare whether counter less than 26. a) Pseudocode
If yes, go to step 3
If no, go to step 7
3. Input name, matrix_num,mark_CA,
mark_FinalTest
Question :
Write a pseudocode and draw a flowchart
for While loop based on the given algorithm
as above.
Answer
b) Flowchart
73
Chapter 3 | Fundamentals of Programming Language
Exercise 3.3E
Question :
Answer
a) Pseudocode
b) Flowchart
74
Chapter 3 | Fundamentals of Programming Language
3.10
FOR LOOP
1 Initialize
1 2 3
For (initialize; condition; counter) A value to start a loop.
{ Must be a number.
example : x=0, i=3,
Statement True
nom=1, count=20
}
WHILE LOOP
2 Condition
Initialize; 1 Rules for the condition:
While (condition) 2 If the condition is true /
{ fulfilled, the process will be
FLOW OF performed.
Statement True
counter 3 LOOPING Then, the program loops back
and recheck the condition, if
} the condition is true / fulfilled,
repeat the process.
When the condition is false,
then exit the loop.
DO WHILE LOOP
Initialize; 1 3 Counter
Do Counter is to increase or
{ decrease the initialize value.
Statement True example : i++, y-- ,
count=count + 10,
counter 3
nom=nom-2
} While (condition); 2
75
Chapter 3 | Fundamentals of Programming Language
PROBLEM &
PROBLEM
ANALYSIS ALGORITHM FLOWCHART
76
Chapter 3 | Fundamentals of Programming Language
PROBLEM &
PROBLEM
ANALYSIS ALGORITHM FLOWCHART
77
Chapter 3 | Fundamentals of Programming Language
Exercise 3.3A
Algorithm Answer
1. Set x = 1
2. Compare whether counter less than 26. a) Pseudocode
If yes, go to step 3
If no, go to step 7
3. Input name, matrix_num,mark_CA,
mark_FinalTest
4. Check if the mark_CA AND mark_FinalTest is
greater or equal to 40%
4.1 If yes, Display name and matrix_num and
status is “Pass”
4.2 If no, Display name and matrix_num and
status is “Fail”
5. Add counter using formula :
x = x+1
6. Repeat step 2
7. End
Question :
Write a pseudocode and draw a flowchart for
While loop based on the given algorithm as
above.
Answer
b) Flowchart
78
Chapter 3 | Fundamentals of Programming Language
Exercise 3.3B
Algorithm Answer
1. Set x = 1
a) Pseudocode
2. Compare whether counter less than 26.
If yes, go to step 3
If no, go to step 7
3. Input name, matrix_num,mark_CA,
mark_FinalTest
4. Check if the mark_CA AND mark_FinalTest is
greater or equal to 40%
4.1 If yes, Display name and matrix_num and
status is “Pass”
4.2 If no, Display name and matrix_num and
status is “Fail”
5. Add counter using formula :
x = x+1
6. Repeat step 2
7. End
Question :
Write a pseudocode and draw a flowchart for For
loop based on the given algorithm as above.
Answer
b) Flowchart
79
Chapter 3 | Fundamentals of Programming Language
Exercise 3.3C
Algorithm Answer
1. Set x = 1
2. Input name, matrix_num,mark_CA, a) Pseudocode
mark_FinalTest
3. Check if the mark_CA AND mark_FinalTest is
greater or equal to 40%
3.1 If yes, Display name and matrix_num and
status is “Pass”
3.2 If no, Display name and matrix_num and
status is “Fail”
4. Add counter using formula :
x = x+1
5. Compare whether counter less than 26
If yes, go to step 2
If no, go to step 6
6. End
Question :
Write a pseudocode and draw a flowchart for
Do While loop based on the given algorithm as
above.
Answer
b) Flowchart
80
Chapter 3 | Fundamentals of Programming Language
Exercise 3.3D
Given the following scenario. Write a Answer
pseudocode and draw a flowchart for While,
For and Do While loop before creating a a) Pseudocode
program.
Answer
b) Flowchart
81
CHAPTER 4
Basic Programming
Codes
82
Contents :
Element of the programming language
Standard and best practices in programming
Steps in creating a C++ program
Basic program concepts
Analyse program to identify input, process and output
Construct a pseudo code based on given programming code
Construct a flowchart based on given programming code
Output
Input
The outcomes from the
User input data by keyboard
programming process.
or text file.
What the user will
A MUST element in a
acquire from the
programming code for
programming code.
interactive programming.
83
Chapter 4 | Basic Programming Codes
1. Comment 6. Identifiers
to document and understand the The name of the variable,
program. constant and function in
// - single line program.
/* */ - multiple paragraph
7. Special symbol
2. Preprocessor directive
To represent the basic
Starts with #. arithmetic operations
Used to include header files. e.g. + , - , * , / , %
9. Statements
4. Main function
An instruction including input,
A block code that runs a task. output, process, variable
A MUST in a program. declaration.
10. Blocks
5. Reserved word
A special word reserved by Opening braces ‘ { ‘ and closing
a programming language or by a braces ‘ } ’ of the function.
program {…}
84
Chapter 4 | Basic Programming Codes
4.2 in Practice
Preprocessor
directive
Comment
Punctuation
Identifiers
Statements
Variable
declaration
Return
Blocks statement
85
Chapter 4 | Basic Programming Codes
Activity 4.1
1. Identify the basic elements of the programming code below.
Answer
Element Programming Code
Comment
Preprocessor directive
Header file
Main Function
statement
Punctuation ;
Return statement
86
Chapter 4 | Basic Programming Codes
Exercise 4.1
Question :
Based on the given program code, write the program code for element of programming
a. Main function
b. Block
c. Reserved word
d. Header file
e. Identifier
Answer
a.
b.
c.
d.
e.
87
Chapter 4 | Basic Programming Codes
4.3 in
Programming
Naming
convention
Commenting
Limit Line &
Length Documentation
kiraMarkahKelas,
Consistent
STANDARD Consistent
& BEST
jumlahMarkah Indentation
@ Naming
& Line
Scheme
PRACTICES
kira_markah_kela Spacing
s, jumlah_Markah
How it Works?
Refer to the given examples to get the better understanding for each of the standard and best
practices.
88
Chapter 4 | Basic Programming Codes
Source
1
Type your source code
File extension .cpp
Compiler Output
2 3
Compile the program The output generated from the
to check the error. compiled program based on
user input to the program
89
Chapter 4 | Basic Programming Codes
HEAD
Code Example :
90
Chapter 4 | Basic Programming Codes
Head
START
Pi = 3.142
Input radius
area = Pi * radius *radius
Output area
END
Pseudocode
Main
function
91
Chapter 4 | Basic Programming Codes
Head
Main
function
START
Variable
Pi = 3.142 declaration
Input radius
area = Pi * radius *radius
Display area
END
Pseudocode
The complete
pseudocode to calculate area program code
of circle
How it Works?
The arrow line from the pseudocode pointing to the matching statement for C++ code.
92
Chapter 4 | Basic Programming Codes
Input
Process
The operations like
arithmetic and logical
operations.
Output
93
Chapter 4 | Basic Programming Codes
START
Read int no1, no2
Input the first number : no1
Input the second number : no2
avg = (no1 + no2) / 2
Print avg
END
A C++ code to calculate average of two input Pseudo Code
numbers
Pseudo code to calculate average of two
input numbers
How it Works?
The arrow line from the C++ code pointing to the matching statement for pseudo code.
94
Chapter 4 | Basic Programming Codes
START
Prompt user
1 “Input the first
number”
2 INPUT no1
1
2 Prompt user
3 “Input the
second number”
3
4
4 INPUT no2
5
6
5 avg – (no1+ no2) /2
6
Display avg
Pseudo code to calculate average of two
input numbers
END
How it works?
Mapping each of the number from the pseudo code to the number from the flowchart. Each of
the number show the conversion from the command in pseudo code to the symbol in flowchart.
95
Chapter 4 | Basic Programming Codes
Activity 4.2
Question :
1. Create a file average.cpp
2. Write the program code
3. Compile the program
4. Generate the output from the program
5. Write the output of the program
6. Identify the input, process and output of the program.
Answer
5. Output :
96
Chapter 4 | Basic Programming Codes
Exercise 4.2
Output :
Question : Arrange the scrambled up snippet code to make a working C++ program that
produce the output listed.
Answer
97
Chapter 4 | Basic Programming Codes
a) Pseudocode
b) Flowchart
Question :
Write a pseudocode and draw a flowchart for
based on the above program code.
98
Chapter 4 | Basic Programming Codes
Question : OUTPUT:
Follow the process below in completing your
codes. Task 2 : Flowchart
Answer
Task 3 : Task 4 :
99
REFERENCES
Somashekara. M.T., Guru. D. S., &. Manjunatha. K. S. (2018). Problem Solving with C (2nd
Edition). India. PHI Learning.
Wan Mohammad, W.A, Mohd Mydin, A. and Ishak, S. (2015). Introduction to C++
Programming. Shah Alam. Oxford University Press.
Unsplash. (n.d.). Beautiful free images & pictures. Unsplash. Retrieved November 18,
2021, from https://unsplash.com/.
101