Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals

Similar presentations


Presentation on theme: "Programming Fundamentals"— Presentation transcript:

1 Programming Fundamentals
Lecture-8

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

3 Programming Fundamentals | Lecture-8
START sum = 0 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

4 Programming Fundamentals | 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

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

6 Programming Fundamentals | Lecture-8
Analyze the Problem 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

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

8 Programming Fundamentals | 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? * Only a decision box can divide the order or flow of program into two alternative paths Programming Fundamentals | Lecture-8

9 Programming Fundamentals | 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 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

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

11 Programming Fundamentals | Lecture-8
How to Test This? 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 2 3 4 5 sum = sum + count 6 10 15 Programming Fundamentals | Lecture-8

12 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

13 Programming Fundamentals | 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

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

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

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

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

18 Programming Fundamentals | Lecture-8
Testing count = 0 fact = 1 n = 5 Repetition-1 Repetition-2 Repetition-3 Repetition-4 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

19 Programming Fundamentals | Lecture-8
Try this Yourself 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

20 Programming Fundamentals | Lecture-9
Another Example 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

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

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

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

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

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

26 Programming Fundamentals | Lecture-9
Consider This 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

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

28 Programming Fundamentals | Lecture-9
Another Example 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

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

30 Programming Fundamentals | Lecture-9
Another Example Find sum of 5 natural numbers starting from 26 Programming Fundamentals | Lecture-9

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

32 Programming Fundamentals | Lecture-9
Testing 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) Programming Fundamentals | Lecture-9

33 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

34 Programming Fundamentals | Lecture-9
A Challenging Task Display the following series for first n numbers in this series …… Programming Fundamentals | Lecture-9

35 Programming Fundamentals | Lecture-9
BE PREPARED FOR QUIZ IN NEXT LECTURE Programming Fundamentals | Lecture-9


Download ppt "Programming Fundamentals"

Similar presentations


Ads by Google