Department of Computer Science GCU Programming Fundamentals LECTURE8

  • Slides: 35
Download presentation
Department of Computer Science GCU Programming Fundamentals LECTURE-8 All Rights Reserved to Department of

Department of Computer Science GCU Programming Fundamentals LECTURE-8 All Rights Reserved to Department of Computer Science – GCU Lahore Instructor : Muhammad Haris

Consider this Example Department of Computer Science Find sum of first 5 natural numbers

Consider this Example Department of Computer Science Find sum of first 5 natural numbers Programming Fundamentals | Lecture-8 2

START sum = 0 Department of Computer Science sum = sum + 1 (sum=0)

START sum = 0 Department of Computer Science sum = sum + 1 (sum=0) sum = sum + 2 (sum=1) sum = sum + 3 (sum=3) sum = sum + 4 (sum=6) sum = sum + 5 (sum=10) DISPLAY sum (sum=15) STOP Programming Fundamentals | Lecture-8 3

Consider This Department of Computer Science What if we have to find sum of

Consider This Department of Computer Science What if we have to find sum of first 50 natural numbers? Should we write down 50 processing steps? Programming Fundamentals | Lecture-8 4

Department of Computer Science What if the numbers are not just 50, they are

Department of Computer Science What if the numbers are not just 50, they are 1000? Programming Fundamentals | Lecture-8 5

Analyze the Problem Department of Computer Science Notice that you are doing the same

Analyze the Problem Department of Computer Science Notice that you are doing the same operation of addition again and again The only difference is the value which is being added If we could somehow instruct the computer to repeat certain number of operations for certain number of times, our job will be much more easier Programming Fundamentals | Lecture-8 6

Is this Correct? Department of Computer Science START sum = 0 sum = sum

Is this Correct? Department of Computer Science START sum = 0 sum = sum + _ STOP Programming Fundamentals | Lecture-8 7

What is Required? Department of Computer Science Your program needs to decide after every

What is Required? Department of Computer Science Your program needs to decide after every repetition that whether further repetition is required or not? Whether number of repetitions has reached its limit or not? As the program needs to “decide”, it is clear that we need to use a “decision box” What will be the basis of decision? Programming Fundamentals | Lecture-8 8

How to Achieve This? Department of Computer Science Need to keep record of number

How to Achieve This? Department of Computer Science Need to keep record of number of repetitions (repetitions count) Make use of a field whose purpose is to keep record of no. of repetitions which are done so far That field (or repetition count) will become the basis of decision Hence your program will be able to control the repetitions and will proceed further after the desired no. of repetitions are done Programming Fundamentals | Lecture-8 9

st 1 Sum of 5 natural numbers Department of Computer Science START sum =

st 1 Sum of 5 natural numbers Department of Computer Science START sum = 0 count = 0 No count < 5 DISPLAY sum Yes count = count + 1 sum = sum + count STOP Programming Fundamentals | Lecture-8 10

How to Test This? count = 0 Department of Computer Science sum = 0

How to Test This? count = 0 Department of Computer Science sum = 0 Repetition-1 Repetition-2 Repetition-3 Repetition-4 Repetition-5 decision (count < 5) 0<5 1<5 2<5 3<5 4<5 count = count + 1 1 2 3 4 5 sum = sum + count 1 3 6 10 15 Programming Fundamentals | Lecture-8 11

Power of Computer Programs Department of Computer Science Controlled Repetition Programmers can instruct computers

Power of Computer Programs Department of Computer Science Controlled Repetition Programmers can instruct computers to do certain tasks repeatedly for certain period of time ○ As the complexity of problems increases, repetition may become more extensive This way computers prove very useful by performing repeating tasks themselves without the intervention of human beings Programming Fundamentals | Lecture-8 12

Steps to Follow Department of Computer Science Which instructions are needed to be repeated?

Steps to Follow Department of Computer Science Which instructions are needed to be repeated? How many times the repetition is to be done? Programming Fundamentals | Lecture-8 13

Extending that Example Department of Computer Science Find sum of first n natural numbers

Extending that Example Department of Computer Science Find sum of first n natural numbers Where n can be any number Programming Fundamentals | Lecture-8 14

START Department of Computer Science READ n sum = 0 count = 0 No

START Department of Computer Science READ n sum = 0 count = 0 No count < n Yes count = count + 1 sum = sum + count DISPLAY sum STOP Programming Fundamentals | Lecture-8 15

Another Example Department of Computer Science Find factorial of a number factorial(n) = 1

Another Example Department of Computer Science Find factorial of a number factorial(n) = 1 * 2 * …… * n Programming Fundamentals | Lecture-8 16

START Department of Computer Science READ n fact = 1 count = 0 No

START Department of Computer Science READ n fact = 1 count = 0 No count < n-1 Yes count = count + 1 fact = fact * (count + 1) DISPLAY sum STOP Programming Fundamentals | Lecture-8 17

Testing count = 0 fact = 1 Department of Computer Science n=5 Repetition-1 Repetition-2

