You are on page 1of 196

DR ATIF SHAHZAD

IE-321

Fundamentals of
Dr. Atif Shahzad

LECTURE #13
9/27/2018
Computer Systems 1
What We have seen
Logic
Memory
Memory Cell
Logical variables CPU
Conditional, Negation, Contrapositive,Biconditional Register
AND, OR,NOT,XOR Program Counter
Logic gates Fetch-Execute Cycle
Truth Tables Q&A
Spreadsheet Concepts:
Boolean Algebra File Management
Using Microsoft Excel
Examples Word Processing Basics Using Microsoft Word
Database Creating Charts in Microsoft Excel
Q&A Microsoft Word Layout and Graphics Features
Flat & Relational Debugging Concepts Using Microsoft Excel
Database Making and using compressed Files
Presentation Concepts Using Microsoft PowerPoint
DBMS WinZip, 7Zip
Image Concepts
Tables & Fields Notepad++
Creating Tables in Wordpad
Access
Design view and Data Adobe acrobat
Sheet View Sumatra PDF
Primary Key & MathType
Foreign key
Relationships & ER
Diagram
Cyber Security
Queries &Simple SQL
Microsoft Visio
Security Problems
Interception
Spoofing
Microsoft Project Falsification
Repudiation
Dr. Atif Shahzad

Security Technologies
Encryption
MAC

9/27/2018 2
TODAY
Data Model
Algorithm
Ingredients
Variables
Constructs
Pseudocode
Flowchart
Symbols
Flowcharting examples
Dr. Atif Shahzad

Q&A
9/27/2018 3
Fundamentals of
Computer Systems

IE321
Dr. Atif Shahzad

9/27/2018 4
Data Processor Model

I really hate this damned machine


Dr. Atif Shahzad

I wish that they would sell it.


It never does quite what I want
But only what I tell it.
9/27/2018 ~Anon 5
Programmable
Data Processor Model
Dr. Atif Shahzad

9/27/2018 6
Dr. Atif Shahzad

9/27/2018
7
Same Program,
Figure 1-3
Different
Same program, differentData
data
Dr. Atif Shahzad

9/27/2018 8
Same Data, Different Program
Figure 1-4

Same data,
different
programs
Dr. Atif Shahzad

9/27/2018 9
von Neumann Model
Figure 1-5
von Neumann model
Dr. Atif Shahzad

9/27/2018 10
Program and Data
Figure 1-6
Program andin Memory
data in memory
Dr. Atif Shahzad

9/27/2018 11
Program instructions
Figure 1-7

Program made of instructions


Dr. Atif Shahzad

9/27/2018 12
A day in the life of a computer
How computers carry out
program instructions based
on algorithms
Dr. Atif Shahzad

9/27/2018 13
Algorithm
Dr. Atif Shahzad

9/27/2018 14
How is algorithm organized?

Title
Dr. Atif Shahzad

9/27/2018
9/27/2018 15
How is algorithm organized?

Title
Ingredients
Steps
Exceptions
Servings
Dr. Atif Shahzad

16
9/27/2018 16
How is algorithm organized?

Title
Ingredients
Steps
Exceptions
When to stop
Servings
Dr. Atif Shahzad

D.A. Clements, UW Inform 17


9/27/2018 ation School 17
How is algorithm organized?

Title
Ingredients
Steps
Servings
Dr. Atif Shahzad

9/27/2018
9/27/2018 18
How is algorithm organized?
Dr. Atif Shahzad

9/27/2018 19
Five Ingredients of Algorithm
Input specified • Data to be transformed during the computation to produce the output
• Must specify type, amount, and form of data

Output specified • Data resulting from the computation—intended result


• It is possible to have no output

Definiteness • Specify the sequence of events


• Details of each step, including how to handle errors

Effectiveness • The operations are do-able

Finiteness • Must eventually stop


Dr. Atif Shahzad

9/27/2018 20
Computational Problem

A computational problem specifies an input-


output relationship
What does the input look like?
What should the output be for each input?
Dr. Atif Shahzad

9/27/2018 21
Computational Problem: Exps

