Characteristics of an algorithm:
unambigious: clear and concise, clear inputs and outputs, all steps of the procedure explicit
finite: algorithm must have a finite number of steps that end once they have been completed. the algorithm must stop eventually with either the expected output or a response that indicates there is no solution
well defined: each step should be well defined, specific and in order, details of each step must be explicit, include how to handle errors
inputs: input is data, which will be transformed by the procedure. algorithm may have 0 or more inputs
outputs: data that has been transformed by the process, should match the desired output, algorithm must have one or more well defined outputs
feasible: in order to be effective, procedure must be possible with the available resources and not contain any redundant unnecessary steps
independent: must have step by step instructions and be independent of any programming language
Components of an algorithm:
instructions: an algorithm consists a series of sub algorithms, each performing a small activity. each set of steps for a small activity is called an instruction. for example: digit addition
variables: which is to temporarily store values while the steps of the algorithm are being executed. as the algorithm is being processed line by line, variable will change value, hence its name. for example: an algorithm used to calculate profit will have the variable named ‘profit’ to store this data
conditionals: one of the steps in an algorithm could be to make a decision or choice. for example: when an algorithm is required to determine whether a profit has been made. this could be written as :
if sales > costs, then print ‘we are profitable’
loops: allows a set of instructions to repeat when a certain condition is met. for example, an algorithm may repeat until there are no more customers
Ways of representing algorithms:
natural language: everyday language allows developers to work with non-coders to write down the steps that the algorithm needs to follow
everyone involved can understand the process (+)
it has tendency to be ambigious and lack clarity (-)
eg: cooking recipe, to make a cake, you'd follow detailed steps of the recipe
flowchart: use a set of standard symbols to represent different components, and arrows are used to show thee direction of the steps
helps programmers visualize the steps of the algorithm and force them to think about the sequence and selection => good planning tool
eg: each rectangle represents an action, while diamonds represents a condition or a loop
programming languages: