About Algorithms & Flowcharts (AAF)


AAF:   SOLD       




So, what is meant by the programming algorithm? First, just think about how you will start to cook a food recipe, what steps will you take? Well, for those of you who already have experience cooking a particular recipe, then it is not surprising if you do not need a guide anymore, so you know what steps to take from preparing to finishing, Well that's a picture of the algorithm.

For those of you who have no cooking experience, you may be confused about how and what steps to take, so don't worry, look for a complete cooking tutorial starting from the recipe to the steps for preparing to serving (finishing) so that those of you who are still beginners and have no cooking experience, can now get an example of an algorithm from an experienced person through his tutorial.

Every same destination, does not necessarily have the same algorithm. Another picture, for example you and your friend want to go home from work to the same boarding house, first you already know from the social media forum maybe, that the main route to the boarding house is undergoing road repairs and there is severe congestion, so you use an alternative route to get to the boarding house even though it is a bit far but it is faster to get there.

This is different from your friend who doesn't join a social media forum, so he doesn't get information about the traffic jam, so he still chooses the main route to his boarding house, even though it's close but it's slower because he's stuck in traffic.

After we understand a simple description of the algorithm, it is time for us to learn how the programming algorithm works. Below I will share a case study along with an example of its solution.

1. Exchange

1). Create an algorithm with Indonesian structure and flowchart to exchange the input contents of two variables.

Answer:

  1. Enter the value of A into C
  2. Fill in the value A with the value B
  3. Fill in the value B with the value C
  4. Print A, B
  5. Finished


Exchange Flow Chart

2. Determine the Largest Value

1). Create an algorithm with Indonesian structure and flowchart to determine the largest value between two inputs.

Answer:

  1. Input A and B
  2. Is the value of A > B?
  3. If true then print A bigger
  4. Otherwise print B larger.
  5. Finished


Flow Chart Determines the Largest Value of 2 Inputs

2). Create an algorithm with Indonesian structure and flowchart to determine the largest value among three inputs.

Answer:

  1. Enter values ​​A, B, C
  2. If BC then print value A
  3. If not then, is B>C, if true print the value B
  4. Otherwise, print the value C.
  5. Finished


Flow Chart Determines the Largest Value of 3 Inputs

3. Determining Whole Numbers, Even, Odd, & Zero

1). Create an algorithm with Indonesian structure and flowchart to determine integer input including even or odd numbers or zero.

Answer:

  1. Enter an integer
  2. If integer = 0, then print 0
  3. If not, then whether the integer is > 0 and divisible by 2, if true then print num.bul.genap (+)
  4. If not, then is the integer > 0 and not divisible by 2, if true then print odd integer (+)
  5. If not, then is the integer < 0 and divisible by 2, if true then print bil.bul.genap (-)
  6. If not, print odd number (-)
  7. Finished.


Flow Chart Determines Whole Numbers, Even, Odd, Zero

Source:

© STMIK El Rahma Yogyakarta. Compiled by Mr. Eko Riswanto, ST, M.Cs.

Netizens

Q1:

  1. KIKI DINIKI Nov 15, 2015, 19:32:00 = 1. Create a program to determine whether a year is a leap year or not, with the following conditions: - If the year number is divisible by 400, then the year is definitely a leap year. - If the year number is not divisible by 400 but is divisible by 100, then the year is definitely not a leap year. - If the year number is not divisible by 400, not divisible by 100 but is divisible by 4, then the year is a leap year. - If the year number is not divisible by 400, not divisible by 100, and not divisible by 4, then the year is not a leap year 2. Create a program to state the names of the months in a year 3. Write an algorithm and program that asks the user for integer input. If the user enters 0, the program displays 'Sunday'; if the user enters 1, the program displays 'Monday', and so on up to 'Saturday'. If the user enters a value outside the range of 0 to 6, the program writes 'Invalid Day'.
  2. UNKNOWN Sep 5 2016, 19:15:00 = thanks for information
  3. WAYANNIKIARDIYANTI Jun 2, 2017, 11:26:00 = nice info

