Upload
ahmed-abd-el-aziz-marzouk
View
223
Download
0
Embed Size (px)
Citation preview
7/31/2019 Lecture 3 Problem Solving
1/74
Alexandria University
Faculty of Science
Computer Science Department
Introduction to Programming(CS 102)
7/31/2019 Lecture 3 Problem Solving
2/74
The Program Development Life
Cycle
Creating new programs is called programdevelopment.
Creating successful applications requires planning Computer professionals need to develop new ormodified applications from time to time The process associated with creating successful
applications programs is called the programdevelopment life cycle (PDLC).
7/31/2019 Lecture 3 Problem Solving
3/74
The Program Development Life Cycle
7/31/2019 Lecture 3 Problem Solving
4/74
1. Problem Analysis
During analysis, a systems analyst andprogrammer: review specifications, and
talk with users to fully understand what the softwareshould do.
Documentation consists of: program specifications,
timetable,
which language will be used,
how the program will be tested, and
what documentation is required.
7/31/2019 Lecture 3 Problem Solving
5/74
2. Program Design
Program design: stage where program
specifications are expanded into a complete
design of the new program.
Structured programming and object-oriented
programming are two of the most significant
approaches to the design process.
7/31/2019 Lecture 3 Problem Solving
6/74
Program Design:
Program Design Tools
Program design tools are planning tools.
Structure charts
Program flowcharts
Pseudocode
Data modeling
7/31/2019 Lecture 3 Problem Solving
7/74
Structure Charts
Structure charts depict the: overall organization of a program, and how the modules of a program are defined and how they connect to each other hierarchically.
Program modules should be arranged hierarchically in atop-downfashion Why?? So that their relationship to each other is apparent.
7/31/2019 Lecture 3 Problem Solving
8/74
7/31/2019 Lecture 3 Problem Solving
9/74
Flowcharts Use geometric symbols and familiar relational operatorsto provide a graphic display of the sequence of stepsinvolved in a program
Different symbols are used to represent different actionssuch as
Start/stopDecisionInput/outputProcessingLooping symbols
7/31/2019 Lecture 3 Problem Solving
10/74
7/31/2019 Lecture 3 Problem Solving
11/74
7/31/2019 Lecture 3 Problem Solving
12/74
7/31/2019 Lecture 3 Problem Solving
13/74
Pseudocode
Uses English-like statements in place of the graphic symbolsof the flowchart.
Unlike a flowchart, pseudocode is easy to modify and canbe embedded into a program as comments.
No standard set of rules exists for writing pseudocode,although a number of conventions have been developed.
7/31/2019 Lecture 3 Problem Solving
14/74
7/31/2019 Lecture 3 Problem Solving
15/74
Program Design:
Program Design Tools
Data modeling
is a technique used to illustrate the data in an applicationand is frequently used with object-oriented programming.
In a data model, the objects in the program are identified,along with their variables and class.
7/31/2019 Lecture 3 Problem Solving
16/74
7/31/2019 Lecture 3 Problem Solving
17/74
7/31/2019 Lecture 3 Problem Solving
18/74
3. Program Coding
Coding: actual process of creating theprogram in a programming language.
Programming language must be chosen.
Coding standards should be adhered to. Make use of reusable code and data dictionaries.
Translate coded programs into executable code.
Documentation results in finished sourcecode.
7/31/2019 Lecture 3 Problem Solving
19/74
7/31/2019 Lecture 3 Problem Solving
20/74
3. Program Coding contd
The coded program is referred to as source code. To beexecuted, the program is converted by the computerto object code using a special program.
Acompiler translates the entire program into machine
language before executing it. The program then doesntneed to be recompiled until it is modified.
An interpreter translates program statements one at atime. Interpreters are helpful during the debugging
stage, but are slower during execution of the finishedprogram.
An assembler converts assembly-language statementsinto machine language.
7/31/2019 Lecture 3 Problem Solving
21/74
7/31/2019 Lecture 3 Problem Solving
22/74
4. Program Debugging and Testing
Debugging: process of making sure a program
is free of errors or bugs.
Preliminary bugging often finds syntax or logic
errors.
Testing can consist of alpha or beta testing.
Documentation includes a copy of the finishedprogram code, plus test data and results.
7/31/2019 Lecture 3 Problem Solving
23/74
4. Program Debugging and Testing
contd
Preliminary debugging begins after the program has beenentered into the computer system.
Rarely is a program error-free the first time it runs.
Two common types of errors are syntax errors and logicerrors: Asyntax error occurs when the programmer has not followed the
rules of the language.
Alogic error, or execution-time error, results when the commandsyntax is correct but the program is producing incorrect results.
7/31/2019 Lecture 3 Problem Solving
24/74
5. Program Maintenance
Programmaintenance: process of updating
software so that it continues to be useful.
A costly process, but can be used to extend the
life of a program.
Documentation consists of amended program
package reflecting what problems occurred
and what program changes were made.
7/31/2019 Lecture 3 Problem Solving
25/74
What Is a Programming Language?
Aprogramming language is a set of rules used to writeinstructions to the computer.
7/31/2019 Lecture 3 Problem Solving
26/74
Categories of Programming Languages
Low-level languages
High-level languages
Fourth-generation languages (4GLs)
Natural and visual languages
7/31/2019 Lecture 3 Problem Solving
27/74
Machine and Assembly Languages Machine and assembly languages are called low-level
languages
Programmers who code in them must write instructions atthe finest level of detail, the base level of the hardware
Virtually no one writes machine-language programsanymore
All programs must be translated by a language translatorinto machine language before they are executed
Assembly languages are fast and consume little storagewhen compared with higher-level languages, but takelonger to write and maintain
7/31/2019 Lecture 3 Problem Solving
28/74
High-Level Languages
High-level languages differ from their low-levelpredecessors in that they require less coding detail andmake programs easier to write.
Programs written in a high-level language (BASIC,COBOL, Pascal, C, etc.) need to be translated into
machine language before they can be executed.
7/31/2019 Lecture 3 Problem Solving
29/74
Fourth Generation Languages (4GL) Very-high-level languages that are much easier to use
than the high-level languages They are declarative rather than procedural languages. For instance, to draw a bar chart in a procedural
language, you must tell the computer how to draw barsand where to place them.
In a declarativelanguage, you may be able to just point
to the data you want graphed, click several menuchoices, and youre in business.
Fourth-generation languages are commonly used toaccess databases (query languages).
7/31/2019 Lecture 3 Problem Solving
30/74
Popular Programming Languages
FORTRAN
COBOL
Pascal
n BASIC and Visual Basic
n C, C++, AND C#
n Java
7/31/2019 Lecture 3 Problem Solving
31/74
7/31/2019 Lecture 3 Problem Solving
32/74
Markup Languages
Markup languages use symbols or tags todescribe what a document should look likewhen displayed.
HTML (Hypertext Markup Language)
Dynamic HTML
XML(extensible markup language)
XHTML (extensible Hypertext Markup Language)
WML (Wireless Markup Language )
7/31/2019 Lecture 3 Problem Solving
33/74
7/31/2019 Lecture 3 Problem Solving
34/74
Scripting Languages
Scripting languages are used to build programinstructions into Web pages, usually to adddynamic content.
JavaScript
VBScript
Perl
7/31/2019 Lecture 3 Problem Solving
35/74
7/31/2019 Lecture 3 Problem Solving
36/74
FLOWCHARTS
Understanding the basic parts of the flowchartand the advantages and limitations of
flowcharts.
7/31/2019 Lecture 3 Problem Solving
37/74
Definition
Flowcharts a diagrammatic representation
that illustrates the sequence of operations to
be performed to get the solution of a
problem.
7/31/2019 Lecture 3 Problem Solving
38/74
How to Write a Program?
1. Start2. Define Problem to Solve
3. Think of an Algorithm
4. Think of a Technique for the Solution
5. Write Solution on a Piece of Paper
6. If not Finished Repeat Steps 4 and 5
7. Write Code and Program
8. Debug and Test9. If not Finished Repeat Steps 7 and 8
10. Stop
7/31/2019 Lecture 3 Problem Solving
39/74
How to Write a Program?
Define Problem to Solve
Stop
Think of an Algorithm(Technique for the solution)
Idea!!!!
Write it downon a piece of paper(before you forget it!!!)
Write Code andProgram
JobDone
Debug and Test
JobDone
Start
Think of aTechnique for the solution
No
Yes
YesNo
7/31/2019 Lecture 3 Problem Solving
40/74
Algorithms
A computer program makes use of algorithms
Algorithm is a complete step-by-step
procedure for solving a problem or
accomplishing a task.
7/31/2019 Lecture 3 Problem Solving
41/74
A1
Off-pageConnector
? Yes
No
Condition
CalculateVariables
Process Rectangle
A
On-Page
Connector
PrintResults
Input/OutputStart Or StopProcess
Start
Standard Flowchart Symbols
7/31/2019 Lecture 3 Problem Solving
42/74
Example of Flow Charts
7/31/2019 Lecture 3 Problem Solving
43/74
Basic Flow Chart Operations
All computer instructions are based on four
basic processing patterns:
Simple Sequence
Selection Pattern
Loop Pattern
Branch Pattern
7/31/2019 Lecture 3 Problem Solving
44/74
Simple Sequence
Logic involves executing instructions one
statement after another, in the order
presented by the program.
This is the simplest and most-used pattern.
The computer assumes that all instructions
are to be executed in this order unless the
program presents other instructions.
7/31/2019 Lecture 3 Problem Solving
45/74
Read Names
Compare with Criteria
Print Eligible Names
Example of a Simple Sequence
7/31/2019 Lecture 3 Problem Solving
46/74
Selection Pattern
Requires that the computer make a choice
among two or more items.
Each choice is based on one of two
comparisons a computer can make: true or
false (Yes or No).
7/31/2019 Lecture 3 Problem Solving
47/74
?HonourStudent
List underStudent Loan
Yes
List underScholarships
No
Example of a Selection Pattern
7/31/2019 Lecture 3 Problem Solving
48/74
Loop Pattern
Causes an interruption in the normal sequence of
processing and directs the computer to loop back
to a previous statement in the program,
repeating the same sequence over again, usuallywith new data.
By looping, the programmer avoids having to
repeat the same set of instructions over and over.
7/31/2019 Lecture 3 Problem Solving
49/74
?
MoreNames
No
Yes
Compare with Criteria
Print Eligible Names
Example of a Loop Pattern
7/31/2019 Lecture 3 Problem Solving
50/74
Branch Pattern
Is often used in combination with selection orlooping.
The branch pattern allows the computer to skip
statements in a program. The branch encourages undisciplined jumping
around among program statements, acharacteristic that is frowned on by most
programming experts. Branching is difficult to follow and is an inefficient
use of computer power.
7/31/2019 Lecture 3 Problem Solving
51/74
?
HonourStudent
No
Yes
Read Area of Concentration
Read Courses Taken
Compare with Criteria
1
1
Example of a Branch Pattern
7/31/2019 Lecture 3 Problem Solving
52/74
Types of Flowcharts
1. Functional Chart A chart that is used to describe how activities interact with one another
within an organization as well as with other organization and/or systems.
2. Process Flow Chart
A chart that is used to describe the sequence and relationship of the tasks
that make up an activity
3. Process Flow Description Chart Detailed description of the tasks outlined in a Process Flow Chart
Typically used to show the kinds of tasks performed within a process (thenumber of operations, review, and transfers; and the amount of storage andtime required to complete an activity)
7/31/2019 Lecture 3 Problem Solving
53/74
Uses of Flowcharts Flowcharts are generally drawn in the early stages of formulating
computer solutions.
Flowcharts facilitate communication between programmers andbusiness people.
Flowcharts play a vital role in the programming of a problem and
are quite helpful in understanding the logic of complicated andlengthy problems.
Once the flowchart is drawn, it becomes easy to write theprogram in any high level language.
Often we see how flowcharts are helpful in explaining the
program to others. Hence, it is correct to say that a flowchart is a must for the better
documentation of a complex program.
7/31/2019 Lecture 3 Problem Solving
54/74
Advantages of Flowcharts
Communication: Flowcharts are better way ofcommunicating the logic of a system to allconcerned.
Effective Analysis: With the help of flowchart,problem can be analysed in more effectiveway.
Proper Documentation: Program flowcharts
serve as a good program documentation,which is needed for various purposes.
7/31/2019 Lecture 3 Problem Solving
55/74
Disadvantages of Flowcharts
Complex Logic: Sometimes, the program logic isquite complicated. In that case, flowchartbecomes complex and clumsy.
Alterations and Modifications: If alterations are
required the flowchart may require re-drawingcompletely.
Reproduction: As the flowchart symbols cannotbe typed, reproduction of flowchart becomes a
problem. The Essentials of what Is Done can easily be lost
in the technical details of how it is done
7/31/2019 Lecture 3 Problem Solving
56/74
Case Study - 1
A certain Company gives each of its sales
people $1,000 at the beginning of each month
to cover travel, lodging, and food expenses.
At the end of the month, a salesperson must
total his/her expense receipts. If the amount is
less than $1,000 then the difference must bereturned to the company.
7/31/2019 Lecture 3 Problem Solving
57/74
Case Study 1 (Solution)
Here is an algorithm for figuring how much
money, if any, must be returned:
1. Total the expense receipts for the month.
2. Subtract the amount of the expense receipts
from 1,000.
3. If the remainder is more than 0, return that
amount to the company.
7/31/2019 Lecture 3 Problem Solving
58/74
Stop
Subtract ExpensesR
7/31/2019 Lecture 3 Problem Solving
59/74
Case Study 1 (Solution 1)
Assume the Employee has Spent $1250
What would be the amount to return to the
company in this case?
7/31/2019 Lecture 3 Problem Solving
60/74
Stop
Subtract ExpensesR
7/31/2019 Lecture 3 Problem Solving
61/74
Case Study 1 (Solution 2)
Now Assume the Employee has spent $950
What would be the amount to return to the
company in this case?
7/31/2019 Lecture 3 Problem Solving
62/74
Stop
Subtract ExpensesR = 1000-E
?E
7/31/2019 Lecture 3 Problem Solving
63/74
Basic Guidelines for Flowcharts
Only one flow line
should come out
from a process
symbol.
7/31/2019 Lecture 3 Problem Solving
64/74
Basic Guidelines for Flowcharts
Only one flow line
should enter a
decision symbol, but
two or three flowlines, one for each
possible answer,
should leave thedecision
symbol.
Y
N
?
7/31/2019 Lecture 3 Problem Solving
65/74
Basic Guidelines for Flowcharts
Only one flow line is
used in conjunction
with terminal
symbol.
Stop
Start
7/31/2019 Lecture 3 Problem Solving
66/74
Basic Guidelines for Flowcharts
Ensure that the flowchart has a logical start
and finish.
It is useful to test the validity of the flowchart
by passing through it with a simple test data.
7/31/2019 Lecture 3 Problem Solving
67/74
Case Study - 2
Draw a flowchart to find the sum of first 5
natural numbers.
Start
7/31/2019 Lecture 3 Problem Solving
68/74
10
Case Study - 2Sum = 0
Stop
N = 0
N = N + 1
?N = 5
Start
Sum = Sum + N
No
Yes
N Sum
0 0
1 1
2 3
3 6
4
5 15Print Sum
7/31/2019 Lecture 3 Problem Solving
69/74
Case Study - 3
Draw a flowchart to find the largest of three
numbers A,B, and C.
7/31/2019 Lecture 3 Problem Solving
70/74
Case Study - 3
Stop
?A > B
Start
No
Read A, B, C
?B > C
No
?A > C
No
Print B Print C Print APrint C
Yes Yes Yes
7/31/2019 Lecture 3 Problem Solving
71/74
Case Study 3 - (Solutions)
Consider four input cases:
1. A = 5 ; B = 7 ; C = 3
2. A = 5 ; B = 3 ; C = 73. A = 5 ; B = 3 ; C = 3
7/31/2019 Lecture 3 Problem Solving
72/74
Case Study 3 (Solution 1)
Stop
?A > B
Start
No
Read A, B, C
?B > C
No
?A > C
No
Print B Print C Print APrint C
Yes Yes Yes
B = 7
C = 3
A = 5
7/31/2019 Lecture 3 Problem Solving
73/74
Case Study 3 (Solution 2)
Stop
?A > B
Start
No
Read A, B, C
?B > C
No
?A > C
No
Print B Print C Print APrint C
Yes Yes Yes
B = 3
C = 7
A = 5
7/31/2019 Lecture 3 Problem Solving
74/74
Case Study 3 (Solution 3)
Stop
?A > B
Start
No
Read A, B, C
?B > C
No
?A > C
No
Print B Print C Print APrint C
Yes Yes Yes
B = 3
C = 3
A = 5