Example 1:
Input: A list of names of people
Output: The same list sorted alphabetically
Example 2:
Input: A picture in digital format
Output: An English description of what the
picture shows
Dr. Atif Shahzad

9/27/2018 22
Algorithm

An algorithm is an exact specification


of how to solve a computational
problem

There can be many different algorithms


for each computational problem.
Dr. Atif Shahzad

9/27/2018 23
Designing Algorithm

Understand your
There is no single
problem well – may
recipe for inventing
require much
algorithms
mathematical analysis!

Use existing
algorithms
(reduction) or
algorithmic ideas
Dr. Atif Shahzad

9/27/2018 24
What is the following ALGORITHM
used for ?

WHAT SUM WILL BE PRINTED


IF N = 2 ?
Dr. Atif Shahzad

9/27/2018 25
Algorithms and Variables

X is the
variable

• X = 50
Dr. Atif Shahzad

9/27/2018
• X=X + 5 26
N=5

0
SUM

5
n 0
count
Dr. Atif Shahzad

9/27/2018 27
N=5

5
SUM

5
n 1
count
Dr. Atif Shahzad

9/27/2018 28
N=5

10
SUM

5
n 2
count
Dr. Atif Shahzad

9/27/2018 29
N=5

15
SUM

5
n 3
count
Dr. Atif Shahzad

9/27/2018 30
N=5

20
SUM

5
n 4
count
Dr. Atif Shahzad

9/27/2018 31
N=5
How many instructions
were processed?

25

25
SUM

5
n 5
count
Dr. Atif Shahzad

9/27/2018 32
Task: Complete the worksheet
using the algorithm.
Fundamentals of Computer –IE321
Name:Student Name KAUID:########

Complete the table using the algorithm. The first one is done for you.
n sum count Output Time taken
20 0 0
20 20 1
20 40 2
20 60 3 60 1 minute
2

24

2.5

500

15
Dr. Atif Shahzad

9/27/2018 33
Pseudo Code

aka Structured English is an internal design


that uses

A limited set of words consistently throughout the


design

Indentation to indicate selection and iteration


• More on those later

Goes into greater detail than the algorithm


Dr. Atif Shahzad

9/27/2018 34
Pseudo Code
Notice several standards within the pseudo code
Commands (Display, Input) begin with a capital letter
Makes them easy to distinguish

Same commands used consistently


We don’t use:Show msg or Print msg we always use Display

Static text enclosed in double quotes


Dr. Atif Shahzad

9/27/2018 35
Pseudo Code
By following standards, the pseudocode is
Easier to understand
Consistency makes things easier to recognize
In real world, traffic light always has
Red light on top
Green light on bottom

Created faster
Designer doesn’t have to think up
words/commands to use
Dr. Atif Shahzad

9/27/2018 36
Flow Charts (FC)
Are internal design diagrams that use
Symbols/Shapes to represent the type of
operation to be preformed
Sequential process
Input/output
Decision

Text that further defines the exact operation to be


performed
Arrows to show the order in which the
Dr. Atif Shahzad

operations must be performed

9/27/2018 37
Flow Charts (FC)
Begin and end with an oval with the
text Start and Stop

Start

Other symbols

Stop
Dr. Atif Shahzad

9/27/2018 38
Flow Charts (FC)
Input and output operations indicated
with a parallelogram

Display
“What’s your
name?”
Sequential processes indicated with a
rectangle

msg = “Hi ”, name


Dr. Atif Shahzad

9/27/2018 39
Flow Charts (FC) Start

So the FC to define the Display


program would be: “What’s your
name?”

Input name

msg = “Hi ”, name

Display msg
Dr. Atif Shahzad

Stop
9/27/2018 40
Flowchart Symbols
Name Symbol Basic
Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation


Dr. Atif Shahzad

Flow line Denotes the direction of logic flow in the program

9/27/2018 41
Figure 8-6
Three constructs
Dr. Atif Shahzad

9/27/2018 42
Algorithm Representation
Flowcharts for three constructs
Dr. Atif Shahzad