A1:

  1. Hi Kiki Diniki, Wow.. it seems like your assignment is quite good.. okay, good luck studying.. hope you succeed
  2. you are welcome
  3. Hi wayannikiardiyanti, welcome to gatewan

How to Present Algorithms

Algorithms can be presented with two techniques, namely written techniques and image techniques. Written techniques usually use the structured English and pse udocode methods, while image techniques usually use flow charts.


Algorithm Presentation

Substance:

  1. ENGLISH STRUCTURE AND PSEUDOCODE
  2. ALGORITHMIC TEXT WRITING RULES
  3. FLOW CHART
  4. PROGRAM FLOWCHART
  5. FLOWCHART SYSTEM
  6. GUIDELINES FOR CREATING FLOWCHARTS

1. Structure English & Pseudocode

Structure English is a fairly efficient tool for describing an algorithm. The basis of structure English is English, but Indonesian can also be used, while pseudocode means code that is similar to the actual programming code. Pseudocode comes from the word pseudo which means imitation/similar/resembling and code which means program. Pseudocode is based on real program code such as Pascal, C, C++. Pseudocode is more detailed than structure English, for example in stating the data type used.

Example of Indonesian structure

Baca data jam_kerja
Hitung gaji adalah jam_kerja dikalikan tarif
Tampilkan gaji

Pseudocode with Pascal:

Read jam_kerja
Gaji := jam_kerja * tarif
Write gaji

2. Rules for Writing Algorithmic Texts

The steps to solve the problem in the algorithm text can be written in any notation, as long as the steps are easy to understand and comprehend. There is no standard notation in the algorithm text as there is in the programming language (notation in the algorithm is called algorithmic notation). Everyone can create their own algorithmic writing and notation rules. In this regard, to facilitate the translation of algorithmic notation into a programming language, the algorithmic notation should correspond to the general programming language notation. For example:

Write the values ​​of X and Y

In algorithmic notation it becomes:

Write(X,Y)

This write notation means that the values ​​of X and Y are printed to the output device. This write notation corresponds to write or writeln in pascal, printf in C and cout in C++. So, the translation of write(X,Y) in each of these languages ​​is:

writeln(X,Y);    { dalam bahasa pascal }
printf("%d %d", X,Y);    /* dalam bahasa C */
cout<< X << Y;    /* dalam bahasa C++ */

Note that each programming language has its own rules for using writing commands.

Another example:

Enter the value of X into max

Written in algorithmic notation as:

max ← X

The notation "←" means to assign the variable max with the value of the variable X. The translation of the notation "←" into Pascal is ":=", in C is "=", in C++ is "=". The translation of max ← X in each language is:

  • max := X; { in Pascal }
  • max = X; /* in C */
  • max = X; /* in C++ */

3. Flow Chart

In structure English / Indonesian structure, the stages of problem solving are described using words (text). The weakness of this method is that the compilation of the algorithm is greatly influenced by the grammar of the maker, so that sometimes other people have difficulty understanding it. Therefore, a method was developed that describes the stages of problem solving by representing certain symbols that are easy to understand, easy to use and standard. One way to write these symbols is by using a flowchart. Flowcharts consist of two types, namely Program Flowcharts & System Flowcharts.

4. Program Flowchart

A program flowchart is a chart that describes the logical flow of data to be processed in a program from start to finish. A program flowchart is a useful tool for programmers to prepare complex programs. A flowchart consists of symbols that represent the functions of the program steps and flow lines that show the sequence of symbols to be worked on.


Standard Flow Chart Program Symbols

Example of using program flowchart:

5. Flowchart System

System flowcharts are different from program flowcharts. Program flowcharts are more detailed about the process steps in a program from start to finish. System flowcharts only describe the data flow of the system. The symbols used in system flowcharts are some that are the same and some that are different from the symbols used in program flowcharts.


System Flow Chart Standard

Example of Using System Flowchart:

Source:

