The Problem-Solving Process Input Processing Output
Program A program is the set of instructions that instruct the computer about the way the task is to be performed. Instructions in a Program have three parts Accept Input Data that needs to be processed Act upon Input Data and process it Provide Output to user Instructions are defined in a specific sequence Chapter-8: Computer Programming Fundamentals 4
Program Planning 1. Analyze 2. Design 3. Code, Test and Debug 4. Complete the Documentation
Program Development Lifecycle Chapter-8: Computer Programming Fundamentals 6
Algorithm Ordered sequence of finite, well defined, unambiguous instructions for completing a task. English-like representation of logic to solve problem Step-by-step procedure for solving problem For a particular task, different algorithms can be written Select an algorithm based on advantages and disadvantages Different Algorithms would typically lead to trade off between memory requirements and execution speed Chapter-8: Computer Programming Fundamentals 7
Algorithm: An Example Chapter-8: Computer Programming Fundamentals 8
Control Structures Specifies statements to be executed and order of execution of statements Execution of a statement based on a decision Repetitively execute statements unless condition met Used by Flowchart, Pseudo code Three kinds Sequential: Instructions executed in linear order Selection (branch or conditional): Asks true/false question and selects next instruction based on answer Iterative (loop): Repeats execution of block of instructions Chapter-8: Computer Programming Fundamentals 9
Flowchart Diagrammatic representation of logic for solving task Drawn using boxes of different shapes with lines connecting them to show the flow of control Make logic of program clearer in a visual form Diagrammatic representation forms a common medium of communication Drawn using different kinds of symbols. Chapter-8: Computer Programming Fundamentals 10
Elements of Flowcharts Symbol Name Flowline Terminal Input/Output Processing Decision
Continued… Symbol Name Connector Off page Connector Predefined process Annotation
Preparing a Flowchart Common symbols that are used to draw a flowchart Process, Decision, Data, Terminator, Connector and Flow lines Rules – Should have Start and End – Direction of flow must be from top to bottom and left to right – Relevant symbols must be used while drawing a flowchart – Sequence, selection, iterative structures may be used wherever required Chapter-8: Computer Programming Fundamentals 14
Control Structures in Flowchart Chapter-8: Computer Programming Fundamentals 15
Preparing a Flowchart (contd..) Sequence: Steps executed in linear order Selection: Step to be executed next is based on decision taken. Different path followed for True and False Iterative: Condition is checked. Either next step in sequence is executed or control goes back to one of the already executed steps to make a loop Limitations May run into multiple pages, difficult to understand Updating with changing requirements Chapter-8: Computer Programming Fundamentals 16
Examples of Flowchart Can you draw a flowchart to read 100 numbers and print the largest number? Product of Maximum of Sum of firsttwo numbers three numbers 100 integers Chapter-8: Computer Programming Fundamentals 17
start Read data Declare a variable “greatest” Assign the first value in the list to “greatest” Compare the number in “greatest” with next no. in the list Is next no. YES greater than “greatest” ? NO Assign next no. to “greatest”YES Are there more numbers in the list?
Solving the Break- in - ClassProblemIn order to make the classes Students Situation Break more productive and easy who want Duration on the nerves the Admin at a university wants to break formulate the policy on Less than Trivial No break giving breaks in the classes. equal to 5% They have come up with an idea of giving breaks only when they are required by 5 < x ≤ 15 % Fair 3 Min the class students and their Break duration is also to be determined by how much 15 < x ≤ 25% Modest 5 Min severely it is demanded. Break You are required to develop a flow chart to demonstrate 25 < x ≤ 50% Dire 10 Min how the policy is to work. They’ve provided you with or greater Break the following data:
startShow ofHands for Break 0 <x<5 No break 5 < x <1 5 3 mins15 < x < 25 5 mins25 < x <50 10 minsEND
Pseudo code Consists of short, readable and formally-styled English language used for explaining an algorithm. Does not include details like variable declarations, subroutines etc. Short-hand way of describing computer program Not based on any programming language Uses structured constructs of programming language but is not machine readable Cannot be compiled or executed No standard for syntax of pseudo code exists Easily translated into a programming language Chapter-8: Computer Programming Fundamentals 21
Preparing Pseudo code Written using structured English Commonly used terms to represent actions Inputting data: INPUT, GET, READ Outputting data: OUTPUT, PRINT, DISPLAY Calculations: COMPUTE, CALCULATE Incrementing: INCREMENT Control structures – Sequence structure: sequence of steps executed in linear order – Selection constructs: if-statement, case statement – Iterative statements: WHILE, DO-WHILE Chapter-8: Computer Programming Fundamentals 22
Control structures for Pseudocode IF (condition) THEN WHILE (condition) Statement(s) 1 Statement 1 ELSE Statement 2 Statement(s) 2 Step 1 : ENDIF : Step 2 END IF (condition) THEN Statement(s) 1 Step 3 DO ENDIF Statement 1 : Statement 2 CASE expression of : : Condition-1 : statement1 : : Condition-2 : statement2 WHILE (condition) : Condition-N : statement N OTHERS: default statement(s)Sequence Selection Iteration Chapter-8: Computer Programming Fundamentals 23
Algorithm, Flowchart, Pseudocode Algorithm: A sequence of instructions used to solve a particular problem Flowchart and Pseudo code: Tools to document and represent algorithm Flowchart : Graphical representation of algorithm Pseudo code: Readable, formally styled English like language representation of algorithm No knowledge of programming language required to write or understand flowchart or pseudo code Chapter-8: Computer Programming Fundamentals 24
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.