How to Implement an Algorithm to Your Computer
Algorithms are very useful in carrying out complicated or repetitive steps to accomplish a task. A database may have millions of people's names, phone numbers and other data. A search algorithm takes a name entered by the userm the input, and reports back any matches in a database. Algorithms that are too complicated to program and analyze in one step need especially close attention, as logical links and loops compound the possibility of human error.
Instructions
-
-
1
Identify inputs and outputs. Inputs can be numbers, words, sensor data and anything that "initiates" a program. Outputs are the desired results: what the programmer intends the algorithm to do. A finite series of steps relate inputs and outputs. These steps can be simple or complicated, depending on the algorithm's purpose.
-
2
Draft the core pseudo-code. Pseudo-code is semi-formal notation "halfway" between formal programming language and "natural" language. Pseudo-code contains the essential inputs, steps, and output. In this case, pseudo-code would contain a prompt for a number, the multiplying operation that gives the number's cube, and an "output command" that displays the resulting cube. Example of pseudocode for the algorithm might be:
Define variable "number";
Input(number);
NewNumber = number^3 ;
//number^3 = number*number*number//
Output(NewNumber);
-
-
3
Make notes of unclear or specialized terms and steps. In this example, the "//number^3 = number*number*number//" is for a programmer's benefit. It has no effect on the code, but is simply a reminder to the current, or any future programmers, what "number^3" means. These notes are very helpful in communicating complex information to people who may be new or inexperienced with the algorithm.
-
4
Convert the pseudo-code into code in a formal language within a computer. This requires a compiler in a programming language. Most programming languages such as C++ differ in notation and formatting details, but are otherwise similar. With quality pseudo-code, the translation to formal language should not be difficult. Common programs such as Microsoft Excel, Matlab or Mathematica can be platforms for formal-language algorithm writing. In C++, the computer algorithm would be similar to:
#include<stdio.h>
int main()
{
float Number;
Number = -3.815;
float NewNumber;
printf("Input number: %f\n", Number);
NewNumber = Number*Number*Number; //Number^3 = Number*Number*Number
printf("\nThe number cubed is:\n");
printf("%f", NewNumber);
}
-
5
Test the programmed algorithm. Verify calculations and results by hand. In this example, this is easy. A calculator shows that (-3.815)^3 = -55.524.
-
6
Build in checks and fail-safes to make the algorithm robust. The given example sets up a number, -3.815, and cubes it. In a realistic algorithm, there would be safeguards against nonsensical inputs. Failure to do so could crash a program or give nonsensical answers. Algorithms that ignore the "what ifs" of human error and programming intricacies are prone to errors, and typically require revision.
-
1
References
- Photo Credit computer image by Orlando Florin Rosu from Fotolia.com