© STMIK El Rahma Yogyakarta. Compiled by Mr. Eko Riswanto, ST, M.Cs.

Netizens

Q1:

IMAM PRAYUDA 2 Jun 2017, 14:46:00\
In creating an algorithm, is it necessary to have a separation procedure such as analyzing data, designing algorithms and so on?

Thank you  Imam Prayuda_Bina Darma University

A1:

About the separation procedure, maybe what you mean is specific purpose, so it is separated based on its functional purpose. Or in the sense that complex / large problems are broken down into small problems and these small problems are solved using special Blocks / functions / methods (specific purpose). If that is the analogy you mean, then there is a true similarity.

and regarding the separation/solving of this problem, it is not absolute, meaning there is no special procedure, meaning that each programmer has their own algorithm to solve their problems, what is a procedure/absolute is the symbols used, because these symbols have become an international standard/ISO 5807:1985.

History of Algorithms & Programming

The Russian stamp in Figure 5.1. next to this is a picture of a man named Muhammad ibn Mūsā al-Khwārizmī. For those of you who are involved in the world of computers, you should know who the person next to this is. He is an Islamic scientist whose works in mathematics, astronomy, astrology and geography have become the basis for the development of modern science. And from his name the term that we will learn in this chapter emerged. From Al-Khawarizmi it then changed to algorithm in English and was translated into algorithm in Indonesian.

Figure 5.1. Stamp depicting Muhammad ibn Mūsā al-Khwārizmī
Figure 5.1. Stamp depicting Muhammad ibn Mūsā al-Khwārizmī

The basic programming algorithm competency standard consists of four basic competencies. In the presentation in this book, each basic competency contains a description of the material and exercises. A summary is placed at the end of each chapter. The basic competencies in this chapter are explaining variables, constants and data types, creating algorithms/programming flow logic, implementing array management, and operating files. Before studying this competency, remember the operating system, problem-solving principles, and supporting materials from mathematics subjects.

At the end of the chapter, there are practice questions arranged from easy to difficult questions. These practice questions are used to measure the ability of this basic competency. This means that after studying this basic competency independently with the guidance of a teacher as a facilitator, measure your own ability by working on the practice questions.

Objective

After studying this chapter, it is expected that readers will be able to:

  • Explaining variables, constants and data types
  • Create algorithms/programming flow logic
  • Implementing array management
  • Operating files

1. VARIABLES, CONSTANTS AND DATA TYPES

Variables, constants and data types are three things that we will always encounter when we create a program. Any programming language from the simplest to the most complex, requires us to understand these three things.

1.1. Variables

Variables are places where we can fill or empty the value and call it back when needed. Each variable will have a name (identifier) ​​and a value. Consider the following example.

Example 5.1. Variable names and values

username = "joni"
Nama = "Al-Khawarizmi"
Harga = 2500
HargaTotal = 34000

In example 5.1. above, username, Name, price and TotalPrice are the names of the variables while "joni", "Al-Khawarizmi", 2500 and 34000 are the values ​​of each variable. These values ​​will be stored in their respective variable names as long as we do not change them.

In most programming languages, variables must be declared first to make it easier for the compiler to work. If the variable is not declared, then every time the compiler encounters a new variable in the program code, there will be a delay because the compiler must create a new variable. This slows down the compiler's work process. Even in some programming languages, the compiler will refuse to continue the compilation process.

Naming variables must follow the rules set by the programming language we use. However, in general there are rules that apply to almost all programming languages. These rules are:

  • Variable names must start with a letter.
  • Spaces are not allowed in a variable name. Spaces can be replaced with the underscore character (_). 
  • Variable names must not contain special characters, such as: .,+, -, *, /, <, >, &, (, ) and others. 
  • Variable names must not use keywords in programming languages.

Example 5.2. Example of variable naming

