Algorithms & Structured Programming (ASP)


ASP:   SOLD       




Substance:

  1. ALGORITHM CONCEPT
  2. STRUCTURED PROGRAMMING CONCEPT
  3. PROGRAMMING ELEMENTS


Algorithms and Structured Programming

1. Algorithm Concept

Algorithm comes from the words algorithmic and rhythmic which were first expressed by Abu Ja'far Mohammad Ibn Musa Al Khowarizmi (825M) in the book Al-Jabr Wa-al Muqobla. In programming, an algorithm means a special method that is precise and consists of a series of structured and systematically written steps that will be carried out to solve problems with the help of a computer.

In simple terms, an algorithm can be defined as a sequence of logical steps to solve a problem that are arranged systematically. The word logical means that its truth value must be able to be determined, true or false. Incorrect steps can produce incorrect results.

Example:

Suppose there are two glasses, glass A and glass B. Glass A contains red water and glass B contains blue water, we want to swap the contents of the two glasses, so that glass A contains blue water and glass B contains red water.

Glass_Swap_Algorithm

  1. Pour water from glass A to glass B.
  2. Pour water from glass B to glass A.

The algorithm above does not produce a correct exchange, the steps are not logical, because what happens is not an exchange but a mixing of water in glass A with water in glass B. So the Exchange_Glass_Fill algorithm above is wrong. From the problem above, the correct algorithm is that to exchange the contents of water in glass A with the contents of water in glass B, an auxiliary glass is needed which is used to hold one of the water in the glass, for example glass C. So the correct algorithm for the problem above is:

Glass_Swap_Algorithm

  1. Pour water from glass A to glass C.
  2. Pour water from glass B to glass A.
  3. Pour water from glass C to glass B.

Initial state before exchange

Exchange process:

1. Pour water from glass A to glass C.

2. Pour water from glass B to glass A.

3. Pour water from glass C to glass B.


Exchange results

Now the above Glass_Swap_Fill algorithm has been improved, so that the water content in glass A and the water content in glass B can be exchanged correctly. The relationship between the algorithm, problem and solution can be described as follows:

  • The problem solving stage is the process from a problem to the formation of an algorithm.
  • The implementation stage is the process of applying the algorithm to produce a solution. 
  • The solution in question is a program which is an implementation of the algorithm that has been compiled.

The characteristics of a good algorithm are:

  1. Algorithms have calculation logic or precise methods in solving problems.
  2. Produce precise and correct output in a short time.
  3. The algorithm is written in a standard language in a systematic and neat way so that it does not give rise to ambiguous meanings.
  4. The algorithm is written in a format that is easy to understand and easy to implement in a programming language.
  5. All required operations are clearly defined.
  6. All processes in an algorithm must end after a certain number of steps have been completed.

2. Structured Programming Concept

Structured programming is an action to organize and create program codes so that they are easy to understand, easy to test, and easy to modify.

Characteristics of structured programming techniques:

  1. Contains proper and correct problem solving techniques.
  2. Have a problem solving algorithm that is simple, standard and effective in solving problems.
  3. Program writing techniques have a correct and easy to understand logical structure.
  4. A program consists solely of three structures, namely sequence structure, looping structure and selection structure.
  5. Avoid using GOTO instructions (unconditional process transition) which makes the program unstructured.
  6. Requires low testing costs.
  7. Have good documentation.
  8. Requires low maintenance and development costs.

3. Elements in Programming Languages

When we learn a programming language, we will encounter elements that are basically similar between one language and another. This is because these elements are part of the grammar of the programming language in question. Here are the elements in a programming language:

  1. Lexical Rules
  2. Data type
  3. Expression
  4. Statement
  5. Function and Procedure

We will discuss the elements mentioned above one by one.

1. Lexical Rules

What is meant by lexical rules are the rules used in forming a declaration, definition, or statement to become a complete program. These rules include several elements, including:

  • a. Token
  • b. Comments
  • c. Identifier
  • d. Keywords (Reserved Words)
  • e. Operator

1.a. Token

Token is the smallest element in a programming language that has an important meaning for the compiler. Tokens include: identifiers, keywords (reserved words), operators, and so on. One token is separated from another by one or more spaces, tabs, new lines, or comments.

1.b. Comments

Comments are text (a collection of characters) that are ignored by the Compiler. Comments are very useful for providing notes on certain parts of a program as a reference for both the programmer himself and others who read the program code. In Pascal, text that is between the opening curly brace {and the closing curly brace } will be considered a comment. In addition, you can also use the ( sign as the comment opener, and the ) sign as the closing one. Example:

In C language, text between / and / will be considered as a comment. And for text after // will also be considered a single line comment. Here is an example of using comments in C language:

1.c. Identifier

Identifier is a collection of characters used as a marker for variable names, data type names, functions, procedures, and so on. The rules for writing identifiers in Pascal and C can be said to be similar. Namely: an identifier must begin with a non-numeric character as follows:

