Part6 ProgrammingLogic&DataStructure
Part6 ProgrammingLogic&DataStructure
Overview of
Computer Programming
1
Overview of Computer Programming
Three major operations in programs:
Input
• Input : Data items enter the computer system
and are put into memory, where they can be
processed.
• Process : processing data items may involve
organizing or sorting them, checking them for
accuracy, or performing calculations with Process
them.
• Output : after data items have been processed,
the resulting information usually is sent to a
printer, monitor, or some other output device so
people can view, interpret, and use the results.
Output
2
Program Development Cycle
3
Understand the problem & Plan the logic
4
Plan the logic
• Use one of two tools
• Pseudocode - is an English-like representation of the logical
steps it takes to solve a problem.
• Flowchart - is a pictorial representation of the same thing.
Pseudocode Flowchart
Start
input number
set answer = number * 2
output answer
stop
5
Pseudocode Statements
• Pseudocode : is an English-like representation of the
logical steps it takes to solve a problem.
• Sample pseudocode is
Start
input number
set answer = number * 2
output answer
stop
6
Flowchart and its symbols
• Flowchart is a pictorial representation of the logical steps it
takes to solve a problem.
• Flowchart symbols are:
input number
1) Input symbol
set answer = number * 2
2) Processing symbol
output answer
3) Output symbol
right on a page. 7
Flowchart and its symbols (cont’d)
• Existing flowchart symbols are –
8
Exercise : 1
1. Input A.
2. Processing B.
3. Output C.
4. Decision D.
5. Terminal E.
9
Exercise : 2
1. Draw a flowchart and write pseudocode to represent the
logic of a program that allows the user to enter a value. The
program multiplies the value by 10 and outputs the result.
2. Draw a flowchart and write pseudocode to represent the
logic of a program that allows the user to enter a value for
the radius of a circle. The program calculates the diameter
by multiplying the radius by 2, and then calculates the
circumference by multiplying the diameter by 3.14. The
program outputs both the diameter and the circumference.
10
Working with Variables
11
Initializing the variable
• Declaring a starting value is known as initializing the variable.
Pseudocode Flowchart
Start
Declarations
num number
num answer
input number
set answer = number * 2
output answer
stop
12
Naming variable
• The variable names used throughout this book following rules:
• Variable names must be one word.
Eg. hourly_wage, num, answer, hour1, hour2, etc.
• Variable names should have some appropriate meaning.
Eg. rate_of_return, interest
• Variable names must be started with letter, and then follows
letters, digits and (_).
• The variable should be used camel casing
Eg. hourlyWage, rateOfRetrun, etc.
13
Data types of Variables
• Variables can also be different data types –
• A numeric variable is one that can hold digits and have
mathematical operations performed on it.
Eg. num age;
• A string variable can hold text, such as letters of the
alphabet, and other special characters, such as punctuation
marks.
Eg. string name
Problem 1
• Adding two numbers.
• Input -> number1, number2
• Process -> answer = number1 + number2
• Output -> answer
14
Problem 1 (con’t)
Pseudocode Flowchart
Start
Declarations
num number1, number2
num answer
input number1, number2
set answer = number1 + number2
output answer
stop
15
Writing clear Prompts and Echoing input
Pseudocode
Start
Sample output
Declarations
-> Enter a name : Mya Mya
string name -> Enter age for Mya Mya 34
num age
output “Enter a name : ” Prompt
input name
output “Enter age for ”,name
Echoing input
input age
stop
Draw a flow chart for given
Pseudocode.
16
Declaring Named Constants
• Pseudocode • Besides variables, most
programming languages
Start
allow you to create
Declarations named constants.
num radius, area • A named constant is
Constant similar to a variable,
num PI=3.14
variable
output “Enter a radius for a
except it can be assigned
Circle:” a value only once.
input radius
set area = PI*radius*radius
output “Circle’s Area: ”,area
stop
Draw a flow chart for given Pseudocode.
17
Operators
• Arithmetic operators
+, -, *, /, %, Operators Symbol Precedence Associativity
19
Exercise 4
1. Draw a flowchart and write pseudocode to represent the
logic of a program that allows the user to enter a Celsius
degree, calculate the corresponding Fahrenheit degree and
outputs the result.
2. Draw a flowchart and write pseudocode to represent the
logic of a program that calculate the area of triangle and
then output the result.
3. Draw a flowchart and write pseudocode to represent the
logic of a program that allows the user to enter two
numbers, calculate the quotient and remainder and then
output the result.
20
Chapter 2. Making Decision
21
Making Decision
Understanding the Three Basic Structures
22
Selection (Decision) Structures
23
Single – alternative selection structure
Pseudocode Flowchart
if condition is true then
do process
endif Condition ?
Process
24
Single – alternative selection structure (Eg.)
• Problem – guessing a number that is predefined in the program.
Pseudocode Flowchart
start
Declarations
num number
output “Enter a number:”
input number
if number == 34 then
output “You Win”
endif
stop
25
Dual – alternative selection structure
Pseudocode Flowchart
if condition is true then
do Process1
else
Condition ?
do Process2
endif
Process2 Process1
26
Dual – alternative selection structure (Eg.)
• Problem – accept two numbers and then display the maximum number.
Pseudocode Flowchart
start
Declarations
num, num1, num2
output “Enter two numbers : ”
input num1, num2
if num1>num2 then
output “Maximum number is ”,num1
else
output “Maximum number is ”,num2
endif
stop
27
Multiple – alternative selection structure
Pseudocode Flowchart
if condition1 is true then
do Statement 1
else if condition2 is true then
do Statement2
else
do Statement3
endif
28
Multiple – alternative selection structure (Eg.)
Pseudocode
start
Declarations
num age
output “Enter Age : ”
input age
if age <= 0 then
output “Wrong input”
else if age > 150 then
output “Wrong input”
else
output “Your age : ”,age
endif
Stop
30
Exercise 6
1. Draw a flowchart and write pseudocode to represent the logic
of a program that accept number from the user and then
display that number is even or odd.
The number : 13
Your entered number is odd.
2. Draw a flowchart and write pseudocode to represent the logic
of a program that accept two numbers and operator from the
user and then display the corresponding result.
First number : 23
Second number : 10
Operator : /
23 / 10 = 2.3
31
Looping
Loop structure
Pseudocode Flowchart
while condition is true
do Process
endwhile
Condition ? Process
32
Loop structure (Eg.)
• Problem – Addition of first ten numbers.
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
sum = 0
sum = sum + i
33
Loop structure (Eg.)
Pseudocode Flowchart
start
Declarations
num i = 1 start
num sum = 0
stop
while i <= 10
do
sum = sum + i
i = i + 1 step (increment)
end while
output “Sum of first ten numbers is ”,sum
stop
34
Loop structure (Eg.)
Pseudocode (using while loop) Pseudocode (using for loop)
start
start
Declarations
Declarations
num i = 1
num i
num sum = 0
num sum = 0
while i <= 10
for i=1 to 10 step 1
sum = sum + i
sum = sum + i
i = i + 1
end for
end while
output “Sum of first
output “Sum of first ten
ten numbers is ”, sum
numbers is ”, sum
stop
stop 35
Loop structure (Eg.)
• Pseudocode (using while loop) Desk - checking
Start
Declarations
num i = 1
num sum = 0
while i <= 10
sum = sum + i
i = i + 1
end while
output “Sum of first ten
numbers is ”, sum
stop 36
Problem 2
• Addition of two numbers until user wants
• Sample output is
37
Problem 2
• Pseudocode
start
Declarations
num num1, num2, ans
string choice = “yes”
while choice == “yes”
do
output “Enter two numbers : ”
input num1, num2
set ans = num1 + num2
output “Do you want to continue (yes or no) : ”
input choice
end while
output “End of the program”
Stop
38
Problem 2
• Flowchart
39
Exercise 7
1. Draw a flowchart and write pseudocode to represent the logic
of a program that accept two numbers and operator from the
user, calculate the result based on operator and then, ask the
user to continue next time to calculate or not. Sample output is
First number : 33
Second Number : 6
Operator : %
33 % 6 = 3
Continue or not (y/n) : n
2. Draw a flowchart and write pseudocode to represent the logic
of a program that ask the user to enter a number and then
calculate the factorial for that number.
3. Draw a flowchart and write pseudocode to represent the logic
of a program that ask the user to enter a number and power data
and then calculate the power of that number.
40
Chapter 3. Data Structure
41
Data Structure
• “Data” is information handled inside a computer.
• “Data structure” is how to handle the data systematically.
• In system development, a design of data structure is the foundation
of everything.
• It is necessary to examine and design data structure beforehand in
order to execute target tasks.
• Basic data structure is as follows:
(1) Variable
• “Variable” is an area to temporarily store data handled in a program.
• When a variable is defined, a variable name is given by using
alphanumeric letters and symbols in order to distinguish it from
other data.
42
Data Structure
• Also, when a variable is used, a value is assigned to the variable.
For example:
a=10
y= a + 10
44
Data Structure
(4) List
• A list is a data structure where sparse multiple data are linked
together.
• Unlike an array, data may not be stored consecutively.
• Along with data, each member of a list has information called a
“pointer” that indicates the stored location of the next member.
• When a link is switched or a new member is added, the way
members are linked can be redefined by changing a pointer.
45
Stack and Queue
• Two ways of thinking for data insertion and deletion in a list are
shown below.
(1) Stack
• “Stack” is a method to insert data into the end of a list, and to
delete data inserted last.
• It is also called a “LIFO list.” (“LIFO” is the last-in first-out
method.)
• The basic syntax of stack is as follows.
46
Stack and Queue
(2) Queue
• “Queue” is a method to insert data into the end of a list, and to
delete data inserted first.
• It is also called “scheduled queue” or “FIFO list.” (FIFO” is a
first-in first-out method.)
• The basic syntax of queue is as follows.
47
Stack and Queue
Example: As shown in the figure, boxes marked with a number are
piled up. These boxes are piled up according to Operations 1
through 4. After all operations have been performed, what is the
total when the numbers marked on all boxes are added? Note that 5
boxes are always in the piled condition, and a box can be extracted
from the bottom only. Also, when putting a new box on the top,
discard the box at the bottom.
48
Stack and Queue
49
Stack and Queue
50
Stack and Queue
51
Chapter 4. Algorithm
52
Algorithm
• “Algorithm” are processing steps to solve a problem.
• When system development or work analysis is conducted, the
algorithm is the first thing to figure out.
• By clarifying steps through an algorithm, a program can be
created efficiently.
• “Flowchart” is a diagram to graphically describe workflow or
program operations by using symbols and arrows.
• In a flowchart, a data route and control can be also shown in
addition to a program operation.
• Therefore, it is utilized as a method to visualize algorithms.
53
Algorithm
(1) Symbols in flowchart
• Symbols in a flowchart are defined by ISO (International
Standardization Organization).
• Representative symbols are as follows.
54
Algorithm
(2) Basic structures of algorithm
• There are “sequential structure,” “selection structure,” and
“repetition structure” in the basic structures of algorithm.
• By combining them, a complicated algorithm can be described.
(i) Sequential structure
• The “sequential structure” shows a flow in sequential order.
55
Algorithm
(ii) Selection structure
• The “selection structure” shows a flow to select a process on the
basis of a given condition.
56
Algorithm
Example: What does a flowchart of a date input system
considering a leap year look like? A leap year is defined as
follows: When an AD year can be divided by 4 without a
remainder, the year is a leap year. However, when the year can
be also divided by 100 without a remainder, the year is not a
leap year. When the year can be divided by 100 and 400
without a remainder, the year is a leap year.
A flowchart created on the basis of the definition on a leap
year will be as follows.
57
Algorithm
58
Algorithm
(iii) Repetition structure
• The “repetition structure” shows a flow to repeat a process while
or until a condition is satisfied, e.g. a specified number of times is
reached.
59
Algorithm
Example: When the variable a is 3 and the process shown in the
flowchart below is executed, what is the value of y?
60
Algorithm
When the process is actually executed:
[1] Assign 3 to x.
[2] 3 × 2 = 6, therefore assign 6 to x.
[3] x is 12 or less, go to
[4]. [4] 6 + 1 = 7, therefore assign 7 to x.
[2] 7 × 2 = 14, therefore assign 14 to x.
[3] x is more than 12; therefore, go to [5].
[5] 14 − 12 = 2, therefore assign 2 to y.
Then the value of y becomes 2.
61