An Algorithm consists of the steps needed to accomplish a task in the order that they need to be processed.
Let's look at a simple algorithm for a program to calculate gas mileage. It still follows the pattern of Get a clear picture of the program's purpose. Determine the inputs and the outputs and the process needed to get from one to the other.
The inputs will be the beginning mileage, the end mileage and the number of gallons
The output will be miles per gallon.
The algoritm looks like this:
1) Get beginning mileage
2) Get ending mileage
3) Get gallons needed to refill the tank (gallons used)
4) Subtract beginning miles from ending miles to get miles traveled
5) Divide miles traveled by gallons used to get Miles per gallon
6) Output miles per gallon
You can also use a diagram to represent the algoritm if it makes it clearer. (These can be expecially useful as the algorithms grow more complex.)
Here is a UML diagram called an Activity diagram that shows the same process.
No comments:
Post a Comment