Introduction to Algorithms and Programming Dr Majed Bouchahma

Introduction to Algorithms and Programming Dr. Majed Bouchahma 1

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. 2

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. 3

What is an Algorithm ? Algorithms must: 1. Be well-ordered and unambiguous 2. Be executable (understandable), 3. Solve the problem, and 4. Terminate. 4

Example: Write an algorithm to add two numbers • • • 5 Start Step 1: Get number 1 Step 2: Get number 2 Step 3: Sum --- number 1 + numbert 2 Step 4: Display/Print sum Stop

Basic (primitive) operations involved • Read the input from user • Carry out basic arithmetical computations • Print the output to the user 6

The key features of an algorithm • • • 7 Sequence (also known as Process) Decision (also known as Selection) Repetition (also known as Iteration or Looping)

• Write and algorithm to compare two numbers and print the smallest number Start: Step 1: Get number 1 Step 2: Get number 2 Step 3: If number 1< number 2 Step 4: print number 1 is smaller Step 4: else print number 2 is smaller. Stop 8

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. 9

Pseudocode • Allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. 10

• 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. 11

Psuedocode: Start Step 1: Read the first number Step 2: Read Second Number Step 3: Add the numbers together Step 4: Find the difference between the numbers Step 5: Display the sum Step 6: Display the difference Stop 12

Variables A named location that stores a value A=5 B=3 C=A+B B=4 C=A+B A=7 A=A+3 A=A+C 13 A B C

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 14 1 2 3 4 A B C D

Algorithm: Start Step 1: Read Number 1 Step 2: Read Number 2 Step 3: Sum = Number 1 + Number 2 Step 4: Difference = Number 1 - Number 2 Step 5: DISPLAY Sum Step 6: DISPLAY Difference Stop 15

Arithmetic operators • • • 16 + * / = add subtract multiply divide assign

Relational operators. • • 17 == equal to (a pair of equals signs) <= less than or equal to >= greater than or equal to <> not equal to

Logical operators • • • 18 AND OR NOT

IF (x = = 32) AND (y = = 7) THEN sumxy = x + y 19

IF (letter = = 'A') OR (letter = = 'E') THEN DISPLAY 'Vowel‘ IF NOT (letter = = 'A') THEN DISPLAY 'Not letter A' 20

Order of precedence for operators • • • 21 ( ) parentheses NOT * / AND + - OR == > < <> >= <= from left to right.

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

• 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 23

A flowchart that reads the name of the students in the class and displays it. Start Read the name of the student Display the name of the student Is there any more student in the class? no Stop 24 yes

Draw a flowchart to add all the numbers from 1 to 20. Start Number =1 Sum =0 Sum=Sum+Number Is Number=20 Display Sum Stop 25 Number =Number+1

Psuedocode 1. 2. 3. 4. 5. 6. 7. 8. 26 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

27

• 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. 28

Start Get the mark of the Student Mark>=80 ? Mark>=60 ? Mark>=50 ? Mark>=40 ? 29 Display Grade F yes Display Grade A yes Display Grade B yes Display Grade C Display Grade D Are there more students? Stop

• 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. 30

Start Get x, y, z Is x > y yes Is x > z no Is y > z no no Display z is largest yes Display y is largest 31 Stop yes Display x is largest

Some rules for flow charts • • 32 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)
- Slides: 32