Unit I PROBLEM SOLVING AND PYTHON PROGRAMMING.pdf
Document Details
Related
Full Transcript
Poriyaalan Lecturer Notes GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING What is Computational thinking? Computational thinking is the thought processes involved in formulating a problem and expressing its solution(s) in such a way that a computer—human or machine—can effectively carry o...
Poriyaalan Lecturer Notes GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING What is Computational thinking? Computational thinking is the thought processes involved in formulating a problem and expressing its solution(s) in such a way that a computer—human or machine—can effectively carry out.’ Example: Predicting climate change Predicting global climate change is only possible because of advanced computer models. Components: decomposition, pattern recognition, abstraction, and algorithms Decomposition invites students to break down complex problems into smaller, simpler problems. Pattern recognition guides students to make connections between similar problems and experience. Abstraction- process of filtering out Types of computation problem: Decision problem, search problem, counting problem, optimization problem. PROBLEM SOLVING Problem solving is the systematic approach to define the problem and creating number of solutions. The problem-Solving process starts with the problem specifications and ends with a Correct program. PROBLEM SOLVING TECHNIQUES Problem solving technique is a set of techniques that helps in providing logic for solving a problem. Problem Solving Techniques: Problem solving can be expressed in the form of 1. Algorithms. 2. Flowcharts. 3. Pseudo codes. 4. programs Poriyaalan Lecturer Notes ALGORITHM It is defined as a sequence of instructions that describe a method for solving a problem. In other words it is a step by step procedure for solving a problem. Properties of Algorithms Should be written in simple English Each and every instruction should be precise and unambiguous. Instructions in an algorithm should not be repeated infinitely. Algorithm should conclude after a finite number of steps. Should have an end point Derived results should be obtained only after the algorithm terminates. Qualities of a good algorithm The following are the primary factors that are often used to judge the quality of the algorithms. Time – To execute a program, the computer system takes some amount of time. The lesser is the time required, the better is the algorithm. Memory – To execute a program, computer system takes some amount of memory space. The lesser is the memory required, the better is the algorithm. Accuracy – Multiple algorithms may provide suitable or correct solutions to a given problem, some of these may provide more accurate results than others, and such algorithms may be suitable. Example Write an algorithm to print „Good Morning” Step 1: Start Step 2: Print “Good Morning” Step 3: Stop Poriyaalan Lecturer Notes BUILDING BLOCKS OF ALGORITHMS (statements, state, control flow, functions) Algorithms can be constructed from basic building blocks namely, sequence, selection and iteration. Statements: Statement is a single action in a computer. In a computer statements might include some of the following actions input data-information given to the program process data-perform operation on a given input output data-processed result State: Transition from one process to another process under specified condition with in a time is called state. Control flow: The process of executing the individual statements in a given order is called control flow. The control can be executed in three ways 1. sequence 2. selection 3. iteration Sequence: All the instructions are executed one after another is called sequence execution. Example: Add two numbers: Step 1: Start Step 2: get a,b Step 3: calculate c=a+b Step 4: Display c Step 5: Stop Poriyaalan Lecturer Notes Selection: A selection statement causes the program control to be transferred to a specific part of the program based upon the condition. If the conditional test is true, one part of the program will be executed, otherwise it will execute the other part of the program. Example Write an algorithm to check whether he is eligible to vote? Step 1: Start Step 2: Get age Step 3: if age >= 18 print “Eligible to vote” Step 4: else print “Not eligible to vote” Step 6: Stop Poriyaalan Lecturer Notes Iteration: In some programs, certain set of statements are executed again and again based upon conditional test. i.e. executed more than one time. This type of execution is called looping or iteration. Example Write an algorithm to print all natural numbers up to n Step 1: Start Step 2: get n value. Step 3: initialize i=1 Step 4: if (ib) THEN DISPLAY a is greater ELSE DISPLAY b is greater END IF END Poriyaalan Lecturer Notes Syntax for For: FOR( start-value to end-value) DO statement... ENDFOR Example: Print n natural numbers BEGIN GET n INITIALIZE i=1 FOR (i0) THEN DISPLAY num is positive ELSE DISPLAY num is negative END IF END Poriyaalan Lecturer Notes To check odd or even number Step 1: Start Step 2: get num Step 3: check if(num%2==0) print num is even Step 4: else num is odd Step 5: Stop BEGIN READ num IF (num%2==0) THEN DISPLAY num is even ELSE DISPLAY num is odd END IF END Poriyaalan Lecturer Notes To check greatest of three numbers Step1: Start Step2: Get A, B, C Step3: if(A>B) goto Step4 else goto step5 Step4: If(A>C) print A else print C Step5: If(B>C) print B else print C Step6: Stop BEGIN READ a, b, c IF (a>b) THEN IF(a>c) THEN DISPLAY a is greater ELSE DISPLAY c is greater END IF Poriyaalan Lecturer Notes ELSE IF(b>c) THEN DISPLAY b is greater ELSE DISPLAY c is greater END IF END IF END Poriyaalan Lecturer Notes Write an algorithm to check whether given number is +ve, -ve or zero. Step 1: Start Step 2: Get n value. Step 3: if (n ==0) print “Given number is Zero” Else goto step4 Step 4: if (n > 0) then Print “Given number is +ve” Step 5: else Print “Given number is -ve” Step 6: Stop BEGIN GET n IF(n==0) THEN Poriyaalan Lecturer Notes DISPLAY “ n is zero” ELSE IF(n>0) THEN DISPLAY “n is positive” ELSE DISPLAY “n is positive” END IF END IF END Write an algorithm to print all natural numbers up to n Step 1: Start Step 2: get n value. Step 3: initialize i=1 Step 4: if (i