cs 1114 - lecture-9
DESCRIPTION
TRANSCRIPT
![Page 1: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/1.jpg)
Instructor : Muhammad Haris
All Rights Reserved to Department of Computer Science – GCU Lahore
Programming Fundamentals
![Page 2: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/2.jpg)
Assignment-1 SolutionPeriod Meaning
March 21 – April 19 If Month is 3 thenDay must be 21st or aboveOr if Month is 4 thenDay must be 19th or below
Programming Fundamentals | Lecture-9 2
(m == 3 AND d >= 21)OR
(m == 4 AND d <= 19)
![Page 3: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/3.jpg)
Wrong Decision Rules
Programming Fundamentals | Lecture-9 3
m == 3 OR
m == 4
m == 4 OR
m == 5
DISPLAY “Aries”
![Page 4: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/4.jpg)
Wrong Decision Rules
Programming Fundamentals | Lecture-9 4
(m == 3 AND d >= 21 AND d <= 31)
(m == 4 AND d >= 20 AND d <= 30)
![Page 5: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/5.jpg)
Less Better Approach
Programming Fundamentals | Lecture-9 5
m == 3
m == 4 Further rules
![Page 6: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/6.jpg)
Lessons from Previous Lecture If we have to multiply/add etc. series of
numbers using a computer program, we can make it happen by multiplying/adding etc. 2 numbers at a time and then multiplying its result by the next number in the next repetition and we keep on doing so until we get the required resultResult from calculation in previous repetition
is used in the next repetition
Programming Fundamentals | Lecture-9 6
![Page 7: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/7.jpg)
Lessons from Previous Lecture To repeatedly do a task we need to do
the following:
Programming Fundamentals | Lecture-9 7
count = count + 1count < n
count = 0
Yes
1 or more steps
![Page 8: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/8.jpg)
Lessons from Previous Lecture The original purpose of count is to keep
record of number of repetitionsHence, it’s value changes like that
○ 1 2 3 4 5 …...n
However, if needed, count can be used in a calculationE.g., sum = sum + count
○ Note that doing so doesn’t affect the value of count (it just uses it)
Programming Fundamentals | Lecture-9 8
![Page 9: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/9.jpg)
Lessons from Previous Lecture Key steps to follow for repetition
Identify what you have to do repeatedly○ If it is a calculation or something else which is
not fixed, analyze which part of it is changing and how it is changingE.g., sum = sum + _fact = fact * _
○ Can you make use of count for the changing part or do you need some other field
Identify how many times you have to repeat it
Programming Fundamentals | Lecture-9 9
![Page 10: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/10.jpg)
Another Example
Find sum of first 5 even numbersThere 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 10
![Page 11: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/11.jpg)
Sum of first 5 even numbers
Programming Fundamentals | Lecture-9 11
START
sum = 0
count = count + 1
STOP
count < 5
count = 0
DISPLAY sum
YesNo
sum = sum + count + count
![Page 12: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/12.jpg)
Alternative
Programming Fundamentals | Lecture-9 12
START
sum = 0
count = count + 1
STOP
count < 5
count = 0
DISPLAY sum
YesNo
sum = sum + even
even = 0
even = even + 2
![Page 13: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/13.jpg)
Another Example
Find sum of first n odd numbersSimilarly, two ways to do it
Programming Fundamentals | Lecture-9 13
![Page 14: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/14.jpg)
Sum of first n odd numbers
Programming Fundamentals | Lecture-9 14
START
sum = 0
count = count + 1
STOP
count < n
count = 0
DISPLAY sum
YesNo
sum = sum + count + count - 1
![Page 15: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/15.jpg)
Alternative
Programming Fundamentals | Lecture-9 15
START
sum = 0
count = count + 1
STOP
count < 5
count = 0
DISPLAY sum
YesNo
sum = sum + odd
odd = -1
odd = odd + 2
![Page 16: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/16.jpg)
Consider This
Display first n even numbersHow 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 16
![Page 17: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/17.jpg)
Display first n even numbers
Programming Fundamentals | Lecture-9 17
START
count = count + 1
STOP
count < 5
count = 0
YesNo
DISPLAY even
even = 0
even = even + 2
![Page 18: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/18.jpg)
Another Example
Display “multiplication table” of a number up to 10Do we need to save the result of calculation
in every repetition?○ Let’s see the solution
Programming Fundamentals | Lecture-9 18
![Page 19: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/19.jpg)
Table of a number up to 10
Programming Fundamentals | Lecture-9 19
START
count = count + 1
STOP
count < 10
count = 0
YesNo
DISPLAY num * count
READ num
![Page 20: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/20.jpg)
Another Example
Find sum of 5 natural numbers starting from 2626 + 27 + 28 +29 + 30
Programming Fundamentals | Lecture-9 20
![Page 21: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/21.jpg)
Programming Fundamentals | Lecture-9 21
START
sum = 0
count = count + 1
STOP
count < 5
count = 0
DISPLAY sum
YesNo
sum = sum + count + 25
![Page 22: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/22.jpg)
Testing
Programming Fundamentals | Lecture-9 22
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 +2526 =
0 + (1 + 25) 53 =
26 + (2 + 25)81 =
53 + (3 + 25)110 =
81 + (4 + 25)140 =
110 + (5 + 25)
count = 0 sum = 0
![Page 23: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/23.jpg)
A Challenging Task
Display the following series for first n numbers in this series1 2 4 7 11 16 ……
Programming Fundamentals | Lecture-9 23
![Page 24: Cs 1114 - lecture-9](https://reader033.vdocument.in/reader033/viewer/2022061113/5458e176b1af9fc15d8b4c27/html5/thumbnails/24.jpg)
Programming Fundamentals | Lecture-9 24
BE PREPAREDFOR QUIZ
INNEXT LECTURE