Y Y C m Y...C C...LLLeeem maaarrrddd COMPUTER SCIENCE WAYS OF REPRESENTING ALGORITHMS Algorithms can be represented in a variety of ways. We will examine 3 of them here : 1. narrative 2. pseudocode 3. flowcharts Narrative This is the use of natural language and natural language rules to construct sentences and paragraphs which describe the actions to take. The advantages of a narrative are that it can be applied outside of computing, It can be understood by most persons and It requires no special training to construct The disadvantages of a narrative are that it can be too verbose (too wordy) It can be context-sensitive (interpretation varies depending on person’s background and experience) Pseudocode A type of structured English that is used to specify an algorithm. The advantages of pseudocode are that it is independent of a particular language so programmers of different languages can all understand it programmers do not have to worry about syntax Lay people can help to determine the validity of the logic because it is not that difficult to understand The disadvantages of pseudocode are that It is very detailed and so requires a high level of concentration to determine the overall logic The loose standards means that one solution may be depicted in several different ways by different persons Note the following about writing pseudocode : • • • • You should give your pseudocode a name and a brief description to indicate what it does You should list all the variables used and what data type they are. You must always have start and stop. Use indentation to help depict the logic 533571824 1 Y Y C m Y...C C...LLLeeem maaarrrddd • The control structures are : • Sequencing • Input statements - use terms such as input, get, read • Output statements – use terms such as display, print • Assignment statements • Storing a value in a variable e.g. limit 12 • Performing a calculation e.g. sum sum + num1 • Selection • IF –THEN ENDIF • IF – THEN – ELSE ENDIF • Iteration • FOR loops • WHILE loops Program Flowcharts A diagrammatic representation of the sequence of steps needed to perform a task. They show the sequence of operations, movements, decisions and storage activities within a system. The advantage of flowcharts is that it is easy to understand by persons at all levels because of the visual nature The disadvantages of flowcharts are that They can become large and unwieldy when the programs are long and complex When alterations are required the flowchart will have to be completely redrawn. Reproduction: As the flowchart symbols cannot be typed, production of flowcharts becomes a complex exercise. Note the following about drawing flowcharts : o o o o o o o o o 2 Flowcharts should have a title/label All flowcharts must have one start and one stop point Flow lines should run from top to bottom or left to right Flow lines should not cross Start symbols have only one flow line going out Stop symbols have one flow line going in Decision symbols should have one flow line going in and two coming out Most other symbols have one line going in and one coming out Symbols must have the operation written in them 533571824