Introduction to Algorithms 2 Dr Khizar Hayat Associate

  • Slides: 8
Download presentation
Introduction to Algorithms - 2 Dr. Khizar Hayat Associate Prof. of Computer Science

Introduction to Algorithms - 2 Dr. Khizar Hayat Associate Prof. of Computer Science

What can a computer do • An algorithm can be written using six basic

What can a computer do • An algorithm can be written using six basic computer operations: 1. 2. 3. 4. A computer can receive (input) information, e. g. read name get x A computer can display (output) information, e. g. write “the average is “, avg display num print “Hello!” A computer can perform arithmetic operations, e. g. set total + numb A computer can assign value to a variable, e. g. set counter 0 set total price+tax

What can a computer do 5. 6. A computer can compare two pieces of

What can a computer do 5. 6. A computer can compare two pieces of information to select one of two actions, e. g. if number < 0 then add 1 to neg_number else add 1 to pos_number end if A computer can repeat a group of actions, e. g. repeat untill total=50 read num write num set total+1 End repeat OR while total ≤ 50 read num write num set total+1 End while

Algorithm to add two numbers 1. begin 2. get num 1 3. get num

Algorithm to add two numbers 1. begin 2. get num 1 3. get num 2 4. set sum num 1 + num 2 5. display sum 6. end Start Read Num 1 Read Num 2 Sum Num 1 + Num 2 What about: 1. The difference of two numbers? 2. The sum and difference of two numbers? Display Sum Stop

Algorithm to input two numbers and print the smaller number Start 1. begin 2.

Algorithm to input two numbers and print the smaller number Start 1. begin 2. get num 1 3. get num 2 4. if num 1 < num 2 5. then display num 1 as smaller 6. else display num 2 as smaller 7. end if 8. end What about: 1. The largest of two numbers? 2. The largest of three numbers? 3. The median of three numbers? Read Num 1 Read Num 2 Num 1 < Num 2? No Yes Display Num 1 as min Stop Display Num 2 as min

Algorithm to sum five numbers input by the user 1. begin 2. set count

Algorithm to sum five numbers input by the user 1. begin 2. set count 0 3. set sum 0 4. repeat until count=5 5. get num 6. set count+1 7. set sum+num 8. end repeat 9. display sum as the total 10. end Start count 0 sum 0 Read Num count + 1 sum + Num count = 5? What about: 1. Sum of N numbers? 2. Average of N numbers? 3. Entering numbers till ‘-1’ is entered? 4. The sum of first 20 numbers? Yes Display sum as total Stop No

Algorithm to compute the GCD of two numbers 1. begin 2. get maqs 3.

Algorithm to compute the GCD of two numbers 1. begin 2. get maqs 3. get maqs_alaih 4. set baqi maqs mod m_alaih 5. while baqi ≠ 0 do 6. set maqs m_alaih 7. set m_alaih baqi 8. set baqi maqs mod m_alaih 9. end while 10. display m_alaih as GCD 11. end Start Read maqs_alaih Baqi Maqs mod M_Alaih baqi = 0? Yes Display M_Alaih as GCD Stop No Maqs M_Alaih baqi Baqi Maqs mod M_Alaih

Exercises • Draw a flowchart that reads names of 50 students in the class

Exercises • Draw a flowchart that reads names of 50 students in the class and display it. • Page 16 -17 of notes • Page 21 of notes • Any sum in Omani currency? • Factorial of a number?