| Penamaan yang benar | Penamaan yang salah                                          |
|---------------------|--------------------------------------------------------------|
| namasiswa           | nama siswa (salah karena menggunakan spasi)                  |
| XY12                | 12X (salah karena dimulai dengan angka)                      |
| harga_total         | harga.total (salah karena menggunakan karakter .)            |
| JenisMotor          | Jenis Motor (salah karena menggunakan spasi)                 |
| alamatRumah         | for (salah karena menggunakan kata kunci bahasa pemrograman) |

1.2. Constants

Constants are variables whose data values ​​are fixed and cannot be changed. So constants are also variables, the difference is in the value they store. If the data value does not change throughout the program, then a variable is better treated as a constant. In a program code, usually the data value of a constant is given directly in the constant declaration section. While for variables, usually only the variable name and data type are specified without filling in the data value. Variable naming rules also apply to constant naming. Likewise, the rules for determining data types.

For example, if we are creating a mathematical calculation program that uses the value of pi (3.14159) which will probably appear in many places in the program code, we can make pi a constant. Using the constant pi will make it easier to write the program code than having to type the value 3.14159 over and over again.

1.3. Data Types

Data type is a type of data that can be processed by a computer to meet the needs of computer programming. Every variable or constant in the program code, we should determine the exact data type. The accuracy of selecting the data type on a variable or constant will greatly determine the use of computer resources (especially computer memory). One of the important tasks of a programmer is to choose the appropriate data type to produce an efficient and high-performance program.

There are many data types available depending on the type of programming language used. However, in general they can be grouped as in Figure 5.2.

Figure 5.2. Grouping data types
Figure 5.2. Grouping data types

Primitive data types are basic data types that are directly available in a programming language. While composite data types are formed data types that consist of two or more primitive data types.

Numeric data types 

Numeric data types are used in variables or constants to store values ​​in the form of numbers or digits. All programming languages ​​provide numeric data types, differing only in the types of numerics accommodated.

Types included in numeric data types include integers (whole numbers), and floats (fractional numbers). In addition to types, the precision of the numbers used is also applied in programming languages, for example the Single data type is a data type for fractional numbers with limited precision, while the Double data type is a data type for fractional numbers with more accurate precision. In the following chapters that discuss the application of programming languages, this section will be explained further.

Determining the numeric data type for a variable/constant must be done very carefully. The manual and instructions in each programming language on the data type section must be observed carefully. Consider the following example.

Example 5.3. Use of numeric data types

| Kode Program A                                                                                                                                                                                                                                              | Hasil eksekusi Program A |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------|
| #include <iostream> using namespace std; int main() {       int x, z;        float y;        x = 12;        y = 2.15;          z = x * y;       cout << "X =" << x << endl;       cout << "Y =" << y << endl;    cout << "Z =" << z << endl;  return   0; } | X  =12 Y  =2.15 Z  =25   |
| Kode Program B                                                                                                                                                                                                                                              | Hasil eksekusi Program B |
| #include <iostream> using namespace std; int main() {        int x;    float y, z;  x = 12.8;        y = 2.15;          z = x * y;       cout << "X =" << x << endl;       cout << "Y =" << y << endl;    cout << "Z =" << z << endl;  return   0; }        | X  =12 Y  =2.15 Z  =25.8 |
| Kode Program C                                                                                                                                                                                                                                              | Hasil eksekusi Program C |
| #include <iostream> using namespace std; int main() {        int x;    float y, z;  x = 12;  y = 2.15;          z = x * y;       cout << "X =" << x << endl;       cout << "Y =" << y << endl;    cout << "Z =" << z << endl;  return   0; }                | X  =12 Y  =2.15 Z  =25.  |

The three program codes above (A, B and C) are written in C++. At first glance they are the same but different in the use of data types and filling in values. In program code A, we declare variables x and z as int data types (Integer = whole numbers) and y as float data types (fractions). The results of executing program A show results that we do not want. The value of z which is the multiplication of x by y should be 25.8 (the result of 12 x 2.15). However, because z is declared as int data type, the result is 25. Of the three program codes above, the most correct is program code C. Why is program code B wrong? Try to observe the part of the code that is printed in bold and then determine where the error occurs.

Character