9/27/2018 43
Figure 8-8
Pseudocode for three constructs
Dr. Atif Shahzad

9/27/2018 44
Figure 8-2
Finding the largest integer
among five integers
Dr. Atif Shahzad

9/27/2018 45
Figure 8-3
Defining actions in FindLargest algorithm
Dr. Atif Shahzad

9/27/2018 46
Figure 8-4
FindLargest refined
Dr. Atif Shahzad

9/27/2018 47
Example

START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE ← (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE←(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<50

PRINT PRINT
“PASS” “FAIL”
Dr. Atif Shahzad

STOP

9/27/2018 48
Example 2
Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying
LFT with 30
Print length in cm (LCM)
Dr. Atif Shahzad

9/27/2018 49
Example 2
Algorithm Flowchart
Step 1: Input Lft START

Step 2: Lcm ← Lft x 30 Input

Step 3: Print Lcm Lft

Lcm ← Lft x 30

Print
Lcm
Dr. Atif Shahzad

STOP

9/27/2018 50
Example 3
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudocode
Input the width (W) and Length (L) of a rectangle
Calculate the area (A) by multiplying L with W
Print A
Dr. Atif Shahzad

9/27/2018 51
Example 3

Algorithm START

Step 1: Input W,L Input


W, L
Step 2: A ← L x W
Step 3: Print A A ← L xW

Print
A
Dr. Atif Shahzad

STOP

9/27/2018 52
Example 4
Write an algorithm and draw a flowchart that will
calculate the roots of a quadratic equation
ax 2 + bx + c =0
Hint: d = sqrt ( b 2 − 4ac),
and the roots are:
x1 = (–b + d)/2a and
x2 = (–b – d)/2a
Dr. Atif Shahzad

9/27/2018 53
Example 4
Pseudocode:
Input the coefficients (a, b, c) of the quadratic
equation
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
Dr. Atif Shahzad

9/27/2018 54
Example 4
START

Algorithm:
Input
Step 1: Input a, b, c a, b, c
Step 2: d ← sqrt ( b × b − 4 × a)× c
Step 3: x1 ← (–b + d) / (2 x a) d ← sqrt(b x b – 4 x a x c)

Step 4: x2 ← (–b – d) / (2 x a)
x1 ←(–b + d) / (2 x a)
Step 5: Print x1, x2
X2 ← (–b – d) / (2 x a)

Print
x1 ,x2
Dr. Atif Shahzad

STOP

9/27/2018 55
DECISION STRUCTURES
The expression A>B is a logical expression
it describes a condition we want to test
if A>B is true (if A is greater than B)we take the
action on left
print the value of A
if A>B is false (if A is not greater than B) we take
the action on right
print the value of B
Dr. Atif Shahzad

9/27/2018 56
DECISION STRUCTURES

Y N
is
A>B

Print A Print B
Dr. Atif Shahzad

9/27/2018 57
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
Dr. Atif Shahzad

9/27/2018 58
IF–THEN–ELSE STRUCTURE
The algorithm for the flowchart is as follows:
If A>B then
print A
else
Y N
print B is
A>B
endif
Print A Print B
Dr. Atif Shahzad

9/27/2018 59
Relational Operators

Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
≥ Greater than or equal to
≤ Less than or equal to
≠ Not equal to
Dr. Atif Shahzad

9/27/2018 60
Example 5
Write an algorithm that reads two values, determines the
largest value and prints the largest value with an identifying
message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX ←VALUE1
else
MAX ←VALUE2
endif
Step 3: Print “The largest value is”, MAX
Dr. Atif Shahzad

9/27/2018 61
Example 5
START

Input
VALUE1,VALUE2

Y is
N
VALUE1>VALUE2

MAX ← VALUE1 MAX ← VALUE2

Print
“The largest value is”, MAX
Dr. Atif Shahzad

STOP
9/27/2018 62
NESTED IFS
One of the alternatives within an IF–THEN–
ELSE statement
may involve further IF–THEN–ELSE statement
Dr. Atif Shahzad

9/27/2018 63
Example 6
Write an algorithm that reads three numbers
and prints the value of the largest number.
Dr. Atif Shahzad

9/27/2018 64
Example 6
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX ← N1 [N1>N2, N1>N3]
else
MAX ← N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX ← N2 [N2>N1, N2>N3]
else
MAX ← N3 [N3>N2>N1]
endif
endif
Dr. Atif Shahzad

Step 3: Print “The largest number is”, MAX

9/27/2018 65
Example 6
Flowchart: Draw the flowchart of the
above Algorithm.
Dr. Atif Shahzad

9/27/2018 66
Example 7

Write and algorithm and draw a flowchart to


a) read an employee name (NAME), overtime hours
worked (OVERTIME), hours absent (ABSENT)
and
b) determine the bonus payment (PAYMENT).
Dr. Atif Shahzad

