MIS 131 - Week1 Algorithms
MIS 131 - Week1 Algorithms
2020/2021 Fall
- Chapter 1 –
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Objectives
• In this chapter, you will learn:
– Basic problem solving techniques.
– Inputs and outputs of a problem
– Concept of variables
– To be able to develop algorithms through the process of top-down,
stepwise refinement
– To be able to convert the algorithm to pseudocode and flowcharts
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.1 Introduction
• Before writing a program:
– Have a thorough understanding of the problem
– Carefully plan an approach for solving it
– The program should be carefully designed
• While writing a program:
– Know what “building blocks” are available
– Use good programming principles
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1
4
2.2 Algorithms
• Computing problems
– All can be solved by executing a series of actions in a
specific order
• Algorithm: procedure in terms of
– Actions to be executed
– The order in which these actions are to be executed
• Program control
– Specify order in which statements are to be executed
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Deck of Cards
• In a game - deck of cards
• Each player pulls a card at the top of the deck
• Read the task written on it
• Perform the task
• Then pull the next card on the top of the deck
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2
7
Example 2.1. Registering to an University
Algorithm
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Notice that
• When each step is compleated
– next step is passed
• It is also possible to branch a previous or later step
– in this example from step 6 to srep 2
• The action in each step may depend on a condition
– in step 6 if everything is OK continue otherwise branch to
step 2
• Each of these steps may consists of sub steps
– ex: step 2 fill out forms: there may be three different forms
– each of which may be a sub step of step 2
• 2a : a from for registration office
• 2b : a form for university hospital
• 2c : a form for university library
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
3
10
Note
• Without performing steps 2 and 3
• Step 4 can not be performed
• Similarly
• Without performing steps 4
• Step 5 can not be performed
• But
• Steps 2 and 3 can be interchanged
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11
Exercis 2.1
• Pick up a real life situation that interests you
• For example:
– driving a car
– cooking egg or cake
• Construct the algorithm to explain step by step
description of the actions to someone who does
not have any idea about that situation
2 minutes!
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12
2.3 Pseudocode
• Pseudocode
– Artificial, informal language that helps us develop programs
– One step closer to programming languages
– Similar to everyday English on one side and programming
languages on the other side
– Not actually executed on computers
– Helps us “think out” a program before writing it
• Easy to convert into a corresponding C/Java program
• Consists only of executable statements
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
4
13
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
14
Note That
• In the algorithm: in step 2
– Getting the height from the user
• In the pseudocode
– first write a message to be displayed in the screen informing
the user what to enter as an input
– Then let the user enter a real number and press ENTER key
– The number entered by the user is stored in the variable
heigth
• Similarly for getting width of the rectangle – in
step 3
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5
16
Building Blocks
• Input output transfer
• Calculations in between - processing
• Decision making
• Iteration
• Variables
– Store data in memory
– Different types in different languages
– Integers, real, character, string, logical,...
• Input: obtain a value from the user and store in a variable
– Enter a value for radius of a circle to compute its area
– Enter your name lastname id number
– Make a choice Yes/No to save or not
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
17
Variables
– Store data in memory
– Different types in different languages
– Integers, real, character, string, logical (true or false) ...
• Integer: Holds whole numbers (i.e., 3, 25, 1000, etc.)
• Real: Holds either whole numbers or numbers with a fractional
part (i.e., 3.5, 0.4, 1000.01, etc.)
• String: Holds any string of characters (i.e., someone’s name,
address, etc.)
• Numerical variables
– heigth, width, age, totalSales, numer of children
• Strings:
– Name, last name, address, e-mail
– “Nazım”, “Taşkın”, “[email protected]”
– usually presented in quats “.....”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
18
6
19
Output
• Output: Display the results of the problem
– Some variables
• display area
– Constant messages
• Display “welcome to Java”
• Display “you failed to register”
– Expressions
• Display pi*r2
– or a mixture of them
• Display “the area is:”, area
– E.g.: displays when the value of area is 200
– the area is: 200
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
20
Calculations - Processing
• Calculations
• Aritmetic and logical computations
• Intermediate results are also in variables
• E.g.:
– Set area = height*width
• area is the product of hight and width
– E.g.:
• Set valueAddedTax = taxRate*basePayment
– E.g.:
– Set total = total + new grade
• add new grade to the total
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
21
7
22
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
23
2.5 Flowcharts
• Flowchart
– Graphical representation of an algorithm
– Drawn using certain special-purpose symbols connected by
arrows called flowlines
– Rectangle symbol (action symbol):
• Indicates any type of action
– Oval symbol:
• Indicates the beginning or end of a program or a section of code
• Single-entry/single-exit control structures
– Connect exit point of one control structure to entry point of
the next (control-structure stacking)
– Makes programs easy to build
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
24
Flowchart Symbols
start or end
calculation
flow
decision making
output input
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8
25
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
26
27
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9
28
Pseudocode
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
29
Variables
• forexRate
– a real number set at the begining
– fixed; it does not change
• dollar
– dollar in the hand of the user
– real input
– every run of the program has a different value
• tl
– calculated TL amout for the dollar with the fixed forexRate
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
30
10
31
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
32
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
33
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11
34
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
35
36
12
37
Algorithm
• 1 Start
• 2 Ask and get name, last name and age of a person
• 2a Ask and get name
• 2b Ask and get last name
• 2c Ask and get age
• 3 Dispay personal information in a formated way
to the screen
• 3a Display name
• 3b Display last name
• 3c Display age
• 4 Stop
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
38
Pseudocode
• 1 Start
• 2a i Display “Enter your name”
• 2a ii Input name
• 2b i Display “Enter your last name”
• 2b ii Input lastName
• 2c i Display “Enter your age”
• 2c ii Input age
• 3a Display “Name: ” , name
• 3b Display “Last Name:”, lastName
• 3c Display “Age:”, age
• 4 Stop
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
39
Variables
• name, lastName
– String – set of characters
• age
– integer - your age
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
13
40
Flowchart
• Draw the flowchart
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
41
• Outputs:
– avgerage, min, max, letter grades, ...
• Limits:
– only for one exam
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
14
43
Algorithm
• A class consisting of three students: Ali,Ahmet, Nazlı
1 Start
2 Get Ali’s grade
3 Get Ahmet’s grade
4 Get Nazlı’s grade
5 Compute the average grade
Average grade = (Ali’s grade + Ahmet’s grade + Nazlı’s
grade)/3
6 Display the average grade
7 End
• Variables:
– Ali’s grade, Ahmet’s grade, Nazlı’s grade,
– average grade
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Variables
alisGrade, ahmetsGrede and nazlisGrade are declared as
integers
average is declared as Real
Note that: if this pseudocode is implemented in Java or C
programming languages sum of three integers is an
integer dividing by 3; you may lose the remainder
because of integer division
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15
Solution
• Declare the student grades as Real variables
average = (10.0 + 9.0 + 9.0) / 3
here average is 9.33333
• if grades are declared as integers
average = (10 + 9 + 9) / 3
here average is 9.00000
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
48
Ex 2.10. Purchase
• What is total payment after a sale?
• Inputs:
– products SKU (Stock Keeping Unit) and amounts
• Outut:
– invoice total payment with value added tax
• Note prices of goods are not inputs
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
16
49
50
51
Pseudocode Flowchart
• Exercise to you
• Note if you are sure that value of a variable will
not change through out the program, you can
declare them a constant.
• Many programming languages including Java has
a syntax for constants
• Any attempt to change the value of a constant is a
syntax error
• Examples of well known constants
pi = 3.1416, e = 2.713
speed of light, Avagadro number
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
17
Constants in Pseudocodes
• Constant Integer priceApple = 4
• Constant Real Pi = 3.14
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
53
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
54
Ex 2.11.Tax Calculation
• Compute the tax burden
• Inputs:
– yearly income
• Output:
– tax burden
• Note: tax rate is given
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
18
55
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
56
Exercise
• Suppose a language does not support mode
• In Java mode is implemented by %
– 14 % 3 = 2
– 12 % 3 = 0
• How do you imlement mode if it is not supported
by the language syntax
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
57
Solution
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
19
Ex 2.13 ATM Problem
• Consider a customer intending to withdraw money from an
ATM, enters the amount s/he wants to withdrow
• available types of notes are say:
– 100, 50, 20, 10...
– assume ATM pays as much 100 as possible then pays the
remaining with 50s and so on
• Write the algorithm of money withdrawing problem. How
much of the money is paid with what type of notes and
remainging money that can not be paid at all
• E.g.: 643 TL is paid with
– 6x100s, 0x50s, 2x20s, and 3 TL can not be paid
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
60
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
20
61
ATM: Algorithm
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
62
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
63
Exercise
• Complete the pseudocode
• Draw the flowchart
• -- Write the Java program
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
21
64
65
66
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
22
67
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
68
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
69
Algorithm 1
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
23
70
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
71
• Example:
– number is 743
– remaining is 743
– ones is 743 mode 10 = 3
– remaining is 743 / 10 74
• perform step 4 amd 5 to find tens and hudereds
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
72
Algorithm 2
• 1 Start
• 2 Get number from user
• 3 Determine hundrends
• 4 Determine tens
• 5 Determine ones
• 6 Compute reverse as
– invnumber = 100*hundreds+10*tens+ones
• 7 Display reverse
• 8 End
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
24
73
Algorithm 2
• Set remaining = number // copy of money
• 3 Determine hundreds:
3a Set hundreds = remaining / 100
3b Set remaining = remaining mode 100
• Example:
– number is 743
– remaing is 743
– hundreds is 743 / 100 = 7
– remaining is 743 mode 100 = 43
• Perform step 4 and 5 to find tens and ones
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
74
Algorithm 3
• 1 Start
• 2 Initialize reverse to zero
• 3 Get number from user
• 4a Determine ones
• 4b Update remainder
• 4c Update reverse
• 5a Determine tens
• 5b Update remainder
• 5c Update reverse
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
75
Algorithm 3 (contg.)
• 6a Determine hundrends
• 6b Update remainder
• 6c Update reverse
• 7 Print reverse
• 8 End
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
25
76
Hearth of Algorithm 3
• Initially reverse variable is set to 0
• 4a Determine ones:
• Set ones = remaining mode 10
• 4b update remainder
• Set remaining = remaining / 10
• 4c updata reverse
• Set reverse = 10*reverse + ones // inverse
becomes ones
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
77
Hearth of Algorithm 3
• Just before 5 inverse is ones
• 5a determine tens:
• Set tens = remaining mode 10
• 5b update remainder
• Set remaining = remaining / 10
• 5c updata reverse
• Set reverse = 10*reverse + tems // inverse
becomes left digit:onces, right digit: tens
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
26