Along with numeric data types, character is the most widely used data type. Character data types are sometimes referred to as char or string. String data types can only be used to store text or anything as long as it is in double quotes ("...") or single quotes ('...'). Consider the following example.

Example 5.4. Using the character data type

#include <iostream>
using namespace std;
int main() {
int x;
x = 5;
char huruf = 'A';
char* kata = "Java";
cout << "X = " << x << endl;
cout << "Isi variabel huruf = "
<< huruf << endl;
cout << "Isi variabel kata = " <<
kata << endl;
return 0; }

Program execution results

X = 5
Isi variabel huruf
= A
Isi variabel kata =
Java

In this example we declare the variable x as int (Integer), while the letter and word variables are of char (character) data type. Note the results of executing the program code above.

Boolean

The Boolean data type is used to store True/False values. In most programming languages, values ​​other than 0 indicate True and 0 represents False. This data type is widely used for decision making in branching structures with IF ... THEN or IF ... THEN ... ELSE.

Array

Array or often referred to as array is a well-structured data type, although still simple. Array is able to store a number of data with the same type (homogeneous) in a variable. Each location of the array data is given an index number that functions as the address of the data. An explanation of arrays will be presented in more detail in another part of this chapter.

Record or Struct

Like Array, Record or Struct is a composite data type. Record is known in Pascal/Delphi language while Struct is known in C++ language. Unlike array, record data type is able to accommodate many data with different data types (heterogeneous). As an illustration, array is able to accommodate many data but with the same data type, for example integer only. While in record, we can use to accommodate many data with different data types, one part integer, one part character, and the other part Boolean. Usually record is used to accommodate data of an object. For example, student has name, address, age, place of birth, and date of birth. Name will use string data type, address is string data type, age is single data type (numeric), place of birth is string data type and date of birth is date data type. Here is an example of record declaration in Delphi.

Example 5.5. Declaring a record data type in Delphi

Type TRecord_Siswa = Record
Nama_Siswa : String[30]
Alamat : String[50]
Usia : Real
EndRecord

Image

Image or picture or image is a graphic data type. For example, a graph of the development of the number of vocational school students, our family photos, travel videos and others. In modern programming languages, especially those based on visuals, this data type has been very well supported.

Date Time

The data values ​​for date and time are stored internally in a specific format. Variables or constants declared with the Date data type can be used to store both dates and times. This data type is included in the composite data type group because it is a formation of several data types. The following is an example of a data type in Visual Basic.

Dim WaktuLahir As Date
WaktuLahir = "01/01/1997"
WaktuLahir = "13:03:05 AM"
WaktuLahir = "02/23/1998 13:13:40 AM"
WaktuLahir = #02/23/1998 13:13:40 AM#

Subrange

The subrange data type is a number data type that has a certain range of values ​​as determined by the programmer. Usually this data type has a minimum limit value and a maximum limit value. This data type is very well supported in Delphi. The following is an example of a subrange data type declaration in Delphi.

Example 5.7. Subrange data type declaration in Delphi

Type
BatasIndeks = 1..20
RentangTahun = 1950..2030
Var
Indeks : BatasIndeks
Tahun : RentangTahun

Enumeration

This data type is a data type that has elements that must be mentioned one by one and have integer constant values ​​according to their order. The integer constant value of this element is represented by a variable name written in brackets. This data type is also found in Delphi and declarative programming languages ​​such as SQL. The following is an example of an enumeration data type declaration in Delphi.

Example 5.8. Use of enumeration data types

Type
Hari_dlm_Minggu = (Nol, Senin, Selasa, Rabu,
Kamis, Jumat, Sabtu,
Minggu)
Nama_Bulan = (Nol, Januari, Pebruari, Maret,
April, Mei, Juni, Juli,
Agustus,
September, Oktober, Nopember,
Desember)
Var
No_Hari : Hari_dlm_Minggu
No_Bulan : Nama_Bulan

