Professional Documents
Culture Documents
Chapter 04 - 1100-Support
Chapter 04 - 1100-Support
PSEUDO CODE
Flow Chart
Process
Decision Box
Flow of Data
2
3
In an algorithm, we use two operations Read() and Write() to take a data from the user entered using
an input like the keyboard and to display some data on the output like the screen. Consider x as
number.
Read(x): the Read operation takes some value entered by the user on the keyboard and puts the
value in the variable x that is stored in memory.
Write(“Message”): the Write operation displays Message on the screen. Sometimes PRINT is also
used.
Example: int x = 7;
Write(x, “is a positive number.”); => 7 is a positive number.
4
5
6
BEGIN
int temp;
WRITE(“Enter the current temperature”);
READ(temp);
IF (temp < 0) THEN
WRITE(“The temperature ”, temp, “ is below zero. It is cold”);
temp = temp + 5;
ELSE IF (temp >40) THEN
WRITE(“The temperature ”, temp, “ is over forty. It is very hot”);
temp = temp - 5;
ELSE IF (temp >=20 AND temp <=25) THEN
WRITE(“Warm temperature, do not change!”);
ENDIF
ENDIF
ENDIF
END
7
8
Example 1: addition 2 numbers
Flow Chart
START
READ num1,num2
WRITE sum
END
9
Example 2
Flow chart
Max between 2 numbers
START
READ n1,n2
yes No
n1>n2
WRITE n1 WRITE n2
END
10
Example 3: temperature
Flow chart
Freezing degree
START
READ temp
yes No
Temp<=0
WRITE "freezing
start" WRITE " above
freezing"
END
11
Example 4: odd or even numbers
Flow chart
Nb%2 allows to compute division remainder
START
READ nb
yes No
nb%2 ==0
END
12
Example 5: rectangle
Flow chart
Perimeter and area of a rectangle
START
READ length,width
Perimeter =2*(length+width)
area =length*width
WRITE Perimeter
WRITE area
13
END
Example 6
Flow chart
Area of a circle
START
READ radius
area =3.14*radius*radius
WRITE area
END
14
Exercise
15
1. BEGIN
2. float price, totalPrice;
3. int Quantity;
4. WRITE(“PRICE?”);
5. READ (Price)
6. READ(Quantity);
7. totalPrice = (Price + Price *0.1) * Quantity;
8. WRITE(“ Total=“, totalPrice);
9. END
16
2.2 Algorithms
Computing problems
Solved by executing a series of actions in a specific order
Algorithm is a procedure determining
Actions to be executed
Order to be executed
Example: recipe
Program control
Specifies the order in which statements are executed
17
2.3 Pseudocode
Pseudocode
Artificial, informal language used to develop algorithms
Similar to everyday English
Not executed on computers
Used to think out program before coding
Easy to convert into C program
Only executable statements
No need to declare variables
18
Building a Program
Whatever type of problem needs to be solved, a careful thought out plan of attack, called
an algorithm, is needed before a computer solution can be determined.
19
Building a Program
23
Building a Program
24
The evolution of programming paradigms
25
Algorithm
26
Algorithm
In your algorithm (or program), you can use different types of statements.
27
Algorithm: Sequence
28
Algorithm: Sequence
INPUT/OUPUT statements
29
Algorithm: Sequence
30
Algorithm: Sequence
31
Algorithm: Sequence
Syntax:
1- WRITE List of variables
where, List of variables contains one or more variables
e.g. WRITE x;
WRITE a, b;
32
Algorithm: Sequence
2- WRITE message;
where message may by any string of
characters enclosed with double quotas.
33
Algorithm: Sequence
3- WRITE expression;
where expression is any arithmetic expression
e.g. WRITE 3 + 6;
WRITE x – y;
The semantics (execution) of this statement:
First, the expression is evaluated, then the result will be displayed on
the screen. For the first example, it will display 9.
34
Algorithm: Sequence
NOTE
You can mix between the different types of the WRITE statements.
e.g.
WRITE “Length = “ , length; è ex: Length= 15
35
Assignment Statement
Syntax:
Variable ç Expression;
The semantics (execution) of this statement:
1- The Expression on the right of ß is evaluated
2- The result of the expression is assigned to the variable on the left of
ß.
e.g.
X ß 10 ; (This means that X has 10 now )
Y ß X + 5; (This means that Y has 11 now, if X is 6)
36
Assignment Statement
NOTE:
The right hand side (RHS) of the assignment statement should be of the
same data type of the left hand side (LHS).
e.g.
1- T ß true;
This will be correct if T is of Boolean type (True or False).
2- A ß x + y * 2;
This will be correct if A has a numeric data type (e.g. integer, or real) and
the value of the expression on (RHS) has the same numeric data type.
37
Assignment Statement
n How to execute a statement like X ß X + 1; ?
Suppose we have:
X ß 5;
Then to execute X ß X + 1, we proceed as follows:
X Xß5
5 6
XßX+1
38
Assignment Statement
n Dereferencing:
If we want to copy a value from one memory location (say, X) into
another location (say,Y), we say that we dereference a variable.
e.g.
X ß 5;
Y ß 10;
X ßY ; // now X has the value 10
X Y
5 10 10
39
Example 1
40
Example 1
1- Analysis stage:
Problem Input:
- num1
- num2
Problem Output:
- summation of two numbers
Formula:
sum=num1+num2
41
Example 1
2- Algorithm Design
We write the algorithm by using the pseudo code
ALGORITHM Summation
BEGIN Output:
1. WRITE “enter 2 nbers: “ ; Enter 2 nbers: 100 5
Sum=105
2. READ num1, num2;
3. sumß num1+ num2;
4. WRITE “sum=“ ,sum;
END
42
Example 1
43
Control Structures
Selection: if/else
44
2.4 Control Structures
Sequential execution
Statements executed in order
Transfer of control
Next statement executed not next one in sequence
3 control structures
Sequence structure
Programs executed sequentially by default
Selection structures
if, if/else
Repetition structures
…
45
2.4 Control Structures
Flowchart
Graphical representation of an algorithm
Special-purpose symbols connected by arrows (flowlines)
Rectangle symbol (action symbol)
Any type of action
Oval symbol
Beginning or end of a program, or a section of code (circles)
46
2.5 if Selection Structure
Selection structure
Choose among alternative courses of action
Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
If the condition is true
Print statement executed, program continues to next statement
If the condition is false
Print statement ignored, program continues
Indenting makes programs easier to read
C ignores whitespace characters (tabs, spaces, etc.)
47
2.5 if Selection Structure
if ( grade >= 60 )
WRITE "Passed";
Diamond symbol (decision symbol)
Indicates decision is to be made
Contains an expression that can be true or false
Test condition, follow path
if structure
Single-entry/single-exit
48
2.5 if Selection Structure
49
2.6 if/else Selection Structure
if
Performs action if condition true
if/else
Different actions if conditions true or false
Example text to Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
Algo:
if ( grade >= 60 )
WRITE "Passed";
else
WRITE "Failed";
50
Exam Grade Flowchart
READ examGrade, quizGrade;
if (examGrade < 60)
WRITE “We have a problem” ;
if (quizGrade < 10)
WRITE “We have a real problem” ;
else
WRITE “Ok”;
51
Ascending order 3 var using one auxiliary var START
READ A, B,C
yes
A>B
B>C
yes
Tmp=B
B=C
C=tmp Swap B and C
No
A>B yes
Tmp=A
Swap A and B
A=B
B=tmp
write A, B,C
52 END
Ascending order 3 var using one auxiliary var START
5 3 1
yes
A>B
5 3 1
Tmp=A Swap A and B
A=B
3 5 1 3 B=tmp
No
3 5 1 B>C
yes
Tmp=B
3 1 5 5 B=C
Swap B and C
C=tmp
No
3 1 5 3 A>B yes
Tmp=A
Swap A and B
1 3 5 A=B
B=tmp
write A, B,C
53 END
Ascending order 3 var using one auxiliary var START
READ A, B,C
yes
A>B
B>C
yes
Tmp=B
B=C
C=tmp Swap B and C
No
A>B yes
Tmp=A
Swap A and B
A=B
B=tmp
write A, B,C
54 END
Ascending order 3 var using one auxiliary var START
5 3 1
yes
A>B
5 3 1
Tmp=A Swap A and B
A=B
3 5 1 3 B=tmp
No
3 5 1 B>C
yes
Tmp=B
3 1 5 5 B=C
Swap B and C
C=tmp
No
3 1 5 3 A>B yes
Tmp=A
Swap A and B
1 3 5 A=B
B=tmp
write A, B,C
55 END
Exercise
ascending order without auxiliary var only branching
statements
56
START
READ A, B,C
No yes
A>B
No yes
No yes B>C
A>C
No No yes
yes C<A
B<C
write B,C, A
write A, C, B
57