9/27/2018 67
Example 7
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but ≤ 40 hours $40
>20 but ≤ 30 hours $30
>10 but ≤ 20 hours $20
≤ 10 hours $10
Dr. Atif Shahzad

9/27/2018 68
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT ← 50
else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT ← 40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT ← 30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT ←20
else
PAYMENT ← 10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
Dr. Atif Shahzad

9/27/2018 69
Example 7
Flowchart: Draw the flowchart of the
above algorithm?
Dr. Atif Shahzad

9/27/2018 70
Dr. Atif Shahzad

9/27/2018
71
NEVER hesitate to
contact should you
have any question
Dr. Atif Shahzad
Dr. Atif Shahzad

9/27/2018 72
Flow Charts Tools
Help the designer generate the diagrams

GUI to drag and drop symbols


Automatically generate start/stop symbols
Based on FC, may create pseudo code or
source code
Dr. Atif Shahzad

9/27/2018 73
Flow Charts Tools
Structured Flow Chart
editor (SFC) is free
software to create and
modify flow charts

Flowgorithm and
Raptor are flow chart
editors and executers
Dr. Atif Shahzad

9/27/2018 74
Welcome window displayed
Dr. Atif Shahzad

Click OK button
9/27/2018 75
Click File, New and enter a title for diagram (Exa
mple), your name, click OK
Dr. Atif Shahzad

9/27/2018 76
Lots of options

This is where the flow This is where the


chart is defined and d pseudocode is dis
isplayed played
Dr. Atif Shahzad

9/27/2018 77
Dr. Atif Shahzad

To insert into diagram, click on location (a circle b


etween symbols) to insert then, Edit, Insert
9/27/2018 78
Dr. Atif Shahzad

9/27/2018
]

79
Often further options
You must be consistent!

Here’s a link to a video (6 mins) on using SFC


Dr. Atif Shahzad

http://web.fscj.edu/Janson/COP1000/FirstProgram.mov
9/27/2018 80
81

Flowgorithm Flowcharts
 Doesn't have modules, has functions

 Creating the main method is standard


 Create new flow chart (File, New)
 Flowgorithm puts “”Main” text in Start oval and
"End" text in the Finish oval

 Add I/O symbols and text to display


then double click on arrow between I/O
symbols…
Dr. Atif Shahzad

9/27/2018 81
82

 … then click Call symbol


Dr. Atif Shahzad

9/27/2018 82
Module Call
main() showCustAddress()

Display “Howdy” Display “Joe Custo


mer”

showCustAddress()
Display “1 Main Str
eet
Display “See ya”

Display “Enid OK,


56565”
End

Return
Dr. Atif Shahzad

83
9/27/2018 83
Flowgorithm

Function name

The return value type

The variable/value to return


Dr. Atif Shahzad

9/27/2018 84
Then add function s
tatements

Flowgorithm
Dr. Atif Shahzad

9/27/2018 85
Flowgorithm
Finished function
Dr. Atif Shahzad

9/27/2018 86
Flowgorithm

When run
Dr. Atif Shahzad

9/27/2018 87
88

Flowgorithm Method Call


 In flowchart, double click Call symbol and
enter name of method followed by ()
Dr. Atif Shahzad

9/27/2018 88
89

Flowgorithm Function Definition


 Click Add Function button

 Specify Func