In the example above, the Day_of_Week data type includes an enumeration with a value range of Zero, Monday to Sunday and data values ​​from 0, 1, to 7. Meanwhile, the Month_Name data type includes an enumeration with a value range of Zero, January to December and data values ​​from 0, 1, to 12.

Object

Object data type is used to store values ​​related to objects provided by Visual Basic, Delphi and other GUI-based programming languages. For example, if we have a form that has a Command button control that we name Command1, we can declare the variable as follows:

Example 5.9. Use of the object data type

Dim A As CommandButton
Set A = Command1
A.Caption = "HEY!!!"
A.FontBold = True

In this example, variable A is declared as Object data type, which is CommandButton. Then we set variable A with Command button control on the form (Command1). In this way, we can access all properties, methods, and events of Command1 object using variable A.

Variants

Data type only exists in Visual Basic. This type is the most flexible data type among other data types, because it can accommodate all other data types as explained.

Multidimensional Array Algorithms & Functions

Figure 6.1. next to this I am sure you have seen it. Yes, this is the general notation of one of the mathematical theories, namely matrices. Matrix operations are operations that are widely used in many activities that require calculations with a model of many numbers. Try to reopen your math book that discusses matrices, you will see a lot of things we can do with matrices.

Have you ever heard of software called MATLAB? This software based on matrix operations is very popular among scientists, engineers and people who are involved in calculations. By using arrays you can also create programs that can perform matrix operations like MATLAB.

Advanced programming algorithm competency standards consist of four basic competencies, namely, using multidimensional arrays and using procedures and functions. In the presentation in this book, each basic competency contains a description of the material and exercises. A summary is placed at the end of each chapter. Before studying this competency, remember the principles of problem solving, basic programming algorithms and supporting materials from mathematics subjects.

At the end of the chapter, there are practice questions arranged from easy to difficult questions. These practice questions are used to measure the ability of this basic competency. This means that after studying this basic competency independently with the guidance of a teacher as a facilitator, measure your own ability by working on the practice questions.

Objective

After studying this chapter, it is expected that you will be able to:

  • Using multidimensional arrays
  • Using procedures and functions

1.1. Understanding Multidimensional Arrays

One-dimensional arrays like those in Chapter 5 are great for storing similar data in sequence, but what if we want to store a list of cities and their average temperatures together, or a list of student names and their test scores? In such cases, we can use two one-dimensional arrays, one for the names and one for the scores. However, this is not a good choice because it makes the code more complicated and inefficient. A better choice is to use a Multidimensional Array approach. We can store using a two-dimensional array for the above case. Look at the following figure to see the difference between two one-dimensional arrays and a two-dimensional array.

Figure 6.2. Differences between one-dimensional and two-dimensional arrays
Figure 6.2. Differences between one-dimensional and two-dimensional arrays

A two-dimensional array has two indices. The first index indicates the row while the second index is the column. In Figure 6.2, the NilaiSiswa array variable has two indices, namely the first index 4 which states the maximum index value for the row is 4 (or there are 5 rows because the first row index is 0), while the second index is 1 which indicates the maximum index value for the column is 1 (or there are 2 columns because the first column index is 0). To declare a two-dimensional array, the following method can be used:

Dim NilaiSiswa(4,1)

Meanwhile, to access values ​​in a two-dimensional array, you can use the following example.

Print NilaiSiswa(3,0)
Print NilaiSiswa(3,1)

The first statement of example 6.1, will produce the output "Dono" (Row 4 and first column) while the second statement will produce the output 56 (Row 4 second column).

The advantage of using multidimensional arrays is that conceptually, these arrays are easier to manage. For example, we want to create a game program and we want to find the position of a place on a game board. Each square can be identified using two numbers, namely its horizontal and vertical coordinates (or rows and columns). This structure is typical of using a two-dimensional array. The horizontal coordinate is the row index while the vertical coordinate is the column index. This form of multidimensional array can be developed into more than two dimensions. The statement Dim Matrix(9,9,9) will create a multidimensional array that has 1000 elements (10x10x10).

1.2. Matrix Operations with Multidimensional Arrays

