Download presentation
Presentation is loading. Please wait.
1
Introduction to Algorithms and Programming
Dr. Majed Bouchahma
2
What is an Algorithm ? An algorithm is a sequence of finite instructions, often used for calculation and data processing. It is formally a type of effective method in which a list of well-defined instructions for completing a task will, when given an initial state, proceed through a well-defined series of successive states, eventually terminating in an end-state.
3
What is an Algorithm ? There are two commonly used tools to help to document program logic (the algorithm). These are flowcharts and Pseudocode. We will use both methods here. Generally, flowcharts work well for small problems but Pseudocode is used for larger problems.
4
What is an Algorithm ? Algorithms must:
1. Be well-ordered and unambiguous 2. Be executable (understandable), 3. Solve the problem, and 4. Terminate.
5
Example: Write an algorithm to add two numbers
Start Step 1: Get number1 Step 2: Get number2 Step 3: Sum --- number1 + numbert2 Step 4: Display/Print sum Stop
6
Basic (primitive) operations involved
Read the input from user Carry out basic arithmetical computations Print the output to the user
7
The key features of an algorithm
Sequence (also known as Process) Decision (also known as Selection) Repetition (also known as Iteration or Looping)
8
Write and algorithm to compare two numbers and print the smallest number
Start: Step 1: Get number1 Step 2: Get number2 Step 3: If number1< number2 Step 4: print number1 is smaller Step 4: else print number2 is smaller. Stop
9
Pseudocode Pseudocode is a more simplified version of an algorithm.
An algorithm may be written using a certain language like C but seudocode is always written in simple English statements.
10
Pseudocode Allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax.
11
The problem: Design a program which accepts as input two numbers and calculates sum and difference and then displays both the sum and the difference.
12
Psuedocode: Start Step1: Read the first number
Step2: Read Second Number Step3: Add the numbers together Step4: Find the difference between the numbers Step5: Display the sum Step6: Display the difference Stop
13
Variables A named location that stores a value
C = A + B B = 4 A = 7 A = A + 3 A = A + C A B C
14
Conditionals Statement implemented if condition is true
A=1, B=2, C=3, D=4 if A < B C = 5 if A > B D = 4 if C < D B = B + 1 else B = B - 1 A B C D 1 2 3 4
15
Algorithm: Start Step1: Read Number1 Step2: Read Number2
Step3: Sum = Number1 + Number2 Step4: Difference = Number1 - Number2 Step5: DISPLAY Sum Step6: DISPLAY Difference Stop
16
Arithmetic operators + add - subtract * multiply / divide = assign
17
Relational operators. == equal to (a pair of equals signs)
<= less than or equal to >= greater than or equal to <> not equal to
18
Logical operators AND OR NOT
19
IF (x = = 32) AND (y = = 7) THEN sumxy = x + y
20
IF (letter = = 'A') OR (letter = = 'E') THEN DISPLAY 'Vowel‘
IF NOT (letter = = 'A') THEN DISPLAY 'Not letter A'
21
Order of precedence for operators
( ) parentheses NOT * / AND + - OR == > < <> >= <= from left to right.
22
Flowcharts Definition: Flow chart is a graphical method of to represent the algorithm that is a popular tool to design programs. - Following symbols are used to draw a flow chart
23
Draw a flow chart that accepts two numbers and displays the sum.
Draw a flowchart for the problem that is accepting two numbers and calculating sum and difference and then displaying the result
24
A flowchart that reads the name of the students in the class and displays it.
Start Display the name of the student Is there any more student in the class? Stop Read the name of the student yes no
25
Draw a flowchart to add all the numbers from 1 to 20.
Start Display Sum Number =1 Is Number=20 Sum=Sum+Number Sum =0 Number =Number+1 Stop
26
Psuedocode Step 1: Set Number=1 Step 2: Set Sum=0
Step 3: Sum=Sum+Number Step 4: Check if Number =20 Step 5: if yes Display the Sum Step 6: Stop Step 7: Otherwise Number=Number+1 Step 8: Go to step 3
28
A program has to be written to display the grades of the students in a class of 30 students. Grade is calculated on the basis of percentage (%) marks obtained by the student. More than or equal to 80% A More than or equal to 60% B More than or equal to 50% C More than or equal to 40% D Less than 40% F (Fail) Draw a flowchart and write the corresponding algorithm.
29
Get the mark of the Student Are there more students?
Start Mark>=80? Get the mark of the Student Mark>=60? Mark>=50? Mark>=40? Are there more students? Display Grade A Display Grade D Display Grade B Display Grade C Display Grade F Stop yes yes yes yes
30
Draw a flowchart that accepts 3 numbers say x, y, and z from the user and finds the largest number. Note that your flowchart should use minimum number of decision boxes.
31
Start Is x > y Display z is largest Is y > z Is x > z
Display x is largest Display y is largest Get x, y, z Stop yes yes no no no yes
32
Some rules for flow charts
Every flow chart has a START symbol and a STOP symbol The flow of sequence is generally from the top of the page to the bottom of the page. This can vary with loops, which need to flow back to an entry point. Use arrowheads on connectors where flow direction may not be obvious. There is only one flow chart per page A page should have a page number and a title A flow chart on one page should not break and jump to another page A flow chart should have no more than around 15 symbols (not including START and STOP)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.