_ a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Next, you may use the numeric characters  ( 0 1 2 3 4 5 6 7 8 9 ) or non-numeric characters above, but you may not use special characters such as  + - * / ? ! { } [ ] and so on. The following are examples of correct and incorrect identifiers.

  • _Name = True
  • Phone_No = True
  • Number2 = True
  • 4data = Wrong, because it starts with a numeric character: 4data
  • Text? = Wrong, because it contains special characters: Text?

But as a note to remember, identifiers in Pascal are case insensitive (uppercase and lowercase letters are considered the same), while in C, identifiers are case sensitive (uppercase and lowercase letters are distinguished). For example, the identifiers No_Telpon and no_telpon in Pascal are considered the same, while in C, they are considered two different identifiers.

1.d. Keywords (Reserved Words)

Keywords or Reserved words are words that already exist/are defined by the programming language in question. These words already have a fixed definition and cannot be changed. Because they already have a certain definition, these words cannot be used as identifiers.

In the Pascal language, reserved words include:

In the C language, reserved words include:

1.e. Operator

Operators are used to express a calculation/operation. Operators used for operations involving one operand are called unary operators. If they involve two operands, they are called binary operators, and if they involve three operands, they are called ternary operators. There can be many operators in an operation. The order of execution of operators is also called operator precedence. The lower precedence will be executed later, for example:

A = 10 + 5 * 2

Because the precedence of the * operator is higher than the + operator, the value of A is 20, obtained from multiplying 5 and 2, then adding it to 10. To prioritize the execution of a lower precedence, the ( and ) sign can be used, for example:

A = (10 + 5) * 2

Variable A will have a value of 30, obtained from adding 10 and 5, then multiplying by 2. Operators can be categorized into several types, including:

  1. Arithmetic Operator
  2. Assignment Operator
  3. Bitwise and Logical Operators
  4. Relational Operator
  5. Pointer Operator

The following will discuss the five groups of operators above. As a note, there are still operators that are not covered in the groups above. What is discussed here are only the very general parts.

1.e.1. Arithmetic Operator

Arithmetic operators include operators used to perform arithmetic operations, such as:

  • addition: + (in C language, there is also the operator ++ as an increment prefix. For example, the operation: i++ or ++i, will increment the value of i by 1)
  • subtraction: - (like addition, in C language, there is also a -- operator as a decrement prefix)
  • multiplication: *
  • division: / (in Pascal, there is also a div operator which is used to divide integers)
  • find the remainder of the division: In Pascal language it is the mod operator, while in C language it uses the % sign. The operators mentioned above are binary operators because they involve two operands. There are also unary operators, namely the -- and + signs which are used as markers for negative or positive numbers.

1.e.2. Assignment Operator

This operator is used to assign a value to an identifier. In Pascal, a colon and an equal sign := are used to assign a value to a variable. Example:

C := A + B; atau C := 4;

In the C language, there are several assignment operators, namely:

  • The = sign has the same function as the := sign in the Pascal language.
  • The += sign is used to perform addition assignments, for example there are two operations as follows:
  • C = 4;
  • C += 3;
  • After the first line is executed, C has a value of 4. After the second line is executed, C has a value of 7.
  • The -= sign is used to perform subtraction assignments. How to use it is the same as the example of using the += sign above.
  • The <<= sign is a left shift assignment, used to shift bits to the left.
  • The >>= sign is a right shift assignment, used to shift bits to the right.

1.e.3. Bitwise and Logical Operators

These operators are used to perform bit and logic operations. Included in these operators are:

Negation

bahasa Pascal : NOT contoh A := NOT B;
bahasa C : ! contoh A = !B;

And

bahasa Pascal : AND contoh A := A AND B;
bahasa C : && contoh A = A && B;

Or

bahasa Pascal : OR contoh A := B OR C;
bahasa C : || contoh A = B || C;

Shift Left

bahasa Pascal : shl contoh A := B shl C;
bahasa C : << contoh A = B << C;

Shift Right

bahasa Pascal : shr contoh A := B shr C;
bahasa C : >> contoh A = B >> C;

1.e.4. Relational Operator

Relational operators are used to compare the values ​​of two operands. Note that the operands

the numbers being compared must have the same data type, except for integers (of type

integer) and fractional numbers (of type real or float). Relational operators include:

  • Equality marker = (In C, the equality marker uses two equal signs, namely == )
  • Larger marker >
  • The greater than or equal to >= marker
  • Smaller marker <
  • Less than or equal to marker <=
  • Marker of inequality. The Pascal language uses the <> sign, while the C language uses the != sign

1.e.5. Pointer Operator

Pointer operators are used to perform operations on operands that are pointers. In Pascal, the ^ sign is used as a deference pointer. While in C, deference pointers use asterisks *.

Source:

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

Netizens

Q1: MICKY Aug 23, 2016, 16:36:00 = Is there a book to study by yourself? Or maybe it can be done by distance learning?

A1: Gramedia should have them, please ask the CS there to find basic books on structured/system programming or Basic C++ Programming.


Post a Comment

Previous Next

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