As mentioned at the beginning of this chapter, we can use arrays to perform matrix operations. Consider Figure 6.3 below.

Figure 6.3 shows a two-dimensional matrix consisting of 4 rows and 3 columns. Or commonly denoted as A4x3. This is identical to the multidimensional array that we define as A(3, 2). Note again that the number in the array index is always one less than the actual number because the index always starts with 0. While the index in the matrix starts at number 1. To create an array or matrix like in Figure 6.3, we need a looping structure. We can use the for form because we already know exactly how many rows and how many columns. The following figure presents a flowchart to create the matrix in Figure 6.3.

In Figure 6.4. we see two counter variables, namely I and J. I is used to create an index on a row, namely 4 rows (1 to 4), while J is used to create an index on a column, namely 3 columns (1 to 3). The data reading will take place as follows:

A(1,1) = ....
A(1,2) = ....
A(1,3) = ....
A(2,1) = ....
A(2,2) = ....
A(2,3) = .... dan seterusnya.

Figure 6.4. Algorithm for creating a 4 x 3 matrix.
Figure 6.4. Algorithm for creating a 4 x 3 matrix.

Example 6.2. Addition operations on matrices

Consider the following matrix:

The matrix operation above is the addition operation of two matrices with the same dimensions, namely 3x3. What is the algorithm for solving it?

Solution:

In the example above, there are two matrices, for example A is the first matrix and B is the second matrix. In addition, a third matrix is ​​also needed, namely C as the result of adding matrices A and B. Figure 6.5 shows the algorithm for adding two matrices.

Figure 6.5. Algorithm for adding two matrices
Figure 6.5. Algorithm for adding two matrices

2. Procedures & Functions

Every programming language always provides functions that have been defined by the programming language (built-in function). However, there are times when we need a certain procedure that we use repeatedly and is not available in the built-in function.

A procedure is a set of commands that are part of a larger program that functions to perform a specific task. Procedures or sometimes called subroutines / subprograms are usually relatively independent of other parts of the program code. Or actually procedures can stand alone. The advantages of using procedures are:

  • reduce program code duplication.
  • provides the possibility of reusing code for other programs.
  • breaking down complex problems into smaller, easier to solve problems (see the principle of problem reduction in Chapter 2).
  • make program code easier to read.
  • can be used to hide program details.

In the flowchart to write the procedure, the Predefined Process notation is used (see Chapter 5 for flowchart notation). Schematically, the use of the procedure can be seen in Figure 6.6.

Figure 6.6. Schematic of procedure usage.
Figure 6.6. Schematic of procedure usage.

Figure 6.6. shows that there is a main process that occurs and there is a procedure that is actually part of this main process. When the main process requires a certain task, the main process will call a certain procedure to complete the task. Consider the following example:

Example 6.3. Procedure

Create an algorithm to calculate the area of ​​a triangle, quadrilateral, and circle.

Solution:

To create this algorithm we can view the process of calculating the area of ​​a triangle, the area of ​​a square and the area of ​​a circle as a stand-alone program. We can create a procedure for each process. And we will call the procedure from the main process (Figure 6.7).

Figure 6.7. Solving example 6.3.
Figure 6.7. Solving example 6.3.

The process sequence in Figure 6.7 is as follows.

  1. Data reading
  2. In the main process, the data that is read will be checked,
  3. If the data read is for a triangle, the main process will call the triangle area calculation procedure by bringing the variable values ​​required by the triangle area calculation procedure.
  4. The process of calculating the area of ​​a triangle is only carried out in this procedure.
  5. After the calculation process, the calculation results will be brought back to the main process to print the results.

The same process sequence also occurs if the data read is for a rectangle or circle.

A good procedure has the following characteristics:

  1. Only have one objective function (logical inherent). A procedure should only have one objective function and not be mixed with other objectives. This is to make the procedure more focused so that the objectives can be achieved properly.
  2. Independent. A procedure should be independent, meaning that it can run and be tested without waiting for other parts to complete. In addition, the variables used in the procedure do not affect the variables used in other parts of the entire program.
  3. Small size. What is meant by size here is the length of the algorithm or the length of the program code in a procedure. Small size will be easy to read and repair. If a module is too large, it should be considered to be broken down into several smaller modules.

