Programing Algorithm Structure

Algorithm Concept
Algorithm is a sequential logical stages to systematically solve a problem. Problem may be anything, but noted that all problems must have an initial condition prior to work on its algorithm. Algorithm concept may be similar to recipe. A recipe usually has a list of needed materials or spices, and sequence and how to build or to cook it. If the material is not available or not calibrated then the recipe may not be built / cook. Likewise if cooking stages is not sequential, a correct result will not be received. Different algorithms may be applied to a problem with same condition. Algorithm complexity level may be measured by the amount of computation to solve a problem. In general, algorithm with short execution time to solve a problem would have low complexity, while algorithm with long execution time may have much higher complexity.

How to write Algorithm
There are three (3) methods to write an algorithm, namely,

 

  • Structured English (SE), SE is a good enough tool to depict an algorithm. SE is basically an English, but we can modify it using Indonesian and call it Structured Indonesian (SI). The algorithm such as in Example 5.10 and 5.11 are written in SE. Since it uses daily language, SE is suitable to be used in communicating algorithm to software users.
  • Pseudocode, Pseudocode resembles SE. Due to its resemblance, SE and Pseudocode are considered to be the same. Pseudo means imitation or resembled, whereas code refer to program code. Thus, pseudocode means code that resembles the instruction of the program code. Pseudocode is based on the the actual programming language, such as, BASIC, FORTRAN or PASCAL. PASCAL  based pseudocode is often used. Sometimes, pseudocode refers as PASCAL-LIKE algorithm.
  • Flowchart, Flowchart is scheme / chart that shows the program flow in logical manner. Flowchart is a tool to show algorithm using certain notations / symbols. The following section will discuss it in a more detail. There are several important symbols used to show an algorithm as shown in Figure 1.

image This notation is known as Terminator to show the beginning and the end of an algorithm

 

image This notation is known as Data to represent the data input or output or to represent data entry operation or printing the result.


image This notation is known as Process to represent a process.

 

 

image This notation is known as Decision to represent condition selection in a program


image This notation is known as Preparation to set starting value, end value, the increase / decrease value of the counter.

 


image This notation is known as Predefined Process to show the process is done by other subprocess, such as, procedure, sub-procedure, function.

 

image This notation is known as Connector to show continuation of flowchart from pne page to another page.

 

image This notation is known as Arrow to show the data flow from one process to another process.

 

 

Figure 1. Symbols used in flowchart.

imageThere are two (2) types of flowchart, namely, program logic flowchart and detailed computer program flowchart. The program logic flowchart is used to show each step of the computer program in a logical manner and usually is prepared by an system analyst. Whereas the detailed computer program flowchart is used to show the detail instruction- of the computer program and usually prepared by a programmer,it is shown in Figure 2.

 

 

Figure 2. Program flowchart.


Sequential Algorithm.
image There are three (3) foundation in algorithm structure, namely, sequencing, branching and looping. An algorithm will usually uses the three (3) structures to solve a problem. In this section, we will firstly discuss
sequential algorithm structure. The sequential structure is similar to a car run in a straight road and no turn or intersection as shown in Figure 3. The car will pass kilometers of road until it reaches its destination.

 


Figure 3. Car travels in straight road.

The sequential structure consists of one or more instruction. Each instruction is sequentially executed based on its sequence in source code, and executed after one instruction is completed. Instruction sequence sets the final stage of the algorithm. If the sequence is changed, the final result may be changed. According to Goldshlager and Lister (1988) the sequential structure follows the following rules:

  • Each instruction is executed one by one.
  • Each instruction is carried out very precise, none is repeated.
  • Instruction sequence in the process is the same is action sequence in the algorithm.
  • At the end of the final instruction is the end of the algorithm.

Branching Algorithm
image A program will not always follow a sequential structure, sometimes we must change program sequence and jump to certain line in the program. This is known as branching or decision process. This occurs when the car reaches an intersection as shown in Figure rigth. The driver must decide to turn left or right.

In the branching structure, a program will branched as the required condition is met. In such process, decision symbol in the flowchart must be used. Decision symbol is meant to test a condition. The result will determine which branch is used.

Looping Algorithm
image In many cases, we encounters job that must be repeatedly done. One of the simplest example is a car racing as shown in Figure  In car racing, participant must follow the circuit tracks as set by the race rule. Whoever the fastest in reaching the finish line, he wins.

In creating a computer program. We sometimes must repeat one or a group of instruction to archive the required result. In computer, a repetition process is easy to carried out. This is due to the advantage of computer as compared to human to do task or repeated instruction without tired, bored, or lazy. Compared to the race car driver, at one time he must be feeling tired driving his race car in circles.

The looping algorithm consists of two parts, namely,

  1. Repetition condition, the requirement to be met to carry out the repetition. This condition is usually stated in Boolean expression that must be tested whether being true or false.
  2. The repetition body (the loop body), that is one or more instructions that will be repeated.

The repetition structure is usually started by an initialization section and termination section. Initialization is the instructions that are carried out prior to repetition. Initialization is usually used to provide initial value to the variables. Whereas termination instruction is carried out after the completion of the repetition.

There are several forms of the repetition, each with its own requirement and characteristics. Several forms may be used for the same case, but some of the form may suitable for a certain case only. The selection of the form may influence the correctness of the algorithm. The correct repetition form depends on the problem.

image Repetition using For is the oldest form of repetition in programming language. Almost all programming languages provide this method, despite may be different in syntax. In For structure, we need to know how many times the the loop bodies will be looped. In this structure we normally use a variable, namely, loop counter, to take note the number of loop has been done. The value of loop counter may be increasing or decreasing during repetition process. General flowchart using For structure is shown in Figure 4. Please note the usage of symbol preparation in the flowchart.

Figure 4. Repetition algorithm using For.

In execution of repetition using for, the following steps must be followed:

  1. Initialized counter value.
  2. Checked whether counter is larger than its maximum / final value. If true then exit the repetition process. If the counter is decreasing, check whether the counter is lower than its minimal / final value. If true then exit the repetition process.
  3. Execute the statements / instructions in the loop body.
  4. Increase / decrease the counter in accordance with the increment value. If no increment value, use 1 as default.
  5. Repeat step no 2.

It is import to initialized counter at the beginning of the loop. If we try to change final value in the loop body, it would give not much impact into how many repetition to be carried out.

loading...