FUNDAMENTALS OF CODING:
CREATING AN ALGORITHM
An algorithm can be defined as a set of mathematical instructions or necessary steps that will help to calculate an answer to a problem. In programming creating algorithms consists of three main headings:
Variable: The names that hold a value, which can stay constant or change depending on conditions or information passed to the program.
Algorithm: The framework in which the necessary steps to reach the solution is set.
Flow Diagram: The schematic way of displaying the steps of the solution in order depending on their functionality and priority.
It is important to acknowledge that algorithms are not programming languages. In coding, an algorithm is a guiding strategic method. The example below demonstrates what an algorithm consists of and its importance.
Q. Let’s imagine we are coding a computer program which calculates whether students have passed or failed a course depending on their exam scores. Let’s assume that an English course student have 3 written exams that are equally weighted and that the passing score is 45%. If you were asked the question whether Robert has passed or failed his course, how would you answer the question?
The first step would be asking Robert what were his exam scores from his written exams and then second step would be taking their average. If the average was less than 45, you would conclude that he has failed. If the average was equal or higher than 45, you would conclude that he has passed. By doing this, you have found a solution method to determine whether Robert has failed or passed his course, which is basically what creating an algorithm is. Creating a methodology to derive a solution to achieve your objective and defining each step that is necessary for your solution is called creating an algorithm.
On the next page, you will find the example of an algorithm for the problem above:
Step 1: Start
Step 2: Enter the student’s name
Step 3: Enter first written exam score
Step 4: Enter second written exam score
Step 5: Enter third written exam score
Step 6: Take the average of three written exam scores
Step 7: If average is below 45;
Step 8: Failed
Step 9: If average is equal or above 45;
Step 10: Passed
Step 11: Stop
Seen above is the algorithm that needs to be created prior to coding the computer program. “Start” and “Stop” are commands to inform the computer when the task should be completed. In the following chapters, we will look into these commands in more depth. One of the most enjoyable parts of coding is creating an algorithm. One can code a computer program to solve the same problem, but in many different ways and with different algorithms. Even though the result is the same, the way the program works will vary depending on how the programmer thinks. This freedom of variety and flow of thought in coding gives programmer the chance to be creative and find alternative solutions.
Q. As an exercise, let’s see the variety that can emerge in creating an algorithm by discussing this exercise with your friends. Let’s think how we can create an algorithm for a computer program that asks the user to enter 6 two-digit numbers and calculates the largest common divider. After the discussion, let’s write the necessary steps for the algorithm.