1 / 48

IPC144 Session 6 Problem Analysis and Program Design Continued

IPC144 Session 6 Problem Analysis and Program Design Continued. 1. Objectives To write pseudo code To convert Flowcharts into Pseudo code To perform walkthroughs on pseudo code To represent data files in flowcharts and pseudo code To represent arrays in flowcharts and pseudo code. 2.

tobit
Download Presentation

IPC144 Session 6 Problem Analysis and Program Design Continued

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IPC144 Session 6 Problem Analysis and Program Design Continued 1

  2. Objectives • To write pseudo code • To convert Flowcharts into Pseudo code • To perform walkthroughs on pseudo code • To represent data files in flowcharts and pseudo code • To represent arrays in flowcharts and pseudo code 2

  3. Pseudo code Pseudo code Pseudo code (pseudo = fake, code = computer language), a fake computer language (3GL)‏ Throughout these discussions we have been using the PDC to move from a high level, general analysis of a problem to a more specific solution The IPO Chart gave us a brief description of the inputs, the outputs and a list of processes for transforming the inputs into the outputs The Flowchart gave us, diagrammatically, the sequence or order in which processes and decisions are made to solve the problem Some people are more comfortable with pseudo code, and may choose to design their algorithm directly in pseudo code. In this course you are expected to be able to read and write both flowcharts and pseudo code. 3

  4. Pseudo code, continued Pseudo code, continued Pseudo code is a hybrid between the structured computer languages (C, Pascal, BASIC...) and English The control structures we defined in Flowcharting are represented here Because pseudo code is not a real computer language and it has loose rules for implementation, it can be used to develop an algorithm for any structured computer language From the pseudo code (after testing the algorithm) the program can be coded by translating the statements in pseudo code to statements in the programming language (C in our case). 4

  5. Pseudo code, continued • Pseudo code, continued • There is no real standard for pseudo code, however it must have the following characteristics: • Statements are written in simple English • Each instruction is written on a separate line • Keywords are used to consistently name the parts of control structures • Indentation is used to make parts of a control structure conspicuous • Each set of instructions is written from top to bottom, with only one entry point and one exit point 5

  6. Pseudo code, continued • Pseudo code, continued • Again the three basic structures are represented: • simple sequence • simple decision • iteration 6

  7. Pseudo code, continued Simple sequence - Assigning Variables This is where variables are manipulated within your algorithm The New Webster's Computer Terms dictionary definition: A variable is a symbolic name representing a value that changes during the program's execution Back to Secondary School algebra, remember these? 5 = 5x + 4y 0 = 5(x + y) - 3x + 5 Solve for x and y. x and y are the variables. 7

  8. Pseudo code, continued Simple sequence - Assigning Variables, continued When creating a name for a variable, make it meaningful Don't make it too long (you will end up with writer's ramp now and carpal tunnel syndrome later)‏ custName may be not as meaningful as customers_first_and_last_name but it is easier to write and probably conveys enough information about how the variable is being used. The other extreme cn is obviously meaningless. Use your judgement. Ask yourself: If I had to re-read this in another year, would the variable name be meaningful? 8

  9. Pseudo code, continued Simple sequence - Assigning Variables, continued There are two types of variable assignments: When you are initializing a variable As a result of some processing It will be considered BAD FORM not to initialize a variable before using it Verbs used: INITIALIZE SET INITIALIZE counter to zero SET recordSize to 500 9

  10. Pseudo code, continued Simple sequence - Assigning Variables, continued As a result of processing: Expect to use the symbol "=" totalPrice = basePrice + salesTax You can use this form for initializing variables as well counterA = 0 counterB = 0 recordSize = 500 The value on the right side of the “=“ is calculated (if necessary) and then placed into the variable that is on the left side of the “=“. You can have only one variable on the left of the “=“. 10

  11. Pseudo code, continued counterA = 0 SET recordSize to 500 Simple sequence - Assigning Variables, continued Pseudo codeFlowchart counterA = 0 SET recordSize to 500 11

  12. Pseudo code, continued Simple sequence - Input of Data Verbs used: READ GET READ usually refers to data being read from a file GET usually refers to data being entered by the user at a keyboard READ address FROM customerFile GET newAddress 12

  13. Pseudo code, continued Read address FROM customerFile Get newAddress Simple sequence - Input of Data, continued FlowchartPseudo code READ address FROM customerFile GET newAddress 13

  14. Pseudo code, continued Simple sequence - Output of Data Verbs used: PRINT WRITE OUTPUT DISPLAY PRINT usually refers to data being sent to a printer WRITE usually refers to data being sent to a file OUTPUT and DISPLAY usually refer to data being sent to a monitor. In pseudo code The output is not necessarily formatted (made to look pretty), so it is common to simply list each piece of information separated by commas. E.g. DISPLAY count, average 14

  15. Pseudo code, continued Simple sequence - Output of Data, continued Example: PRINT "Program Completed" WRITE newAddress TO customerFile OUTPUT name, address, postalCode DISPLAY "End of Data Reached" Note that any phrase that is to be output EXACTLY as written is enclosed in quotes This helps to differentiate between a literal string and a variable name. E.g. average = sum / count DISPLAY The average is average over a range of count values Should be written as: DISPLAY “The average is”, average, “over a range of ” , count, “values” 15

  16. Pseudo code, continued Write address TO customerFile Display newAddress Simple sequence - Output of Data, continued Flowchart Pseudo code WRITE address TO customerFile DISPLAY newAddress 16

  17. Pseudo code, continued Simple sequence - Computation Some verbs used: ADD COMPUTE CALCULATE MULTIPLY DIVIDE SUBTRACT ADD 1 TO counter COMPUTE tax = price x 0.15 degreesC = (degreesF - 32) x (5/9)‏ 17

  18. Pseudo code, continued Add 1 to counter degreesC = (degreesF - 32)‏ x (5/9)‏ Simple sequence - Computation, continued FlowchartPseudo code ADD 1 TO counter degreesC = (degreesF - 32) × (5/9)‏ 18

  19. Pseudo code, continued Simple sequence - Module Call Verbs Used: CALL CALL myModule CALL yourModule(myvar)‏ CALL theirModule(yourVar, myVar, someVar)‏ 19

  20. Pseudo code, continued dspMenu func(a, b, c)‏ Simple sequence - Module Call Flowchart Pseudo code CALL dspMenu CALL func(a, b, c)‏ 20

  21. Pseudo code, continued • Selection - Simple Decision • The basic form looks like: • IF condition THEN • statement(s)‏ • ELSE • statement(s)‏ • ENDIF • If the condition is TRUE, then the statements immediately following the THEN keyword are executed, until the ELSE clause. At which point the program resumes after the ENDIF keyword • If the condition is FALSE, then the statements immediately following the ELSE keyword are executed • Note the indentation that is being used • Makes it easier to see the components of the selection • Provides for readability 21

  22. Pseudo code, continued c = e + f a > b c = t x q Selection - Simple Decision, continued FlowchartPseudo code IF a > b THEN c = e + f ELSE c = t x q END IF TRUE FALSE 22

  23. Pseudo code, continued StatementD StatementA If condition StatementE StatementB StatementC Selection - Simple Decision, continued FlowchartPseudo code IF condition THEN StatementA StatementB StatementC ELSE StatementD StatementE END IF FALSE TRUE 23

  24. Pseudo code, continued Selection - Simple Decision, continued An example of no indentation: with indentation: IF a = b THEN IF a = b THEN DISPLAY "A equals B" DISPLAY "A equals B" c = (23 - d) / 2 c = (23 - d) / 2 lifeUniverseEverything = 42 lifeUniverseEverything = 42 ELSE ELSE DISPLAY "A does not equal B" DISPLAY "A does not equal B" vogons = 1 vogons = 1 earth = 0 earth = 0 ENDIF ENDIF 24

  25. Pseudo code, continued DISPLAY "Low Average" avg < 50 Selection - Simple Decision, continued What if you don't need an ELSE clause? IF avg < 50 THEN DISPLAY "Low average” END IF TRUE FALSE 25

  26. Pseudo code, continued Selection - Multiway Decision The basic form looks like: SWITCH (variable)‏ CASE (option)‏ statement(s)‏ CASE (option)‏ statement(s)‏ . . . CASE (option)‏ statement(s)‏ DEFAULT statement(s)‏ ENDSWITCH variable is what is being tested option is the possible value that variable takes on, and represents the new path of logic. 26

  27. Pseudo code, continued usrSel DISPLAY "Invalid option" res = a+ b res = a- b res = a* b res = a/ b Selection - Multiway Decision, continued FlowchartPseudo code SWITCH (usrSel)‏ CASE (A)‏ res = a + b CASE (S)‏ res = a - b CASE (M)‏ res = a * b CASE (D)‏ res = a / b DEFAULT DISPLAY "Invalid option” ENDSWITCH Default A M D S 27

  28. Pseudo code, continued Iteration - Pre-Test Loop The basic form looks like: DOWHILE condition statement(s)‏ ENDWHILE 28

  29. Pseudo code, continued i < 10 b = b x c i = i + 1 Iteration - Pre-Test Loop, continued FlowchartPseudo code DOWHILE i < 10 b = b x c i = i + 1 ENDWHILE False True 29

  30. Pseudo code, continued Iteration - Post-Test Loop The basic form looks like: DO statement(s)‏ WHILE condition 30

  31. Pseudo code, continued b = b x c i = i + 1 i < 10 Iteration - Post-Test Loop, continued FlowchartPseudo code DO b = b x c i = i + 1 WHILE i < 10 True False 31

  32. Pseudo code, continued Converting Flowcharts to Pseudo code Use the indentation that has been depicted for each of the structures. The statements that follow IF, CASE, DO and DOWHILE are indented an additional level. ELSE is at the same level as it's corresponding IF. The statements after the ELSE are indented to the same level as the statements in the IF side ENDIF, ENDCASE, ENDWHILE and WHILE end the indentation for their structure, and a re aligned with their corresponding IF, CASE, DOWHILE and DO 32

  33. Pseudo code, continued A Start B resp ≠ “C” hi = 101 C hi = guess resp = “L” guess = (hi + lo) / 2 lo = 1 lo = guess resp = “H” DISPLAY "I Guess", guess, "is it Hi, Lo or Correct?" resp = “N” GET resp DISPLAY "Choose a number" DISPLAY "I found your number", guess B C A Stop Converting Flowcharts to Pseudo code Convert the following flowchart into pseudo code: FALSE TRUE TRUE FALSE TRUE FALSE 33

  34. Pseudo code, continued Converting Flowcharts to Pseudo code, continued hi = 101 lo = 1 resp = “N” DISPLAY "Choose a number" DOWHILE resp ≠ “C” guess = (hi + lo) / 2 DISPLAY "I guess", guess, "is it Hi, Lo or Correct?" GET resp IF resp = “L” THEN hi = guess ELSE IF resp = “H” THEN lo = guess ENDIF ENDIF ENDWHILE DISPLAY "I found your number: ", guess 34

  35. Pseudo code Walkthroughs What is the output of num = 1 var = 0 DO var = num + var DISPLAY var num = num + 2 WHILE NUM <= 7 Note: <= means “less than or equal) 35

  36. Pseudo code Walkthroughs What is the output of num = 1 var = 0 DO var = num + var DISPLAY var num = num + 2 WHILE num <= 7 1 4 9 16 36

  37. Pseudo code Walkthroughs What is the output of i = 5 DOWHILE i > 0 j = i f = 1 DOWHILE j > 1 f = f x j j = j – 1 ENDWHILE DISPLAY f i = i – 1 ENDWHILE DISPLAY “Done” 37

  38. Pseudo code Walkthroughs What is the output of i = 5 DOWHILE i > 0 j = i f = 1 DOWHILE j > 1 f = f x j j = j – 1 ENDWHILE DISPLAY f i = i – 1 ENDWHILE DISPLAY “Done” 120 24 6 2 1 Done 38

  39. Data Files • Data Files • From Session1 we talked about Secondary Storage. • Secondary storage stores files in a hierarchy of directories. • There are two general types of data files: • text files • binary files • Binary files are not discussed in this course. • Text files contain data that is generally readable by people (the format might be difficult to understand). 39

  40. Data Files, continued Data Files, continued The contents and format of a data file will be specified by the program requirements. A file contains records Records contain fields Field contain characters The end of the data file is represented by an 'End Of File' marker, typically referred to as EOF. The end of a record is represented by a 'Carriage Return' character, typically referred to as CR or EOR. Fields are either fixed size (the same size for the same field for all records) or they are delimited (a special character that will not be found in the input data is chosen for this purpose and separates each field within the record. 40

  41. Data Files, continued Data Files, continued Regardless of whether the data file has fixed size fields or delimited fields, all of the fields come in the same order. For simplicity, this course assumes fixed size fields. 41

  42. Data Files, continued • Data Files, continued • Typical operations that are performed on a Data File: • Open - tell the O/S that you wish to perform some actions on the data in the file • Read - read some data from the data file • Rewind - return to beginning of file • Write - write some data to the data file • Close - tell the O/S that you are finished with the data file • Open, Close and Rewind are represented as processes • Read and Write use the traditional Input/Output box. The fields that are read or written are in the same order in the I/O Box as in the data file. • A file pointer keeps track of where you are in the file. 42

  43. Data Files, continued open file.dat READ a, b, c FROM file.dat DISPLAY "Read", a, b, c a = b + c REWIND file.dat WRITE a,b ,c TO file.dat close file.dat Data Files, continued FlowchartPseudo code OPEN file.dat READ a, b, c FROM file.dat DISPLAY "Read", a, b, c a = b + c REWIND file.dat WRITE a, b, c TO file.dat CLOSE file.dat 43

  44. Data Files, continued not EOF file.dat EOF file.dat Data Files, continued While reading from a data file, how do you know you have reached the End-of-File? In your condition, refer to a test for EOF. For example, FALSE TRUE FALSE TRUE 44

  45. Arrays Arrays In programming there are times when we need to work with lists of data such as a list of names or a list of data points from an experiment. These lists can be stored in an Array. The simplest explanation is to consider a single column from a spreadsheet: 45

  46. Arrays Arrays 46

  47. Arrays Arrays Note that the rows are numbered. The same technique is used in computers. The array can have any name, using the same techniques as selecting variable names. Each element of the array is numbered. The element number of the array follows the array name, enclosed in square brackets: myList[0] myList[1] … myList[n] ** In C the first element of the array is number 0: myList[0] 47

  48. Arrays Start i = 0 i < 10 a[i] = 2 * i + 1 i = i + 1 Stop Arrays An example that places a calculated value into the first 10 rows of an array: FALSE TRUE 48

More Related