tion name
Dr. Atif Shahzad

9/27/2018 89
90

Flowgorithm Method Definition

 showCustAddress FC
created and displayed

 Simply add symbols to


showCustAddress

 To display Functions cl
ick drop down arrow a
nd select
Dr. Atif Shahzad

9/27/2018 90
Flowgorithm Method Definition
91

 Run
Dr. Atif Shahzad

9/27/2018 91
Practice Problem
Display
“What’s your name?”
Create the flowchart on the left
using SFC and call it Name

Input name

msg = “Hi ”, name


Dr. Atif Shahzad

Display msg

9/27/2018 92
Flow Charts (FC)
Sample 1: Write a program that calculates the
sum of two input numbers and display the result.
Sample 2: Write a program to calculate the area
of a circle and display the result. Use the
formula: A=πr 2 where Pi is approximately equal
to 3.1416.
Sample 3: Write a program that computes the
average of three input quizzes, and then display
the result.
Dr. Atif Shahzad

9/27/2018 93
Flow Charts (FC)
Sample 4: Write a program that converts the
input Fahrenheit degree into its Celsius degree
equivalent. Use the formula: C= (5/9)*F-32.
Sample 5: Create a program to compute the
volume of a sphere. Use the formula: V= (4/3)*
πr 3 where is pi equal to 3.1416 approximately.
The r 3 is the radius. Display result.
Sample 6: Write a program that converts the
input Celsius degree into its
equivalent Fahrenheit degree. Use the formula:
Dr. Atif Shahzad

F= (9/5) * C+32.
9/27/2018 94
User Interface

Graphical User Interfaces (GUI)


• Which you are all familiar with
• Use mouse to tell program what to do
• Click buttons, choose list options, enter data
into text boxes

Command Line Interface (CLI)


• Enter English-like commands and data to tell
the computer to do stuff
Dr. Atif Shahzad

9/27/2018 95
Windows Command Prompt

Run the application program (java OilCalc)


Enter info at command prompt (250), press the Enter key
Program displays the result
Dr. Atif Shahzad

9/27/2018 96
GUI GUI Input
Run the program
Enter info, click Calc button
Program displays the result
Dr. Atif Shahzad

9/27/2018 97
Example 1

Write an algorithm in pseudocode that


finds the average of two numbers
Dr. Atif Shahzad

9/27/2018 98
Solution
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Dr. Atif Shahzad

9/27/2018 99
Example 2

Write an algorithm to change a numeric


grade to a pass/no pass grade.
Dr. Atif Shahzad

9/27/2018 100
Solution

Pass/NoPassGrade
Input: One number
1. if (the number is greater than or equal to 60)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “nopass”
End if
2. Return the grade
End
Dr. Atif Shahzad

9/27/2018 101
Example 3

Write an algorithm to change a numeric


grade to a letter grade.
Dr. Atif Shahzad

9/27/2018 102
Solution

LetterGrade
Input: One number
1. if (the number is between 90 and 100, inclusiv
e)
then
1.1 Set the grade to “A”
End if
2. if (the number is between 80 and 89, inclusive)
then
2.1 Set the grade to “B”
End if
Dr. Atif Shahzad

Continues on the next slide

9/27/2018 103
Letter grade (continued)
3. if (the number is between 70 and 79, inclusive)
then
3.1 Set the grade to “C”
End if
4. if (the number is between 60 and 69, inclusive)
then
4.1 Set the grade to “D”
End if
Continues on the next slide
Dr. Atif Shahzad

9/27/2018 104
Letter grade (continued)
5. If (the number is less than 60)
then
5.1 Set the grade to “F”
End if
6. Return the grade
End
Dr. Atif Shahzad

9/27/2018 105
Example 4

Write an algorithm to find the largest of a


set of numbers. You do not know the total
number of numbers.
Dr. Atif Shahzad

9/27/2018 106
Solution Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 if (the integer is greater than Largest)
then
2.1.1 Set largest to the value of the integer
End if
End while
3. Return Largest
End
Dr. Atif Shahzad

9/27/2018 107
Example 5

Write an algorithm to find the largest of 100


0 numbers.

Solution

See Algorithm 8.5 on the next slide.


Dr. Atif Shahzad

9/27/2018 108
Algorithm 8.5: Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
Dr. Atif Shahzad

End

9/27/2018 109
8.4
MORE FORMA DEFINITION
•Ordered set
•Unambiguous steps
•Effectiveness
•Termination
Dr. Atif Shahzad

9/27/2018 110
8.5

SUBALGORITHMS
Dr. Atif Shahzad

9/27/2018 111
Figure 8-9
Concept of a subalgorithm
Dr. Atif Shahzad

9/27/2018 112
Algorithm 8.6: Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 FindLarger
End while
3. Return Largest
End
Dr. Atif Shahzad

9/27/2018 113
Subalgorithm: Find larger
FindLarger
Input: Largest and current integer
1. if (the integer is greater than Largest)
then
1.1 Set Largest to the value of the integer
End if
End
Dr. Atif Shahzad

9/27/2018 114
8.6

BASIC
ALGORITHMS
Dr. Atif Shahzad

9/27/2018 115
Figure 8-10
Summation
Dr. Atif Shahzad

9/27/2018 116
Figure 8-11
Product
Dr. Atif Shahzad

9/27/2018 117
Figure 8-12
Selection sort
Dr. Atif Shahzad

9/27/2018 118
Figure 8-13: part I
Example of selection sort
Dr. Atif Shahzad

9/27/2018 119
Figure 8-13: part II
Example of selection sort
Dr. Atif Shahzad

9/27/2018 120
Figure 8-14
Selection sort
algorithm
Dr. Atif Shahzad

9/27/2018 121
Figure 8-15
Bubble sort
Dr. Atif Shahzad

9/27/2018 122
Figure 8-16: part I
Example of bubble sort
Dr. Atif Shahzad

9/27/2018 123
Figure 8-16: part II
Example of bubble sort
Dr. Atif Shahzad

9/27/2018 124
Figure 8-17
Insertion sort
Dr. Atif Shahzad

9/27/2018 125
Figure 8-18: part I
Example of insertion sort
Dr. Atif Shahzad

9/27/2018 126
Figure 8-18: part II
Example of insertion sort
Dr. Atif Shahzad

9/27/2018 127
Figure 8-19
Search concept
Dr. Atif Shahzad

9/27/2018 128
Figure 8-20: Part I
Example of a sequential sort
Dr. Atif Shahzad

9/27/2018 129
Figure 8-20: Part II
Example of a sequential sort
Dr. Atif Shahzad

9/27/2018 130
Figure 8-21
Dr. Atif Shahzad
Example of a binary sort

9/27/2018 131
8.1

RECURSION
Dr. Atif Shahzad

9/27/2018 132
Figure 8-22
Iterative definition of factorial
Dr. Atif Shahzad

9/27/2018 133
Figure 8-23

Recursive definition of factorial


Dr. Atif Shahzad

9/27/2018 134
Figure 8-24

Tracing recursive solution to factorial problem


Dr. Atif Shahzad

9/27/2018 135
Algorithm 8.7: Iterative factorial
Factorial
Input: A positive integer num
1. Set FactN to 1
2. Set i to 1
3. while (i is less than or equal to num)
3.1 Set FactN to FactN x I
3.2 Increment i
End while
4. Return FactN
End
Dr. Atif Shahzad

9/27/2018 136
Algorithm 8.8: Recursive factorial
Factorial
Input: A positive integer num
1. if (num is equal to 0)
then
1.1 return 1
else
1.2 return num x Factorial (num – 1)
End if
End
Dr. Atif Shahzad

9/27/2018 137
Dr. Atif Shahzad

9/27/2018
138
Chapter 9

Programming
Languages
Dr. Atif Shahzad

9/27/2018 139
OBJECTIVES
After reading this chapter, the reader should
be able to:
Have a vision of computer language evolution.

Distinguish between machine, assembly, and high-level


languages.

Understand the process of creating and running a program.

Distinguish between the different categories of languages:


