Last updated: Tuesday 18th May 2010, 17:47 PT, AD
An algorithm is a sequence of precise instructions (written in English) that leads to the solution to a problem. An algorithm can be compared to a recipe to bake a cake, a set of directions to enroll for a class or a set of instructions to change a light bulb... Example Algorithm: Problem: Find the average of three of numbers 1. get the first number 2. get the second number 3. get the third number 4. add the three numbers together to calculate the total 5. divide the total by 3 to calculate the average The set of five instructions above is known as an algorithm. The algorithm above solves the problem of finding the average of three numbers. All the instructions necessary to solve the problem are there, and they are in the correct order. To make the algorithm easier to understand, we can include example values. See below for an example... Example Algorithm: Problem: Find the average of three of numbers 1. get the first number (e.g. 32) 2. get the second number (e.g. 27) 3. get the third number (e.g. 100) 4. add the three numbers together to calculate the total (e.g. 159) 5. divide the total by 3 to calculate the average (e.g. 53) The study of algorithms is a very important part of computer/computing science. An algorithm is simply a set of precise instructions to solve a problem. Algorithms are used when designing a computer program. When developing a computer program there is always a problem solving and design stage which results in the algorithm. Problems which can be solved using a computer are always planned in advance of actually typing the program in. The coding part of programming is the part that involves typing the program in using your chosen programming language for example Python, Java, C++, PHP, VB etc. Programs are typed in using the algorithm for guidance. The coding phase of programming results in a program that can be run and tested. The implementation phase of programming includes running and testing the program developed from the algorithm. FYI - Most beginner programmers skip the planning and testing parts of programming. Experienced programmers never skip these phases. Your program has more chance of being correct if it is well planned.
The following is a computer program written in the Python 3 programming language, which solves the problem of how to find the average of three numbers. The three numbers are typed in at the keyboard by the user of the program:
number1 = float(input("Enter the first number: ")) number2 = float(input("Enter the second number: ")) number3 = float(input("Enter the third number: ")) total = number1 + number2 + number3 average = total / 3 print ("The average is " + str(average))This is what the program looks like when it runs:
Enter the first number:
Enter the second number:
Enter the third number:
The average is 53.0User input is shown in black font, program output is shown in blue font. More Python resources