Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving & Computer Programming

Similar presentations


Presentation on theme: "Problem Solving & Computer Programming"— Presentation transcript:

1 Problem Solving & Computer Programming
Course Code : 17CS1101 L-T-P structure: 2-4-2 Course Credits : 05 Dr. P. Sai Kiran

2 TEXT BOOKS R. F. Gilberg, B. A. Forouzan, “Data Structures”, 2nd Edition, Thomson India Edition-2005.

3 Reference Books:- Mark Allen weiss, “Data Structures and Algorithm Analysis in C”, 2008, Third Edition, Pearson Education. Horowitz, Sahni, Anderson Freed, “Fundamentals of Datastructures in C”, 2nd Edition Robert Kruse, C. L. Tondo, Bruce Leung, Shashi Mogalla, “Data structures and Program Design in C”, 4th Edition-2007. C for Engineers and Scientists – An Interpretive Approach by Harry H. Cheng, Mc Graw Hill International Edition-2010. Jeri R. Hanly, Elliot B. Koffman, “Problem Solving and Program Design in C”, 7/e, Pearson Education-2004. Jean Paul Trembly Paul G.Sorenson, “An Introduction To Data Structures with applications”, 2nd Edition.

4

5 Lab Continuous Evaluation Evaluation– Internal(50%) External(50%)
Test 1 Weightage (5%) Max Marks (30) Test 2 Test 3 Test 4 Weightage (5 %) LTC Class room assignment : problem solving Weightage (6%) Max Marks (75) Home Assignment Weightage (4%) SE Surprise Test/Quiz Weightage (5%) Max Marks (50) Lab Continuous Evaluation Weightage (5%) Max Marks (50) Lab Exam Weightage (5%) Max Marks (50 ) Attendance Weightage (5%) Evaluation– Internal(50%) External(50%) SE Lab Exam Weightage (5%) Max Marks (50) SE Project Weightage (5%) Max Marks (50) Semester End Exam Weightage (40%) Max Marks(50)

6 Course Objectives

7 1. Problem Solving 2. Understand C Programming Language 3. Understand Basic Data Structures

8 Computer (1646): one that computes; A programmable electronic device that can store, retrieve, and process data based on the Instructions given to it. “Computers are good at following instructions, but not at reading your mind.” - Donald Knuth “Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” ― Bill Gates “But active programming consists of the design of new programs, rather than contemplation of old programs.” - Niklaus Wirth “ When someone says, "I want a programming language in which I need only say what I want done," give him a lollipop. ” - Alan Perlis

9 A typical programming task can be divided into two phases:
Problem solving phase produce an ordered sequence of steps that describe solution of problem this sequence of steps is called an algorithm Implementation phase implement the program in some programming language

10 Algorithm: Instructions for solving a problem or subproblem in a finite amount of time using a finite amount of data Example 1: Write an algorithm that will read the two sides of a rectangle and calculate its area. Pseudocode: Input the width (W) and Length (L) of a rectangle Calculate the area (A) by multiplying L with W Print A Detailed Algorithm Step 1: Input W,L Step 2: A  L x W Step 3: Print A

11 Algorithm Representations
Selection IF: (test condition) Statement(s) to be executed if test condition is TRUE ELSE: Statement(s) to be executed if test condition is FALSE Selection IF: (test condition) Statement(s) to be executed if test condition is TRUE ELSE IF (Second Test Condition if above Is not TRUE): ELSE Statement(s) to be executed if above both test conditions are FALSE

12 Example 2: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. Pseudocode: Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50 Print “FAIL” else Print “PASS” Detailed Algorithm Step 1: Input M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print “FAIL” else Print “PASS” endif

13 Example 3: Write an algorithm that reads two different values, determines the largest value and prints the largest value with an identifying message. Pseudocode: Input two values Value1 and Value2 If value1 is greater than value2 then maximum value will be value 1 other wise maximum value will be value 2 Print maximum value Detailed Algorithm Step 1: Input VALUE1, VALUE2 Step 2: if (VALUE1 > VALUE2) then MAX  VALUE1 else MAX  VALUE2 endif Step 3: Print “The largest value is”, MAX

14 Example 4: Write an algorithm that reads three different numbers and prints the value of the largest number. Pseudocode: Input three values If value1 is greater than value2 and value 3 then maximum value will be value1 If value 2 is greater than value 1 and value 3 then Maximum value will be value2 If both the above are not true maximum value will be value 3 Print maximum value Detailed Algorithm Step 1: Input N1, N2, N3 Step 2: if (N1>N2) and (N1>N3) then MAX  N1 [N1>N2, N1>N3] else if (N2>N1) and (N2>N3) then MAX  N2 [N2>N1, N2>N3] else MAX  N3 [N3>N2>N1] endif Step 3: Print “The largest number is”, MAX

15 Euclid’s algorithm Given two positive integers m and n, find their greatest common divisor, that is, the largest positive integer that evenly divides both m and n. 1. Take input into m and n 2. Divide m by n and let r be the remainder. (We will have 0 ≤ r < n.) 3. If the remainder r is zero then the algorithm terminates; n is the answer. 4. Other wise Set m ← n, n ← r, go back to step 2 Step 1. INPUT m,n Step 2. r ← m%n Step 3. If r = 0 then print “GCD is” n EXIT Step 4. else m ← n, n ← r goto Step 2 Test Case 1: m=119 and n=544 Test Case 2: m=27 and n=7

16 important features/Properties/ characteristics
1. Finiteness: An algorithm must always terminate after a finite number of steps 2. Definiteness. Each step of an algorithm must be precisely defined; the actions to be carried out must be rigorously and unambiguously specified for each case. 3.Input. An algorithm has zero or more inputs. quantities that are given to it initially before the algorithm begins, or dynamically as the algorithm runs. An Algorithm has Five important features/Properties/ characteristics 4. Output. An algorithm has one or more outputs : quantities that have a specified relation to the inputs. 5. Effectiveness. An algorithm is also generally expected to be effective, in the sense that its operations must all be sufficiently basic that they can in principle be done exactly and in a finite length of time by someone using pencil and paper.

17 Algorithm Representations - Repetition
LOOP: WHILE: (test condition) Statement(s) to be executed if test condition is TRUE END LOOP LOOP: Statement(s) to be executed if test condition is TRUE WHILE: (test condition) END LOOP LOOP: UNTIL: (test condition) Statement(s) to be executed if test condition is FALSE END LOOP LOOP: Statement(s) to be executed if test condition is FALSE UNTIL: (test condition) END LOOP

18 Give the Sum of 10 Numbers from 1
Add 10 Numbers Give the Sum of 10 Numbers from 1 Step 1. counter← 1, sum ←0 Step 2. LOOP WHILE counter<=10 sum ←sum + counter counter ←counter + 1 END LOOP Step 4. print sum Step 1. counter← 1, sum ←0 Step 2. LOOP UNTIL counter>10 sum ←sum + counter counter ←counter + 1 END LOOP Step 4. print sum Step 1. counter← 1, sum ←0 Step 2. LOOP sum ←sum + counter counter ←counter + 1 WHILE counter<=10 END LOOP Step 4. print sum Step 1. counter← 1, sum ←0 Step 2. LOOP sum ←sum + counter counter ←counter + 1 UNTIL counter>10 END LOOP Step 4. print sum

19 Given a Positive Number N, Find if the number is Prime or Not.
Prime Number Given a Positive Number N, Find if the number is Prime or Not. (A Number is Prime if it is Divisible by 1 and itself) Step 1. INPUT n Step 2. m ← 1 , count ← 0 LOOP WHILE m<=n if(n%m=0) count ← count+1 m ← m+1 END LOOP Step 4. If (count=2) print “PRIME” else print “NOT PRIME” 1. Take input n 2. start with 1 and check for every number from 1 to n if it divides n. 3. Count every time n is divisible. 4. If the count is 2 then the number n is prime. Other wise it is not prime. Test Case 1: n=7 Test Case 2: n=10

20 Given a Positive Number N, Find if the number is Prime or Not.
Prime Number Given a Positive Number N, Find if the number is Prime or Not. (A Number is Prime if it is Divisible by 1 and itself) Step 1. INPUT n Step 2. m ← 2 , count ← 0 LOOP WHILE m<n if(n%m=0) count ← count+1 BREAK LOOP m=m+1 END LOOP Step 4. If (count=0) print “PRIME” else print “NOT PRIME” Step 1. INPUT n Step 2. m ← 2 , count ← 0 LOOP WHILE m<n/2 if(n%m=0) count ← count+1 BREAK LOOP m=m+1 END LOOP Step 4. If (count=0) print “PRIME” else print “NOT PRIME”

21 A graphical representation of the sequence of operations in a program
Flow Chart A graphical representation of the sequence of operations in a program

22 Step 1: Input W,L Step 2: A  L x W Step 3: Print A
Example 1: Write an algorithm that will read the two sides of a rectangle and calculate its area. Detailed Algorithm Step 1: Input W,L Step 2: A  L x W Step 3: Print A START W, L A  L x W A STOP

