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

Cs 1114 - lecture-8

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Cs 1114 - lecture-8

Instructor : Muhammad Haris

All Rights Reserved to Department of Computer Science – GCU Lahore

Programming Fundamentals

Page 2: Cs 1114 - lecture-8

Consider this Example

Find sum of first 5 natural numbers

Programming Fundamentals | Lecture-8 2

Page 3: Cs 1114 - lecture-8

Programming Fundamentals | Lecture-8 3

START

STOP

sum = 0

sum = sum + 2

sum = sum + 3

sum = sum + 4

sum = sum + 5

DISPLAY sum

(sum=1)

(sum=3)

(sum=6)

(sum=10)

(sum=15)

sum = sum + 1 (sum=0)

Page 4: Cs 1114 - lecture-8

Consider This

What if we have to find sum of first 50 natural numbers?Should we write down 50 processing steps?

Programming Fundamentals | Lecture-8 4

Page 5: Cs 1114 - lecture-8

Programming Fundamentals | Lecture-8 5

What if the numbers are not just 50, they are 1000?

Page 6: Cs 1114 - lecture-8

Analyze the Problem

Notice that you are doing the same operation of addition again and againThe 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

Page 7: Cs 1114 - lecture-8

Is this Correct?

Programming Fundamentals | Lecture-8 7

START

sum = 0

sum = sum + _

STOP

Page 8: Cs 1114 - lecture-8

What is Required? 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

Page 9: Cs 1114 - lecture-8

How to Achieve This?

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 decisionHence 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

Page 10: Cs 1114 - lecture-8

Sum of 1st 5 natural numbers

Programming Fundamentals | Lecture-8 10

START

sum = 0

count = count + 1

STOP

count < 5

count = 0

DISPLAY sum

YesNo

sum = sum + count

Page 11: Cs 1114 - lecture-8

How to Test This?

Programming Fundamentals | Lecture-8 11

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

count = 0 sum = 0

Page 12: Cs 1114 - lecture-8

Power of Computer Programs 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

Page 13: Cs 1114 - lecture-8

Steps to Follow

Which instructions are needed to be repeated?

How many times the repetition is to be done?

Programming Fundamentals | Lecture-8 13

Page 14: Cs 1114 - lecture-8

Extending that Example

Find sum of first n natural numbersWhere n can be any number

Programming Fundamentals | Lecture-8 14

Page 15: Cs 1114 - lecture-8

Programming Fundamentals | Lecture-8 15

START

sum = 0

count = count + 1

STOP

count < n

count = 0

DISPLAY sum

YesNo

sum = sum + count

READ n

Page 16: Cs 1114 - lecture-8

Another Example

Find factorial of a numberfactorial(n) = 1 * 2 * …… * n

Programming Fundamentals | Lecture-8 16

Page 17: Cs 1114 - lecture-8

Programming Fundamentals | Lecture-8 17

START

fact = 1

count = count + 1

STOP

count < n

count = 0

DISPLAY sum

YesNo

fact = fact * count

READ n

Page 18: Cs 1114 - lecture-8

Testing

Programming Fundamentals | Lecture-8 18

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

fact = fact * count 1 = 1 * 1 2 = 2 *1 6 = 2 * 3 24 = 6 * 4 120 = 24 * 5

count = 0 fact = 1 n = 5

Page 19: Cs 1114 - lecture-8

Try this Yourself

Find 220

Hint: Multiply 2 by itself, 20 times

Find xy

x and y can be any numbersXy is basically equivalent to x multiplied by

itself y times

Programming Fundamentals | Lecture-8 19

Page 20: Cs 1114 - lecture-8

Tasks (to be done by next lecture)

Find the sum of square of first n natural numbers

Find first 5 multiples of a numberHint: Multiply the number from 1 to 5

Page 21: Cs 1114 - lecture-8

Programming Fundamentals | Lecture-8 21

BE PREPAREDFOR QUIZ

INNEXT LECTURE