What is an algorithm and what is a program?
What is an algorithm?
- An algorithm is a recipe for solving a set of problems.
- It is a set of instructions that tells you what to do step by step.
- An algorithm is something conceptual and can be described using language, flowcharts or pseudocode.
- An algorithm can be implemented in different programming languages.
Flowcharts
It is a graphic description of an algorithm using different shapes and arrows that connect them. Convenient for simple algorithms.
When they get bigger they become a "spaghetti diagram" difficult to follow.
Nowadays they are not commonly used.
Pseudocode
The idea is to use an intermediate way of communication between natural language and programming language. It does not have a formal syntax but it usually has the following structure:
- The concept of variable and assignment is used. Eg: "a <- 5" means that the variable a has the constant 5 assigned.
- The input and output data are shown.
- Conditional statements are used: E.g: "if <condition> then <instructions>
- Cycle instructions like "repeat until ..." are used
Algorithm example:
Algorithm to define if a triangle is scalene, isosceles or equilateral Input data: side1, side2, side3 Output data: triangle type if side1 = side2 and side1 = side3 then type <- equilateral else if (side1 = side2) or (side1 = side3) or (side2 = side3) then type <- isosceles else type <- scalene
What is a program?
A program is intrinsically linked to the existence of a computer. Computers run programs. The programs read data from files, databases, networks, keyboards and write data to files, databases, networks, screens, etc..
Programs are executed by the computer processor. For this, they must be loaded onto the main memory RAM.
The loading of the programs onto the memory is carried out by a special type of software or program called operating system.
Differences between Algorithm and Program
Although they may be mixed up because they are both a set of instructions, they represent two very different concepts. An algorithm is more like an idea, a way to solve a problem, while a program is more linked to the execution of one or more tasks by a computer.
A program can implement one or more algorithms, or it may be so simple that we don´t have to use an algorithm.
The task of a developer usually starts by designing algorithms to solve the problems and then implement them and include them in a program.
When we talk about a program there is always the idea that it will be executed by a computer while an algorithm could be executed by a person.
A program is written in machine language or at least in a language that can be compiled or interpreted by some kind of machine (sometimes a virtual machine).