computer chapter 8, 9 & 10
DESCRIPTION
Lahore Business School (LBS)TRANSCRIPT
Basic Computing/ Computer Orientation
Lecture 8, 9 & 10
by Sara Hassan
Chapter 8
Computer Programming Fundamentals
The Problem-Solving Process
Input
Processing
Output
Program
A program is the set of instructions that instruct the computer about the way the task is to be performed.
Instructions in a Program have three parts
Accept Input Data that needs to be processed
Act upon Input Data and process it
Provide Output to user Instructions are defined in a specific sequence
4Chapter-8: Computer Programming Fundamentals
Program Planning
1. Analyze
2. Design
3. Code, Test and Debug
4. Complete the Documentation
Program Development Lifecycle
Chapter-8: Computer Programming Fundamentals 6
Algorithm
Ordered sequence of finite, well defined, unambiguous instructions for completing a task.
English-like representation of logic to solve problem Step-by-step procedure for solving problem For a particular task, different algorithms can be written
Select an algorithm based on advantages and disadvantages
Different Algorithms would typically lead to trade off between memory requirements and execution speed
Chapter-8: Computer Programming Fundamentals 7
Algorithm: An Example
Chapter-8: Computer Programming Fundamentals 8
Control Structures
Specifies statements to be executed and order of execution of statements
Execution of a statement based on a decision Repetitively execute statements unless condition met
Used by Flowchart, Pseudo code Three kinds
Sequential: Instructions executed in linear orderSelection (branch or conditional): Asks true/false question and selects next instruction based on answerIterative (loop): Repeats execution of block of instructions
Chapter-8: Computer Programming Fundamentals 9
Flowchart
Diagrammatic representation of logic for solving task Drawn using boxes of different shapes with lines connecting
them to show the flow of control Make logic of program clearer in a visual form Diagrammatic representation forms a common medium of
communication Drawn using different kinds of symbols.
Chapter-8: Computer Programming Fundamentals 10
Flowchart symbols
Chapter-8: Computer Programming Fundamentals 11
Elements of Flowcharts Symbol Name
Flowline
Terminal
Input/Output
Processing
Decision
Continued… Symbol Name
Connector
Off page Connector
Predefined process
Annotation
Preparing a Flowchart
Common symbols that are used to draw a flowchart
Process, Decision, Data, Terminator, Connector and Flow lines Rules
Should have Start and End
Direction of flow must be from top to bottom and left to right
Relevant symbols must be used while drawing a flowchart
Sequence, selection, iterative structures may be used wherever required
Chapter-8: Computer Programming Fundamentals 14
Control Structures in Flowchart
Chapter-8: Computer Programming Fundamentals 15
Preparing a Flowchart (contd..)
Sequence: Steps executed in linear order Selection: Step to be executed next is based on decision
taken. Different path followed for True and False Iterative: Condition is checked. Either next step in sequence is
executed or control goes back to one of the already executed steps to make a loop
Limitations
May run into multiple pages, difficult to understand
Updating with changing requirements
Chapter-8: Computer Programming Fundamentals 16
Examples of Flowchart
Chapter-8: Computer Programming Fundamentals 17
Product of two numbers
Product of two numbers
Maximum of three numbersMaximum of
three numbersSum of first 100 integersSum of first 100 integers
Can you draw a
flowchart to read 100 numbers and print
the largest number?
start
Read data
Is next no. greater than
“greatest” ?
Assign the first value in the list to “greatest”
Compare the number in “greatest” with next
no. in the list
Assign next no. to “greatest”
Declare a variable “greatest”
Are there more numbers in the list?
YES
YES
NO
NO
Solving the Break- in - Class Problem
In order to make the classes more productive and easy on the nerves the Admin at a university wants to formulate the policy on giving breaks in the classes. They have come up with an idea of giving breaks only when they are required by the class students and their duration is also to be determined by how much severely it is demanded. You are required to develop a flow chart to demonstrate how the policy is to work. They’ve provided you with the following data:
Students who want break
Situation Break Duration
Less than equal to 5%
Trivial No break
5 < x ≤ 15 % Fair 3 Min Break
15 < x ≤ 25% Modest 5 Min Break
25 < x ≤ 50% or greater
Dire 10 Min Break
start
Show of Hands for
Break
0 < x < 5
No break
5 < x <1 5
3 mins
15 < x < 25
5 mins
END
25 < x <50
10 mins
Pseudo code
Consists of short, readable and formally-styled English language used for explaining an algorithm.
Does not include details like variable declarations, subroutines etc.
Short-hand way of describing computer program Not based on any programming language Uses structured constructs of programming language but is
not machine readable Cannot be compiled or executed No standard for syntax of pseudo code exists Easily translated into a programming language
Chapter-8: Computer Programming Fundamentals 21
Preparing Pseudo code
Written using structured English Commonly used terms to represent actions
Inputting data: INPUT, GET, READ
Outputting data: OUTPUT, PRINT, DISPLAY
Calculations: COMPUTE, CALCULATE
Incrementing: INCREMENT Control structures
Sequence structure: sequence of steps executed in linear order
Selection constructs: if-statement, case statement
Iterative statements: WHILE, DO-WHILE
Chapter-8: Computer Programming Fundamentals 22
Control structures for Pseudo code
Chapter-8: Computer Programming Fundamentals 23
IF (condition) THEN Statement(s) 1ELSE Statement(s) 2ENDIFStep 1
Step 2
Step 3
:::
WHILE (condition) Statement 1 Statement 2 : :END
DO Statement 1 Statement 2 : : WHILE (condition)
CASE expression of Condition-1 : statement1 Condition-2 : statement2 : Condition-N : statement N OTHERS: default statement(s)
IF (condition) THEN Statement(s) 1ENDIF
Sequence Selection Iteration
Algorithm, Flowchart, Pseudo code Algorithm: A sequence of instructions used to solve a
particular problem Flowchart and Pseudo code: Tools to document and represent
algorithm
Flowchart : Graphical representation of algorithm
Pseudo code: Readable, formally styled English like language representation of algorithm
No knowledge of programming language required to write or understand flowchart or pseudo code
Chapter-8: Computer Programming Fundamentals 24