How to Write an Algorithm in Programming Language

Table of contents:

How to Write an Algorithm in Programming Language
How to Write an Algorithm in Programming Language
Anonim

An algorithm is a series of steps created to solve a problem or perform a task. Usually, before a program is written, algorithms are written in pseudocode or in a combination of spoken language and one or more programming languages. This wikiHow article teaches you how to piece together the pieces of an algorithm to start your application.

Steps

Write an Algorithm in Programming Language Step 1
Write an Algorithm in Programming Language Step 1

Step 1. Define the result of your code

What is the specific problem you want to solve or the task you intend to perform? Once you have a clear idea of what you intend to achieve, you can determine the steps that will allow you to reach the goal.

Write an Algorithm in Programming Language Step 2
Write an Algorithm in Programming Language Step 2

Step 2. Establish a starting point

Finding the starting point and the ending point is essential to list the steps of the procedure. To establish your starting point, find answers to the following questions:

  • What data or elements are available?
  • Where is the data located?
  • What are the formulas that can be applied to the problem in question?
  • What are the rules for working with available data?
  • How are the data values related to each other?
Write an Algorithm in Programming Language Step 3
Write an Algorithm in Programming Language Step 3

Step 3. Find the end point of the algorithm

Like the starting point, you can find the ending point of your algorithm by focusing on the following questions:

  • What concrete data will we learn from the procedure?
  • What changes from start to finish?
  • What needs to be added or what is no longer available?
Write an Algorithm in Programming Language Step 4
Write an Algorithm in Programming Language Step 4

Step 4. List the steps from start to finish

Start with more general steps. To use a concrete example, suppose your goal is to eat lasagna for dinner: your starting point is to find a recipe, while the end result is to have a lasagna cooked and ready to eat by 7pm; the steps might be similar to the following:

  • Search for a recipe on the Internet.
  • Check the ingredients already available in the kitchen.
  • Draw up a list of ingredients to buy.
  • Purchase the missing ingredients.
  • Go back home.
  • Prepare the lasagna.
  • Remove the lasagna from the oven.
Write an Algorithm in Programming Language Step 5
Write an Algorithm in Programming Language Step 5

Step 5. Determine how to complete each step

Once you've got a pattern for subsequent actions, it's time to think about how to code each step. What language will you use? What resources are available? What is the most efficient way to complete each step in that language? Incorporate some of this code into your algorithm, then expand each step until you have detailed the entire process.

  • For example, the first step in the lasagna preparation algorithm example is: Look for a recipe online; what does this research imply? Be specific. For instance:

    • Turn on the computer.

      Connect to the Internet or make sure you are already connected

    • Open a web browser.
    • Enter your search terms.
    • Click on a recipe link.
    • Determine if the recipe meets your needs.

      • Exclude non-vegetarian recipes.
      • Make sure the recipe is for at least 5 servings.
    • Repeat some of the above steps until you find the right recipe.
  • Consider the resources at your disposal, such as the capabilities of the system for which you are developing a program. In the case of lasagna, we would assume that the person making it knows how to search the internet, use an oven, and so on.
Write an Algorithm in Programming Language Step 6
Write an Algorithm in Programming Language Step 6

Step 6. Review the algorithm

Once you have written your algorithm you will have to evaluate the procedure, because the algorithm is created to perform something specific and you need it to start writing the program. Ask yourself the following questions and address each as necessary:

  • Does the algorithm solve the problem / perform the task?
  • Are the input and output data clearly defined?
  • Do we need to redefine the final goal to make it more general or more specific?
  • Is it possible to simplify the steps?
  • Are you sure that the algorithm ends with the correct result?

Advice

  • Consult existing algorithms for ideas on how to write your own.
  • Use quick calculation iterations.
  • Focus on efficiency while coding.
  • Don't forget to terminate otherwise the code fails.

Recommended: