computer programming - lecture 2

Download Computer Programming - Lecture 2

Post on 19-May-2015




1 download

Embed Size (px)


  • 1. Lecture 2Algorithms andProblem SolvingComputer Programming I1

2. Overview Algorithm Program design Pseudocode Structure diagram Flowcharts Introduction to C++ Testing and debugging Program errorsComputer Programming I2 3. Algorithm Algorithm A sequence of precise instructions whichleads to a solution Program An algorithm expressed in a language thecomputer can understandComputer Programming I 3 4. Program Design Programming is a creative process No complete set of rules for creating a program Program Design Process Problem Solving Phase Result is an algorithm that solves the problem Implementation Phase Result is the algorithm translated into a programming language Computer Programming I4 5. Problem Solving Phase Be certain the task is completely specified What is the input? What information is in the output? How is the output organized? Develop the algorithm before implementation Experience shows this saves time in getting yourprogram to run. Test the algorithm for correctness Computer Programming I5 6. Implementation Phase Translate the algorithm into a programming language Easier as you gain experience with the language Compile the source code Locates errors in using the programming language Run the program on sample data Verify correctness of results Results may require modification of the algorithm andprogram Computer Programming I6 7. Object Oriented Programming Abbreviated OOP Used for many modern programs Program is viewed as interacting objects Each object contains algorithms to describeits behavior Program design phase involves designingobjects and their algorithmsComputer Programming I 7 8. Sample problemsWrite a program calculating the sum of two numbersInputProcessing Output5, 1015 1) Declare variables3) Process 2) Assign values input_1 sum = input_1 + input_2 input_1 = 5 input_2 input_2 = 10 sumThe computer (and so C++)Names for our cellsprovides basic arithmetic8operations. If the operationyou want to use is not provided,you have to compose it. Computer Programming I8 9. Write a program calculating the sum of two numbers There are many models supporting the development of the code. We will see now the same algorithm expressed as: 1)Pseudocode 2)Structure diagram 3)Flowcharts and finally in C++.Computer Programming I9 10. Pseudocode Mixture of C++ and ordinary English Allows us to make our algorithm precisewithout worrying about the details of C++syntaxComputer Programming I10 11. PseudocodeWrite a program calculating the sum of two numbersVersion 1:Version 2:PROGRAM Add Two Numbers PROGRAM Add Two Numbers READ two numbers READ First ADD the numbersREAD Second WRITE the sumSum = First + SecondEND PROGRAM WRITE SumEND PROGRAMComputer Programming I 11 12. Structure Diagram Helpful to break the algorithm into more manageable piecesWrite a program calculating the sum of two numbers Version 1:PROGRAMAdd Two NumbersREADADDWRITETwo Numbers Two Numbers The SumComputer Programming I12 13. Structure DiagramWrite a program calculating the sum of two numbers Version 2:PROGRAMAdd Two NumbersREADADD WRITE Two NumbersTwo NumbersThe Sum READ READSum =Input_1 Input_2Input_1 + Input_2 Computer Programming I 13 14. Rules for Structure Diagram A module which resides above others isreferred to as a Calling module A module which resides below another isreferred to as a Called module A module can be both a calling and calledmodule A called module can only be called by onecalling module Computer Programming I14 15. Flowchart Diagram that shows the logical flow of a program Stress on structured programming Useful for planning each operation a programperforms, and in order in which the operationsare to occur By visualizing the process, a flowchart canquickly help identify bottlenecks or inefficiencieswhere the process can be streamlined orimproved The final visualization can then be easilytranslated into a program Computer Programming I 15 16. Flowcharting symbolsInput/Output (used for all I/O operations)Processing (used for all arithmetic and data transferoperations).Decision (used to test for a condition).Terminal (used to indicate the beginning and end of aprogram or module).Connector (used to indicate the point at which a transfer ofcontrol operation occurs).Predefined (used to indicate the name process of a moduleto be executed).Connecting all the symbols and showing the flowComputer Programming I16 17. Write a program calculating the sum of two numbersSTARTREAD First READ Second Sum = First + Second WRITE Sum ENDComputer Programming I17 18. Flowchart Conventions1) Each symbol denotes a type of operation.2) A note is written inside each symbol to indicate the specific function to be performed.3) The symbols are connected by flow-lines.4) Flowcharts are drawn and read from top to bottom unless a specific condition is met that alters the path.5) A sequence of operations is performed until a terminal symbol designates the sequences end or the end of the program.6) Sometimes several steps or statements are combined in a single processing symbol for ease of reading. Computer Programming I 18 19. Computer Programming I 19 20. startA flowchart to accept two numbers asinput and prints out the maximum Input A Input B False True A>Bprint B print Aend Computer Programming I20 21. Structured Programming Structured Programming is a technique using logical control constructsthat make programs easier to read, debug, and modify if changes arerequired.true false trueSequence Selection RepetitionComputer Programming I 21 22. Different selection structuresIf a > 10 then do S1 If a > 10 then do nothing else do S2 falsetruetruefalse A>10A>10S1 S2 If a > 10 then do S1 else do S2If a 10A