ALGORITHMS PSEUDOCODE Algorithms An algorithm is a detailed







- Slides: 7

ALGORITHMS & PSEUDOCODE

Algorithms ■ An algorithm is a detailed set of steps to solve a problem ■ It states the actions to be executed and the order in which they are to be completed ■ Algorithms can be described using pseudocode or a flowchart ■ Algorithms are fundamental to computer programming

Developing an algorithm High Level Algorithm • Written in pseudocode or drawn as a flowchart, it describes how the problem will be solved without the fine detail Detailed Pseudocod e • Expands the logic of the high level algorithm to closely match a real program Real Code • Line by line conversion of the pseudocode to real code.

Important Constructs There are 3 important constructs used in algorithms and coding that you need to be familiar with: ■ Sequence – The order that instructions are executed. Order is important eg: ■ Clean teeth ■ Get out of bed ■ Selection – Making a decision based on certain conditions eg: ■ If its raining then I’ll stay indoors Else I’ll go to the park ■ Iteration – Another name for repeating a set of actions ■ While I feel hungry Eat more food

Pseudocode ■ Pseudocode is an artificial language that helps programmers develop algorithms. ■ It is intended to be easy to read and free from the strict formal syntax of a programming language ■ The rules of pseudocode are reasonably straightforward – there should be a line of pseudocode for every line of real code in your program

Example 1 - Student grades Task: A program is required to output ”passed” if a student achieves 60 or more marks otherwise failed High Level Algorithm If student's mark is greater than or equal to 60 Display"passed" else Display "failed" Pseudocode If mark >= 60 Print "passed" else Print "failed" In this example the problem to be solved, high level algorithm and pseudocode are similar because of the very fact that it is a simple program. However, that should not prevent you from completing the process!

Example 2 – Class Average Task: A program is required to input the individual marks and calculate the average score for a class of 10 students High Level Algorithm Initialise variables Loop add 1 to count input mark calculate average Until count > 10 Display average Pseudocode average 0, class-total 0, mark “” For count 1 to 10 mark userinput (”enter student’s marks”) class-total + mark average class-total/count Next count Useroutput (“class average is “, average) Now the value of the high level algorithm becomes clearer. • It shows the “big picture” of how the problem is to be solved • Finer detail is added in the pseudocode ie: ▫ Variables to be used ▫ type of loop (for or while) ▫ Calculation of average