3/24/2018
Cairo University
Faculty of Economics and Political Science
Social Science Computing Department
2nd Year Course
Introduction To Programming
Dr. Ghada Eldegedi
A Program is
A sequence of instructions that specifies
how to perform a task on a computer.
1
3/24/2018
Steps for writing a program
1. Determine what the output should be—
that is, exactly what the task should
produce.
2. Identify the data, or input, necessary to
obtain the output.
3. Determine how to process the input to
obtain the desired output.
Example
Consider the following algebra problem:
How fast is a car traveling if it goes 50 miles in 2
hours?
1. The first step is to determine the type of answer
requested. The answer should be a number giving the
rate of speed in miles per hour (the output).
2. The information needed to obtain the answer is the
distance and time the car has traveled (the input).
3. The formula: rate= distance / time is used to process
the distance traveled and the time elapsed in order to
determine the rate of speed. That is,
Rate = 50miles / 2
= 25 miles / hour
So, we determine what we want as output, get the needed input, and
process the input to produce the desired output.
2
3/24/2018
The Program Development Cycle
1. Analyze
2. Design
3. Code
4. Test and debug
5. Complete the documentation
The Program Development Cycle (cont)
1. Analyze: Define the problem.
Be sure you understand what the
program should do, that is, what the
output should be.
Have a clear idea of what data (or
input) are given and the relationship
between the input and the desired
output.
3
3/24/2018
The Program Development Cycle (cont)
2. Design: Plan the solution to the problem.
Find a logical sequence of precise steps
that solve the problem. Such a sequence
of steps is called an algorithm.
The Program Development Cycle (cont)
3. Code: Translate the algorithm into a
programming language.
Coding is the technical word for writing the
program.
4. Test and debug: Locate and remove any errors
in the program.
Testing is the process of finding errors in a
program, and debugging is the process of
correcting errors that are found. (An error in a
program is called a bug.)
4
3/24/2018
Program Errors
There are three types of program errors:
syntax errors
run-time errors
logic errors
Heba M. Ezzat
1. syntax error
is a violation of the rules of the programming language.
E.g. in Python if you write
print “hellow world” instead of
Print(“hellow world”)
The Python interpreter catches syntax errors and displays
a red error message beginning with ‘SyntaxError’. A single
syntax error will prevent your program from executing.
Heba M. Ezzat
10
5
3/24/2018
2. run-time error
is an error that results from using invalid
operand values for an operation.
E.g. The following statement generates a
run-time error if Count has the value zero,
since division by zero is not
mathematically defined.
Heba M. Ezzat
11
3. logic error
A logic error in a program is any code that causes
incorrect output/results even though the program runs to
completion.
E.g. The following code contains a logic error because
the > should be >=
if Age > 18 print(“can vote”)
A program with a logic error may give the correct answer
sometimes and the wrong answer other times.
Logic errors typically are the most difficult type of errors
to find and correct.
Finding logic errors is the primary goal of testing.
Heba M. Ezzat
12
6
3/24/2018
The Program Development Cycle (cont’d)
5. Complete the documentation: Organize all the
material that describes the program.
Documentation is intended to allow another person or
the programmer at a later date, to understand the
program.
Internal documentation consists of statements in the
program that are not executed, but point out the
purposes of various parts of the program.
Documentation might also consist of a detailed
description of what the program does and how to use the
program (for instance, what type of input is expected).
13
Programming Tools
The following tools are used to convert
algorithms into computer programs
Flowcharts Pseudocodes
14
7
3/24/2018
Flowcharts
Graphically depict the logical steps to carry out
a task and show how the steps relate to each
other.
A flowchart consists of special geometric symbols
connected by arrows.
Within each symbol is a phrase presenting the
activity at that step.
The shape of the symbol indicates the type of
operation that is to occur.
15
Table 1: Flowchart Symbols
16
8
3/24/2018
Example: A flowchart of a decision structure
Many problems require a decision to determine whether a series of
instructions should be executed. If the answer to a question is “Yes,”
then one group of instructions is executed. If the answer is “No,” then
another is executed. This structure is called a decision structure.
17
Advantages & Disadvantages of flowcharts
The main advantage of using a flowchart to plan a
task is that it provides a pictorial representation of
the task, which makes the logic easier to follow.
We can clearly see every step and how each step
is connected to the next.
The major disadvantage with flowcharts is that
when a program is very large, the flowcharts may
continue for many pages, making them difficult to
follow and modify.
18
9
3/24/2018
** Pseudocode
Uses English-like phrases to outline the task.
Pseudocode is an abbreviated version of actual
computer code.
The geometric symbols used in flowcharts are
replaced by English-like statements that outline
the process.
As a result, pseudocode looks more like
computer code than does a flowchart.
19
Example: A Pseudocode of a decision structure
If condition is true Then
Process step(s) 1
Else
Process step(s) 2
End If
20
10
3/24/2018
Advantages of Pseudocodes
Pseudocode allows the programmer to focus on
the steps required to solve a problem rather than
on how to use the computer language.
It is compact and probably will not extend for
many pages as flowcharts commonly do.
Also, the plan looks like the code to be written
and so is preferred by many programmers.
21
Example:
The postage stamp problem
When you mail a letter, you must decide how much
postage to put on the envelope. One rule of thumb is to use
one stamp for every five sheets of paper or fraction thereof.
You need to determine the number of stamps to place on
an envelope.
* Determine the proper number of stamps for a letter with
16 sheets of paper.
First write the algorithm that you will use to solve this
problem then describe it using a flowchart, and
Pseudocode,
22
11
3/24/2018
The Algorithm of the postage stamp
problem
1. Request the number of sheets of paper;
Sheets = 16.
2. Dividing 16 into 5 gives 3.2.
3. Rounding 3.2 up to 4 gives Stamps = 4.
4. Reply with the answer, 4 stamps.
23
The Flowchart
of the postage
stamp problem
24
12
3/24/2018
** The Pseudocode for the postage stamp
problem
Read Sheets (input)
Set the number of stamps to Sheets / 5 (processing)
Round the number of stamps up to the next whole number
(processing)
Display the number of stamps (output)
25
Lectue 2
Dr. Heba M. Ezzat
26
13
3/24/2018
Types of Algorithms
The algorithm and flowchart, classification
depends on the three types of control
structures. They are:
1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
27
SEQUENCE
In a computer program or an algorithm, sequence
involves simple steps which are to be executed one
after the other. The steps are executed in the same
order in which they are written.
Below are examples set of instructions to calculate
the area of a circle and converting temperature
measurement.
28
14
3/24/2018
Examples of
Problem Solving
Techniques
29
Problem 1: Find the
area of a Circle of
radius r.
30
15
3/24/2018
Algorithm:
Step1: Read\input the Radius r of the
Circle
Step2: Area = PI*r*r // calculation of area
Step3: Print Area
31
Flowchart:
32
16
3/24/2018
Problem 3: Convert
temperature
Fahrenheit to Celsius
33
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C = 5/9*(F - 32)
Step 4: Print Temperature in Celsius: C
Step5: End
34
17
3/24/2018
Flowchart
35
Decision
is used in a computer program or algorithm to
determine which particular step or set of steps is to
be executed. A decision statement can be used to
choose a specific path dependent on a condition.
There are two types of selection: binary selection
(two possible pathways) and multi-way selection
(many possible pathways).
36
18
3/24/2018
binary selection
37
Multi-way decision
38
19
3/24/2018
Problem 4: write algorithm
to find the greater number
between two numbers
39
Algorithm:
Step1: Start
Step2: Read/input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End
40
20
3/24/2018
Flowchart
41
Problem 5: The Temperature Flowchart
42
21
3/24/2018
Problem 6: Playing Golf Flowchart
43
Problem 7: The Software Development
Cycle Flowchart
44
22
3/24/2018
Problem 8:
The Software
Installation
Flowchart
45
Problem9: A
algorithm to find the
largest value of any
three numbers.
46
23
3/24/2018
Algorithm:
Step1: Start
Step2: Read/input A,B and C
Step3: If (A>=B) and (A>=C) then Max=A
Step4: If (B>=A) and (B>=C) then Max=B
Step5:If (C>=A) and (C>=B) then Max=C
Step6: Print Max
Step7: End
47
48
24
3/24/2018
REPETITION (Looping)
Repetition allows for a portion of an algorithm or
computer program to be executed any number of
times depending on some condition being met. An
occurrence of repetition is usually known as a loop.
An essential feature of repetition is that each loop
has a termination condition to stop the repetition, or
the obvious outcome is that the loop never
completes execution. This is known as an infinite
loop and is obviously undesirable. The termination
condition can be checked or tested at the beginning
or end of the loop and is known as a pre-test or post-
test, respectively. Following is a description of each
of these types of loop.
49
Repetition pre-test loop
Repetition post-test loop
50
25
3/24/2018
Problem10: An
algorithm to print
even numbers
between 10 and 99
51
Algorithm:
1. Start
2. I = 10
3. Write I in standard output
4. I = I+2
5. If (I <=98) then go to line 3
6. End
52
26
3/24/2018
i = 10
i=i+2
53
Problem11: Draw a
flowchart to find the
sum of first 50
natural numbers.
54
27
3/24/2018
Sum = 0
N=0
55
Problem12:The algorithm
sums all the even numbers
between 1 and 20 inclusive
and then displays the sum.
56
28
3/24/2018
Algorithm:
1. Start
2. sum = 0
3. count = 1
4. REPEAT
5. IF count is even THEN sum = sum + count
6. count = count + 1
7. UNTIL count > 20
8. DISPLAY sum
9. End 57
sum = 0
count = 1
Display sum
58
29