Logic Gates Ghader Kurdi Adapted from the slides prepared by DEPARTMENT OF PREPARATORY YEAR.
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year...
-
Upload
jody-parrish -
Category
Documents
-
view
232 -
download
0
Transcript of ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year...
![Page 1: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/1.jpg)
ALGORITHMS AND FLOW CHARTS
Adapted from the slides Prepared by Department of Preparatory year
Prepared by: lec. Ghader Kurdi
![Page 2: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/2.jpg)
2
Programming language
• Programming language is an artificial language that specifies instruction to be executed on a computer.
• There are two types of programming languages:• Low level languages• High level languages
![Page 3: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/3.jpg)
3
Low level language
• Low level language is a language that is machine-dependent and/or that offers few control instructions and data types.
• Each statement in a program written in a low-level language usually corresponds to one machine instruction.
![Page 4: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/4.jpg)
4
Computer Languages
• There are many types of computer languages, which can be categorized into the following four types:-
• Low-Level Languages (1st & 2nd Generation Languages)• High-Level Languages (3rd Generation Languages)• User-Friendly Languages (4th Generation Languages)• Object-Oriented Languages (5th Generation Languages)
![Page 5: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/5.jpg)
5
4
Main operation of high level language program
• Input operations:• Like input, read
• Output operations:• Like write, print
• Arithmetic operations:• Like add, subtract, multiply etc....
• control transferring operations:• Like “GO TO”, conditional, non-conditional etc....
• Looping:• Like repeat, do while, for, etc...
![Page 6: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/6.jpg)
6
Main step in developing program
• Problem understanding• Algorithm developing• Program writing• Program editing• Program compiling• Program running• Testing and debugging
![Page 7: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/7.jpg)
7
What an Algorithm is ???
“Sequence of precise instructions which leads to a solution is called an algorithm.”
or“An algorithm is a set of steps that defines how a task is to performed.”
Example:Steps (algorithm) for preparing a cake.Steps (algorithm) for calculating the area of a rectangle.Steps (algorithm) to solve a first degree equation.Steps (algorithm) to solve a second degree equation
using delta.
![Page 8: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/8.jpg)
8
What an Algorithm is??? • In other words, algorithm is a method of representing the
step-by-step logical procedure for solving a problem.
• An algorithm is a recipe for finding the right-answer to a problem or to a difficult problem by breaking down the problem into simple cases.
• An algorithm can be written in English like sentences or in any standard representation. Sometimes, algorithm written in English like languages is called Pseudo Code.
![Page 9: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/9.jpg)
9
What an Algorithm is??? • According to D.E. Knuth, a pioneer in the computer science
discipline, an algorithm must possess the following properties: (i) Finiteness: An algorithm must terminate in a finite number of steps(ii) Definiteness: Each step of the algorithm must be precisely and unambiguously stated(iii) Effectiveness: Each step must be effective, in the sense that it should be primitive (easily convertible into program statement) can be performed exactly in a finite amount of time.(iv) Generality: The algorithm must be complete in itself so that it can be used to solve all problems of a specific type for any input data.(v) Input/output: Each algorithm must take zero, one or more quantities as input data produce one or more output values.
![Page 10: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/10.jpg)
10
Algorithm Representation
• There are many ways in which we may represent an algorithm;
• We use many techniques to represent an algorithm in computer programming. The techniques that will study are: Flowchart Pseudocode
![Page 11: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/11.jpg)
11
Some verbs used in Pseudocode
• Get, Read used when a computer is required to receive information or input from a particular source• Put, Output, Display, Print used when the computer
is required to receive information or input from a particular source • Compute, Calculate used when when the computer
is required to perform arithmetic• Initialise, Set used to give data an initial value • If used to select one of two alternate actions• Dowhile, Enddo used to repeat a group of actions
![Page 12: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/12.jpg)
12
Pseudocode Example
What is algorithm for calculating the area of a rectangle ?
1. Get the values width, height
2. Calculate the value of area = width * height
3. Display the value of the area
![Page 13: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/13.jpg)
13
What is algorithm for solving a first degree equation (AX+B = 0) ?
1. Read the values A,B
2. Check if A=0 (if yes there is no equation) therefore no
equation to be solved (end processing).
3. Check if B=0 (if yes X=0 and end processing).
4. Calculate the value of X= -B/A
5. Print the value of : X
Pseudocode Example:
![Page 14: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/14.jpg)
14
Why Flowcharts:
The flowchart is graphical representation of the steps required for an algorithm or program.
The flowchart is characterized by:
Clarify the program logic
Identify alternative processing methods
Serve as guide for program coding
Serve as documentation
![Page 15: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/15.jpg)
15
Principals of Flowchart
1. Simplicity:- Easy to put on paper- Easy to draw- Readable and meaningful
2. Organization:- putting ideas together and organizing those ideas in logical way.
3. Planning:- flowchart helps for looking the program in deeper and global way.
![Page 16: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/16.jpg)
16
General Concepts
Flowchart must be characterized by the following:
1. The major element of the project.
2. Elements are clearly labeled.
3. Sequence of element must be clear.
4. No gap or dead ends.
5. Sequence of elements must be in logical form.
6. Flowchart must be used correctly.
![Page 17: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/17.jpg)
17
No Name Symbol Usage
1 Ellipse Start/Stop
2 Rectangle Expressions
3 Parallelogram
Input (Read) / Output(Print)
4 Rhombus Conditional checking
5 Arrow Flow of solution
6 Circle Connector
7 Elongated Hexagon Continue
8 Rectangle with bars
Procedure / Function call
![Page 18: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/18.jpg)
18
Levels of Program Flowchart
1. Simple sequential flowchart.
2. Branched flowchart.
3. Simple-loop flowchart.
4. Multi-loop flowchart.
![Page 19: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/19.jpg)
19
Simple sequential flowchart
• It is simplest level which contain the sequence of steps without loops or branch.
• The flowchart comes in straight line from the beginning to the end.
![Page 20: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/20.jpg)
20
Example1: Algorithm for reading student name
1. Start
2. Read student name
3. End
start
Read student name
Stop
![Page 21: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/21.jpg)
21
Example2: Algorithm for calculate the area of circle
1. Start
2. Read value of R
3. Set PI equal to 3.14
4. Calculate Area=PI*R*R
5. Print R, Area
6. Stop
start
Read R
Stop
PI=3.14
Area=Pi*R*R
Print R, Area
![Page 22: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/22.jpg)
22
Example3: Write an algorithm and draw the flow chart to add two numbers.
Algorithm:
1.Start
2.Read a, b
3.Calculate c=a + b
4.Print c
5.Stop
Flow chart:
Start
c = a+ b
Print c
Read a, b
Stop
![Page 23: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/23.jpg)
23
Example4: Write an algorithm and draw the flow chart to find the circumference of circle
Algorithm:
1.Start
2.Read r
3.Calculate c= 2*3.14 * r
4.Print c
5.Stop
Start
c =2 * 3.14 * r
Print c
Read r
Stop
![Page 24: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/24.jpg)
24
Branched flowchart
• It is branched flowchart, when there is a condition statement in the program.
![Page 25: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/25.jpg)
25
Example5: Draw flow chart for evaluating the following functions: F(X)=x if X>=0, F(X)= -X if X<0
1. Start
2. Read X
3. If X>=0 then go to step 4, else step 5
4. Calculate F(X)=X than go to step 6
5. Calculate F(X)= -X
6. Print X, F(X)
7. Stop
start
Read X
Stop
F(x)= -x
Print x, F(x)
X>=0
F(x)=x
A
Yes
No
![Page 26: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/26.jpg)
26
Example6: Write an algorithm and draw the flowchart that reads three numbers and print the value of the largest number.
![Page 27: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/27.jpg)
27
Simple-loop flowchart
• It is a flowchart which contain iteration or repetitions.
• It is usually called loop flowcharts.
• In this type we need to repeat some operation several times using the same set of operation.
![Page 28: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/28.jpg)
28
Example7: write an algorithm and draw a flowchart for printing 10 integers starting from 1
Algorithm:
1.Start
2.Take the initial value of A=1
3.Check if A>=10 if yes and
If no print the number and increment A by 1
start
EndPrint A
A>=10?
A=A+1
A=1
Yes
No
![Page 29: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/29.jpg)
29
Multi-loop flowchart
• Looping is used to manage a loop to fulfill a repetition under a definite condition
Algorithm Sample1.Counter i 2. Any Initial value → ( Set i =0) 3.Final Value (N) → (Set N= 10) 4.Increment or Decrement Value (M) → (Set M=1)
![Page 30: ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.](https://reader030.fdocuments.net/reader030/viewer/2022033100/56649d945503460f94a7b719/html5/thumbnails/30.jpg)
30
Exercises
Write algorithms and draw flowcharts to represent the logic of the following programs:
• Calculate and print the sum of numbers from 1 to 100 .
• Accepts a number and display “Even Number” if the number entered is even. Otherwise the program will display “Odd Number”.
• Asks the user to input the password “hello”. Only if the password is correct, will display “Welcome”.
• Compares two numbers to see if one is greater than the other and print the result of the comparison.