Testing count = 0 fact = 1 Department of Computer Science n=5 Repetition-1 Repetition-2 decision (count < 5) 0 < 5 -1 1 < 5 -1 2 < 5 -1 3 < 5 -1 count = count + 1 1=0+1 2=1+1 3=2+1 4=3+1 fact = fact * (count +1) 2 = 1 * (1 + 1) 6 = 2 * (2 + 1) 24 = 6 + (3 + 1) 120 = 24 * (4 + 1) Programming Fundamentals | Lecture-8 Repetition-3 Repetition-4 18

Try this Yourself Department of Computer Science Find 220 Hint: Multiply 2 by itself,

Try this Yourself Department of Computer Science Find 220 Hint: Multiply 2 by itself, 19 times Find xy x and y can be any numbers Xy is basically equivalent to x multiplied by itself y-1 times Programming Fundamentals | Lecture-8 19

Another Example Department of Computer Science Find sum of first 5 even numbers There

Another Example Department of Computer Science Find sum of first 5 even numbers There are two ways to do this ○ Make use of count OR ○ Use a special field instead of count What about sum of first ‘n’ even numbers Programming Fundamentals | Lecture-9 20

Sum of first 5 even numbers Department of Computer Science START sum = 0

Sum of first 5 even numbers Department of Computer Science START sum = 0 count = 0 No count < 5 Yes count = count + 1 sum = sum + count DISPLAY sum STOP Programming Fundamentals | Lecture-9 21

Alternative Department of Computer Science START sum = 0 even = 0 count =

Alternative Department of Computer Science START sum = 0 even = 0 count = 0 No count < 5 Yes count = count + 1 even = even + 2 DISPLAY sum STOP sum = sum + even Programming Fundamentals | Lecture-9 22

Another Example Department of Computer Science Find sum of first n odd numbers Similarly,

Another Example Department of Computer Science Find sum of first n odd numbers Similarly, two ways to do it Programming Fundamentals | Lecture-9 23

Sum of first n odd numbers Department of Computer Science START sum = 0

Sum of first n odd numbers Department of Computer Science START sum = 0 count = 0 No count < n Yes sum = sum + count + 1 DISPLAY sum count = count + 1 STOP Programming Fundamentals | Lecture-9 24

Alternative Department of Computer Science START sum = 0 odd = -1 count =

Alternative Department of Computer Science START sum = 0 odd = -1 count = 0 No count < 5 Yes count = count + 1 odd = odd + 2 DISPLAY sum STOP sum = sum + odd Programming Fundamentals | Lecture-9 25

Consider This Department of Computer Science Display first n even numbers How it is

Consider This Department of Computer Science Display first n even numbers How it is different from the earlier example? If we don’t need to use the result of previous calculation, we don’t need to save it Programming Fundamentals | Lecture-9 26

Display first n even numbers Department of Computer Science START even = 0 count

Display first n even numbers Department of Computer Science START even = 0 count = 0 No count < 5 Yes count = count + 1 even = even + 2 STOP DISPLAY even Programming Fundamentals | Lecture-9 27

Another Example Department of Computer Science Display “multiplication table” of a number up to

Another Example Department of Computer Science Display “multiplication table” of a number up to 10 Do we need to save the result of calculation in every repetition? ○ Let’s see the solution Programming Fundamentals | Lecture-9 28

Table of a number up to 10 Department of Computer Science START READ num

Table of a number up to 10 Department of Computer Science START READ num count = 0 No count < 10 Yes count = count + 1 DISPLAY num * count STOP Programming Fundamentals | Lecture-9 29

Another Example Department of Computer Science Find sum of 5 natural numbers starting from

Another Example Department of Computer Science Find sum of 5 natural numbers starting from 26 26 + 27 + 28 +29 + 30 Programming Fundamentals | Lecture-9 30

START Department of Computer Science sum = 0 count = 0 No count <

START Department of Computer Science sum = 0 count = 0 No count < 5 Yes count = count + 1 sum = sum + count + 25 DISPLAY sum STOP Programming Fundamentals | Lecture-9 31

Testing count = 0 Department of Computer Science sum = 0 Repetition-1 Repetition-2 Repetition-3

Testing count = 0 Department of Computer Science sum = 0 Repetition-1 Repetition-2 Repetition-3 Repetition-4 Repetition-5 decision (count < 5) 0<5 1<5 2<5 3<5 4<5 count = count + 1 1=0+1 2=1+1 3=2+1 4=3+1 5=4+1 sum = sum + count +25 26 = 0 + (1 + 25) 53 = 26 + (2 + 25) 81 = 53 + (3 + 25) 110 = 81 + (4 + 25) 140 = 110 + (5 + 25) Programming Fundamentals | Lecture-9 32

Tasks (to be done by next lecture) Find the sum of square of first

Tasks (to be done by next lecture) Find the sum of square of first n numbers Find first 5 multiples of a number Hint: Multiply the number from 1 to 5 Department of Computer Science

A Challenging Task Department of Computer Science Display the following series for first n

A Challenging Task Department of Computer Science Display the following series for first n numbers in this series 1 2 4 7 11 16 …… Programming Fundamentals | Lecture-9 34

Department of Computer Science BE PREPARED FOR QUIZ IN NEXT LECTURE Programming Fundamentals |

Department of Computer Science BE PREPARED FOR QUIZ IN NEXT LECTURE Programming Fundamentals | Lecture-9 35