23 Statement(s) to be executed if test condition is TRUE ELSE:
Selection IF: (test condition) Statement(s) to be executed if test condition is TRUE ELSE: Statement(s) to be executed if test condition is FALSE Test Condition If False If True Y N If False If True Y N Second Test Condition Test Condition Selection IF: (test condition) Statement(s) TRUE ELSE IF (Second Test Condition TRUE): ELSE Statement(s) to be executed if above both test conditions are FALSE

24 Step 2: GRADE  (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then
Example 2: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. Detailed Algorithm Step 1: Input M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print “FAIL” else Print “PASS” endif START M1,M2,M3,M4 GRADE(M1+M2+M3+M4)/4 IS GRADE<50 “FAIL” STOP Y N “PASS”

25 “The largest value is”, MAX
Example 3: Write an algorithm that reads two different values, determines the largest value and prints the largest value with an identifying message. Y MAX  VALUE1 “The largest value is”, MAX STOP START VALUE1,VALUE2 MAX  VALUE2 is VALUE1>VALUE2 N Detailed Algorithm Step 1: Input VALUE1, VALUE2 Step 2: if (VALUE1 > VALUE2) then MAX  VALUE1 else MAX  VALUE2 endif Step 3:Print “The largest value is”, MAX

26 Example 4: Write an algorithm that reads three different numbers and prints the value of the largest number. Detailed Algorithm Step 1: Input N1, N2, N3 Step 2: if (N1>N2) and (N1>N3) then MAX  N1 [N1>N2, N1>N3] else if (N2>N1) and (N2>N3) then MAX  N2 [N2>N1, N2>N3] else MAX  N3 [N3>N2>N1] endif Step 3: Print “The largest number is”, MAX

27 Flowchart Representations - Repetition
LOOP: WHILE: (test condition) Statement(s) to be executed if test condition is TRUE END LOOP Test Condition Statement YES NO Test Condition Statement NO LOOP: UNTIL: (test condition) Statement(s) to be executed if test condition is FALSE END LOOP YES

28 Euclid’s algorithm Given two positive integers m and n, find their greatest common divisor, that is, the largest positive integer that evenly divides both m and n. m,n r ← m%n r=0 m ← n, n ← r Start “GCD is” n End Yes No Step 1. INPUT m,n Step 2. r ← m%n Step 3. If r = 0 then print “GCD is” n EXIT Step 4. else m ← n, n ← r goto Step 2

29 Give the Sum of 10 Numbers from 1
Add 10 Numbers Give the Sum of 10 Numbers from 1 Step 1. counter← 1, sum ←0 Step 2. LOOP WHILE counter<=10 sum ←sum + counter counter ←counter + 1 END LOOP Step 4. print sum counter← 1, sum ←0 Counter<=10 sum ←sum + counter counter ←counter + 1 Start sum End No Yes

30 Give the Sum of 10 Numbers from 1
Add 10 Numbers Give the Sum of 10 Numbers from 1 counter← 1, sum ←0 Counter>10 sum ←sum + counter counter ←counter + 1 Start sum End Yes No Step 1. counter← 1, sum ←0 Step 2. LOOP UNTIL counter>10 sum ←sum + counter counter ←counter + 1 END LOOP Step 4. print sum

31 Give the Sum of 10 Numbers from 1
Add 10 Numbers Give the Sum of 10 Numbers from 1 counter← 1, sum ←0 Counter<=10 sum ←sum + counter counter ←counter + 1 Start sum End No Yes Step 1. counter← 1, sum ←0 Step 2. LOOP sum ←sum + counter counter ←counter + 1 WHILE counter<=10 END LOOP Step 4. print sum

32 Give the Sum of 10 Numbers from 1
Add 10 Numbers Give the Sum of 10 Numbers from 1 counter← 1, sum ←0 Counter>10 sum ←sum + counter counter ←counter + 1 Start sum End Yes No Step 1. counter← 1, sum ←0 Step 2. LOOP sum ←sum + counter counter ←counter + 1 UNTIL counter>10 END LOOP Step 4. print sum

33 Given a Positive Number N, Find if the number is Prime or Not.
Prime Number Given a Positive Number N, Find if the number is Prime or Not. m ← 2 , count ← 0 m<√n count ← count+1 Start PRIME End Yes No n n%m=0 count=0 NOT PRIME m← m+1 Step 1. INPUT n Step 2. m ← 2 , count ← 0 LOOP WHILE m<√n if(n%m=0) count ← count+1 BREAK LOOP m ←m+1 END LOOP Step 4. If (count=0) print “PRIME” else print “NOT PRIME”

34


Download ppt "Problem Solving & Computer Programming"

Similar presentations


Ads by Google