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

Cs 1114 - lecture-10

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Cs 1114 - lecture-10

Instructor : Muhammad Haris

All Rights Reserved to Department of Computer Science – GCU Lahore

Programming Fundamentals

Page 2: Cs 1114 - lecture-10

Test Yourself

Find sum of all integers between any two numbersHow many times you have to repeat it?

Programming Fundamentals | Lecture-10 2

Page 3: Cs 1114 - lecture-10

Consider this Example

Find even and odd numbers from first n natural numbersWhat’s different in this example?

○ Let’s have a look at the solutions of finding even and odd numbers separately

Programming Fundamentals | Lecture-10 3

Page 4: Cs 1114 - lecture-10

Find first n even numbers

Programming Fundamentals | Lecture-9 4

START

count = count + 1

STOP

count < n

count = 0

YesNo

DISPLAY even

even = 0

even = even + 2

All odd numbers are skipped

by this solution

Page 5: Cs 1114 - lecture-10

Find first n odd numbers

Programming Fundamentals | Lecture-9 5

START

count = count + 1

STOP

count < n

count = 0

YesNo

DISPLAY odd

odd = -1

odd = odd + 2

All even numbers are skipped

by this solution

Page 6: Cs 1114 - lecture-10

What’s Required

We have to consider all odd and even numbers up to nIt is quite obvious that our solution needs to

decide whether a number is even or odd○ Hence, we’ll use a decision box while finding a

number in every repetitionWhat should be the decision rule? Or how can we

decide that a number that is even or odd?- If we divide a number by 2 and remainder is 0 (the

number is divisible by 2), it is considered as an even number

- How can we check the remainder of a division?

Programming Fundamentals | Lecture-10 6

Page 7: Cs 1114 - lecture-10

Remainder Operator

An operator like “+”, “-”, “/” etc. denoted by “%”, that performs the division between two number and gives you the remainderExamples:

○ 4%2 will give 0 as answer because remainder of 4/2 is “0”

○ 9%2 will give you “1”○ 11%3 = ?○ 54%5 = ?

Programming Fundamentals | Lecture-10 7

Page 8: Cs 1114 - lecture-10

Programming Fundamentals | Lecture-10 8

START

count = count + 1

STOP

count < n

count = 0

No

No

DISPLAY count “is odd”

READ n

count % 2 == 0

DISPLAY count “is even”

Yes

Yes

Page 9: Cs 1114 - lecture-10

Important Observation

Any kind of decision making can be done while repeatedly doing a taskIt can range from simple decision making to

complex series of decision makings as you learnt in your previous lessons

Programming Fundamentals | Lecture-10 9

Page 10: Cs 1114 - lecture-10

Try this Yourself

Find all numbers which are divisible by 3 from first n integers

Find all numbers which are divisible by a number from first n integers

Programming Fundamentals | Lecture-10 10

Page 11: Cs 1114 - lecture-10

Programming Fundamentals | Lecture-10 11

START

count = count + 1

STOP

count < n

count = 0

YesNo

READ n

count % 3 == 0

DISPLAY count

YesNo

Page 12: Cs 1114 - lecture-10

Consider this Example

Find all odd numbers which are divisible by 3 from first n integersHow many decisions are required in every

repetition?

Programming Fundamentals | Lecture-10 12

Page 13: Cs 1114 - lecture-10

Programming Fundamentals | Lecture-10 13

START

count = count + 1

STOP

count < n

count = 0

YesNo

READ n

YesNo count % 2 != 0

count % 3 == 0

DISPLAY count

YesNo

Page 14: Cs 1114 - lecture-10

Consider this Example

Display “multiplication table” of a number only if the number is not equal to “0”

Programming Fundamentals | Lecture-10 14

Page 15: Cs 1114 - lecture-10

Programming Fundamentals | Lecture-10 15

START

count = count + 1

STOP

count < n

count = 0

YesNo

DISPLAY num * count

READ num, n

num != 0

No

YesNo

Page 16: Cs 1114 - lecture-10

Important Observation

“Decision Making” and “Repetitions” can be inter-mixed in either wayRepeatedly doing a task if a certain

condition is true ORWhile repeatedly doing a task, making

certain decisions ORBoth of the above

Programming Fundamentals | Lecture-10 16

Page 17: Cs 1114 - lecture-10

A Premonition

All of you who are not yet practicing well enough and taking this course “lightly and non-seriously” should:Get ready to face hard-time (as things are

going to get worse for them) ORStart practicing before it gets too late

Programming Fundamentals | Lecture-10 17

Page 18: Cs 1114 - lecture-10

Tasks (to be done by next lecture) Display “Fibonacci Series” up to n

Display “Fibonacci Series” up to n, if n is not less than 0

Programming Fundamentals | Lecture-10 18

Page 19: Cs 1114 - lecture-10

Programming Fundamentals | Lecture-10 19

BE PREPAREDFOR QUIZ

INNEXT LECTURE