3. Summary

  • A two-dimensional array has two indices. The first index shows rows while the second index shows columns.
  • Reading and writing data on multidimensional arrays is done by directly pointing to the index number.
  • A procedure is a set of commands that is part of a larger program that functions to perform a specific task.

4. Practice Questions

  1. Consider example 6.1 above. Create an algorithm for the subtraction operation of two matrices.
  2. Also create an algorithm for the multiplication operation of two matrices.
  3. Using the procedure, create an algorithm that reads matrix data and then selects to perform addition, subtraction and multiplication operations on two matrices.

Assembly Program Structure & Flowchart Symbols

| SMKN 2 YOGYAKARTA                 | Kompetensi                     | Kode            : 06/PDTM/KL-XII/1 |
|-----------------------------------|--------------------------------|------------------------------------|
| Prog. Keahlian : Tek. Audio Video | SIMBOL SIMBOL STRUKTUR PROGRAM | Waktu          :      menit        |
| Prog. Diklat     : PDTM           |                                | Nama Siswa :                       |
| Kelas                : XII AV     |                                | Tanggal        :                   |

Performance Criteria:

  • Get to know the symbols of program structure or flowchart
  • Using program structure symbols to describe the program's flow
  • Translate program structure/flowchart into programming language.

A. Program Structure / Flowchart

Program structure/flowchart can be used to describe the behavior or sequence of a program (using appropriate images or symbols). When a complete program structure/flowchart has been completed, a complete picture of a programmer's thought process in solving a problem can be followed. The role of program structure/flowchart is very important, especially in program examination. The program structure/flowchart which is an important part of a completed program can also help others to correctly understand the sequence of a program created by the programmer.

There are two types of program/flowchart structures, namely:

  1. Program structure/system flowchart: shows the general flow of the program (outline only).
  2. Detailed program structure/flowchart: contains details that are especially useful for programmers.

Usually a complex program is preceded by a system program structure/flowchart, then completed with a detailed program structure/flowchart. The advantage of a program structure/flowchart is that it shows the sequence of steps using arrow symbols (the direction of the program).

Program Structure / Flowchart Symbols:

B. Creating a program structure

Example: If the Accumulator stores data 7A, register B stores data 5F.

Command: move to register H data from the Accumulator and move to register L data from register B.

Solution:

| Isi Accumulator dengan data 7A                |
|-----------------------------------------------|
| Isi register B dengan data 5F                 |
| Pindahkan ke register H data dari Accumulator |
| Pindahkan ke register L data dari register B  |
| Akhir program                                 |

Translation into program language:

| Alamat Memori | Bahasa     |              | Keterangan                                                                                |
|---------------|------------|--------------|-------------------------------------------------------------------------------------------|
|               | Assembly   | Mesin        |                                                                                           |
| 1800          | LD A,7A    | 3E   7A      | Mengisi Accumulator dengan data 7A                                                        |
| 1802          | LD B,5F    | 06   5F      | Mengisi register H dengan data 00                                                         |
| 1804          | LD B,H     | 44           | Memindahkan ke register B data dari register H                                            |
| 1805          | LD HL,1A00 | 21  00  1A   | Mengisi pasangan register HL dengan data 1A00                                             |
| 1808          | LD (HL),E  | 73           | Memindahkan ke alamat memori yang ditunjuk oleh pasangan register HL data dari register E |
| 1809          | LD HL,1A01 | 21   01   1A | Mengisi pasangan register HL dengan data 1A01                                             |
| 180C          | LD (HL),B  | 70           | Memindahkan ke alamat memori yang ditunjuk oleh pasangan register HL data dari register B |
| 180B          | RST 0038   | FF           | Akhir program                                                                             |

Post a Comment

Previous Next

نموذج الاتصال