procedural, object-oriented, functional, declarative, and special.
Dr. Atif Shahzad

Become familiar with elements of the procedural language C.

9/27/2018 140
9.1

EVOLUTION
Dr. Atif Shahzad

9/27/2018 141
Figure 9-1
Evolution of computer languages
Dr. Atif Shahzad

9/27/2018 142
Program 9.1 Program in machine language
1 00000000 00000100 0000000000000000
2 01011110 00001100110000100000000000000010
3 11101111 000101100000000000000101
4 11101111 10011110 0000000000001011
5 11111000 10101101 11011111 0000000000010010
6 0110001011011111 0000000000010101
7 11101111 00000010 11111011 0000000000010111
8 11110100 1010110111011111 0000000000011110
9 0000001110100010 11011111 0000000000100001
10 11101111 00000010 11111011 0000000000100100
11 01111110 11110100 10101101
12 11111000 10101110 110001010000000000101011
13 0000011010100010 11111011 0000000000110001
14 11101111 00000010 11111011 0000000000110100
15 00000100 0000000000111101
16 00000100 0000000000111101
Dr. Atif Shahzad

9/27/2018 143
Note:

The only language understood by


a computer is machine language.
Dr. Atif Shahzad

9/27/2018 144
Program 9.2 Program in symbolic language
1 Entry main, ^m<r2>
2 subl2 #12,sp
3 jsb C$MAIN_ARGS
4 movab $CHAR_STRING_CON
5
6 pushal -8(fp)
7 pushal (r2)
8 calls #2,read
9 pushal -12(fp)
10 pushal 3(r2)
11 calls #2,read
12 mull3 -8(fp),-12(fp),-
13 pushal 6(r2)
14 calls #2,print
15 clrl r0
16 ret
Dr. Atif Shahzad

9/27/2018 145
Program 9.3 Program in C++ language
1 /* This program reads two integer numbers from the
2 keyboard and prints their product.
3 */
4 #include <iostream.h>
5
6 int main (void)
7 {
8 // Local Declarations
9 int number1;
10 int number2;
11 int result;
12 // Statements
13 cin >> number1;
14 cin >> number2;
15 result = number1 * number2;
16 cout << result;
17 return 0;
18 } // main
Dr. Atif Shahzad

9/27/2018 146
9.2
BUILDING
A
PROGRAM
Dr. Atif Shahzad

9/27/2018 147
Figure 9-2
Building a program
Dr. Atif Shahzad

9/27/2018 148
9.3
PROGRAM
EXECUTION
Dr. Atif Shahzad

9/27/2018 149
Figure 9-3
Program execution
Dr. Atif Shahzad

9/27/2018 150
9.4
CATEGORIES
OF
LANGUAGES
Dr. Atif Shahzad

9/27/2018 151
Figure 9-4
Categories of languages
Dr. Atif Shahzad

9/27/2018 152
Figure 9-5

Function in a functional language


Dr. Atif Shahzad

9/27/2018 153
Figure 9-6

Extracting the third element of a list


Dr. Atif Shahzad

9/27/2018 154
Table 9.1 Common tags

Beginning Tag Ending Tag Meaning


---------------- ---------------- ----------------------------
<HTML> </HTML> document
<HEAD> </HEAD> document head
<BODY> </BODY> document body
<TITLE> </TITLE> document title
<Hi> </Hi> different header levels
<B> </B> boldface
<I> </I> Italic
<U> </U> underlined
<SUB> </SUB> subscript
<SUP> </SUP> superscript
<CENTER> </CENTER> centered
<BR> line break
<OL> </OL> ordered list
<UL> </UL> unordered list
<LI> </LI> an item in the list
<IMG> an image
<A> </A> an address (hyperlink)
Dr. Atif Shahzad

9/27/2018 155
Program 9.4 HTML Program
<HTML>
<HEAD>
<TITLE> Sample Document </TITLE>
</HEAD>
<BODY>
This is the picture of a book:
<IMG SRC="Pictures/book1.gif" ALIGN=MIDDLE>
</BODY>
</HTML>
Dr. Atif Shahzad

