Successfully reported this slideshow.
Upcoming SlideShare
×

# Cs 1114 - lecture-9

452 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Cs 1114 - lecture-9

1. 1. Programming Fundamentals
2. 2. Assignment-1 Solution Programming Fundamentals | Lecture-9 (m == 3 AND d >= 21) OR (m == 4 AND d <= 19) Period Meaning March 21 – April 19 If Month is 3 then Day must be 21 st or above Or if Month is 4 then Day must be 19 th or below
3. 3. Wrong Decision Rules Programming Fundamentals | Lecture-9 m == 3 OR m == 4 m == 4 OR m == 5 DISPLAY “Aries”
4. 4. Wrong Decision Rules Programming Fundamentals | Lecture-9 (m == 3 AND d >= 21 AND d <= 31) (m == 4 AND d >= 20 AND d <= 30)
5. 5. Less Better Approach Programming Fundamentals | Lecture-9 m == 3 m == 4 Further rules
6. 6. Lessons from Previous Lecture <ul><li>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 result </li></ul><ul><ul><li>Result from calculation in previous repetition is used in the next repetition </li></ul></ul>Programming Fundamentals | Lecture-9
7. 7. Lessons from Previous Lecture <ul><li>To repeatedly do a task we need to do the following: </li></ul>Programming Fundamentals | Lecture-9 count = count + 1 count < n count = 0 Yes 1 or more steps
8. 8. Lessons from Previous Lecture <ul><li>The original purpose of count is to keep record of number of repetitions </li></ul><ul><ul><li>Hence, it’s value changes like that </li></ul></ul><ul><ul><ul><li>1  2  3  4  5  …...n </li></ul></ul></ul><ul><li>However, if needed , count can be used in a calculation </li></ul><ul><ul><li>E.g., sum = sum + count </li></ul></ul><ul><ul><ul><li>Note that doing so doesn’t affect the value of count (it just uses it) </li></ul></ul></ul>Programming Fundamentals | Lecture-9
9. 9. Lessons from Previous Lecture <ul><li>Key steps to follow for repetition </li></ul><ul><ul><li>Identify what you have to do repeatedly </li></ul></ul><ul><ul><ul><li>If it is a calculation or something else which is not fixed, analyze which part of it is changing and how it is changing </li></ul></ul></ul><ul><ul><ul><ul><li>E.g., sum = sum + _ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>fact = fact * _ </li></ul></ul></ul></ul><ul><ul><ul><li>Can you make use of count for the changing part or do you need some other field </li></ul></ul></ul><ul><ul><li>Identify how many times you have to repeat it </li></ul></ul>Programming Fundamentals | Lecture-9
10. 10. Another Example <ul><li>Find sum of first 5 even numbers </li></ul><ul><ul><li>There are two ways to do this </li></ul></ul><ul><ul><ul><li>Make use of count OR </li></ul></ul></ul><ul><ul><ul><li>Use a special field instead of count </li></ul></ul></ul><ul><li>What about sum of first ‘n’ even numbers </li></ul>Programming Fundamentals | Lecture-9
11. 11. Sum of first 5 even numbers Programming Fundamentals | Lecture-9 START sum = 0 count = count + 1 STOP count < 5 count = 0 DISPLAY sum Yes No sum = sum + count + count
12. 12. Alternative Programming Fundamentals | Lecture-9 START sum = 0 count = count + 1 STOP count < 5 count = 0 DISPLAY sum Yes No sum = sum + even even = 0 even = even + 2
13. 13. Another Example <ul><li>Find sum of first n odd numbers </li></ul><ul><ul><li>Similarly, two ways to do it </li></ul></ul>Programming Fundamentals | Lecture-9
14. 14. Sum of first n odd numbers Programming Fundamentals | Lecture-9 START sum = 0 count = count + 1 STOP count < n count = 0 DISPLAY sum Yes No sum = sum + count + count - 1
15. 15. Alternative Programming Fundamentals | Lecture-9 START sum = 0 count = count + 1 STOP count < 5 count = 0 DISPLAY sum Yes No sum = sum + odd odd = -1 odd = odd + 2
16. 16. Consider This <ul><li>Display first n even numbers </li></ul><ul><ul><li>How it is different from the earlier example? </li></ul></ul><ul><li>If we don’t need to use the result of previous calculation, we don’t need to save it </li></ul>Programming Fundamentals | Lecture-9
17. 17. Display first n even numbers Programming Fundamentals | Lecture-9 START count = count + 1 STOP count < 5 count = 0 Yes No DISPLAY even even = 0 even = even + 2
18. 18. Another Example <ul><li>Display “multiplication table” of a number up to 10 </li></ul><ul><ul><li>Do we need to save the result of calculation in every repetition? </li></ul></ul><ul><ul><ul><li>Let’s see the solution </li></ul></ul></ul>Programming Fundamentals | Lecture-9
19. 19. Table of a number up to 10 Programming Fundamentals | Lecture-9 START count = count + 1 STOP count < 10 count = 0 Yes No DISPLAY num * count READ num
20. 20. Another Example <ul><li>Find sum of 5 natural numbers starting from 26 </li></ul><ul><ul><li>26 + 27 + 28 +29 + 30 </li></ul></ul>Programming Fundamentals | Lecture-9
21. 21. Programming Fundamentals | Lecture-9 START sum = 0 count = count + 1 STOP count < 5 count = 0 DISPLAY sum Yes No sum = sum + count + 25
22. 22. Testing Programming Fundamentals | Lecture-9 count = 0 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)
23. 23. A Challenging Task <ul><li>Display the following series for first n numbers in this series </li></ul><ul><ul><li>1 2 4 7 11 16 …… </li></ul></ul>Programming Fundamentals | Lecture-9
24. 24. Programming Fundamentals | Lecture-9 BE PREPARED FOR QUIZ IN NEXT LECTURE