9/27/2018 156
9.5
A PROCEDURAL
LANGUAGE:
C
Dr. Atif Shahzad

9/27/2018 157
Figure 9-7
Variables
Dr. Atif Shahzad

9/27/2018 158
Table 9.2 Arithmetic operators

Operator Definition Example


---------------- ---------------- ----------------------
+ Addition 3 + 5
− Subtraction 2 − 4
∗ Multiplication Num * 5
/ Division (quotient) Sum / Count
% Division (remainder) Count % 4
−−−−−−−−−− ----------------------- -----------------------
++ Increment Count ++
−− Decrement Count −−
Dr. Atif Shahzad

9/27/2018 159
Table 9.3 Relational operators

Operator Definition Example


---------------- ---------------- ----------------------
< Less than Num1 < 5
<= Less than or equal to Num1 <= 5
> Greater than Num2 > 3
>= Greater than or equal to Num2 >= 3
== Equal to Num1 == Num2
!= Not equal to Num1 != Num2
Dr. Atif Shahzad

9/27/2018 160
Table 9.4 Logical operators

Operator Definition Example


---------------- ---------------- ----------------------
! NOT ! ( Num1 < Num2 )
&& AND (Num1 < 5 ) && (Num2 > 10 )
|| OR (Num1 < 5 ) || (Num2 > 10 )
Dr. Atif Shahzad

9/27/2018 161
Table 9.5 Assignment operators

Operator Example Meaning


---------------- ---------------- ----------------------
== Num =5 Store 5 in Num
+= Num += 5 Num = Num + 5
−= Num −= 5 Num = Num − 5
*= Num *= 5 Num = Num * 5
/= Num /= 5 Num = Num / 5
%= Num %= 5 Num = Num % 5
Dr. Atif Shahzad

9/27/2018 162
Figure 9-8

Statements
Dr. Atif Shahzad

9/27/2018 163
Figure 9-9
Side effect of a function
Dr. Atif Shahzad

9/27/2018 164
Figure 9-10
Dr. Atif Shahzad
Function declaration

9/27/2018 165
Figure 9-11
if-else statement
Dr. Atif Shahzad

9/27/2018 166
Figure 9-12
Dr. Atif Shahzad
switch statement

9/27/2018 167
Figure 9-13
Dr. Atif Shahzad
while loop

9/27/2018 168
Figure 9-14
Dr. Atif Shahzad
for loop

9/27/2018 169
Figure 9-15
Dr. Atif Shahzad
do-while loop

9/27/2018 170
Dr. Atif Shahzad

9/27/2018
171
Dr. Atif Shahzad

9/27/2018
172
Dr. Atif Shahzad

9/27/2018
173
Dr. Atif Shahzad

9/27/2018
174
Dr. Atif Shahzad

9/27/2018
175
Dr. Atif Shahzad

9/27/2018
176
Dr. Atif Shahzad

9/27/2018
177
Dr. Atif Shahzad

9/27/2018
178
Dr. Atif Shahzad

9/27/2018
179
Dr. Atif Shahzad

9/27/2018
180
Dr. Atif Shahzad

9/27/2018
181
Dr. Atif Shahzad

9/27/2018
182
Dr. Atif Shahzad

9/27/2018
183
Dr. Atif Shahzad

9/27/2018
184
Dr. Atif Shahzad

9/27/2018
185
Dr. Atif Shahzad

9/27/2018
186
Dr. Atif Shahzad

9/27/2018
187
Dr. Atif Shahzad

9/27/2018
188
Dr. Atif Shahzad

9/27/2018
189
Dr. Atif Shahzad

9/27/2018
190
Dr. Atif Shahzad

9/27/2018
191
Dr. Atif Shahzad

9/27/2018
192
Dr. Atif Shahzad

9/27/2018
193
Dr. Atif Shahzad

9/27/2018
194
Dr. Atif Shahzad

9/27/2018
195
NEVER hesitate to
contact should you
have any question
Dr. Atif Shahzad
Dr. Atif Shahzad

9/27/2018 196

You might also like