BASICS ALGORITHM AND PROGRAMMING

Computer programming

In everyday life, to communicate with others, we should use the same language as the person. If we use Indonesian, our speaker should also understand Indonesian. If the other person we do not understand Indonesian, we can still communicate with them through an interpreter.
In computer programming, applies also things like the above. We should use a language understood by the computer to give an instruction.
Basically, the computer is a digital machine, meaning the computer just knows no electric current condition (usually denoted by 1) and no electric current (usually denoted by 0). In other words, we must use a password 0 and 1 to do computer programming. The programming language using a password 0 and 1 is called machine language. Perhaps you can already imagine how difficult it is to program the machine language. However you do not need to worry because nowadays rarely the person who programmed the machine language.
Because machine language is very difficult, then came the idea to symbolize the password string of 0 and 1 by the acronym that is more easily understood word human. This word stands for then often called mnemonic code. The programming language using abbreviations is called assembly language.
For example, in the Intel processors, there is a command 0011 1010 0000 1011. Orders in machine language is tantamount to a command assembly CMP AL, OD, which means compare the value of the AL register with 0D. CMP here is actually the abbreviation of the compare. DapatAnda see here that the command CMP AL, 0D much easier to understand than 0011 1010 0000 1011. Of course, if viewed from a human viewpoint, for the computer, a combination of 0 and 1 would be more easily understood. The software that converts commands into machine language assembly is often referred to as assembler.
Assembly language programming with many feel is still too difficult, including myself. Sesusai with human nature that is never satisfied, finally developed a programming language that is easier to use. This programming language using words that are easily recognizable by humans. The programming language is called a third-generation language or abbreviated 3GL (thirdgeneration language). Some people call this language with high-level language or abbreviated HLL (high-level language). There are many examples of third-generation language, such as: Basic, Pascal, C, C ++, COBOL, etc.


Third-generation language normally used words in the English language because the language is the international language. For example, you can write the following command in the Pascal language:
writeln ( 'Algorithm');
or the following command in the C language:
printf ( "algorithm \ n \ r");
or the following command in C ++:
cout «" Algorithm "« endl;

The third statement above the same aim, namely to write text 'Algorithm' to the standard output (standard output), usually to the computer screen.
These three simple command above is actually made up of dozens or maybe even hundreds of statements assembly. The software that translate the program into human language into assembly language or machine there are two kinds of interpreters and compilers.
Interpreter translates the program line by line, meaning that if a line to be executed, then the line first translated into machine language. If the next line to be executed, then the new line is translated into machine language. Examples of programming languages that use the interpreter is Basic.
Compilers translate all the instructions into machine language and then run the result of translation. This translation results can be saved in a file or memory. Examples of programming language compiler uses is Pascal, C, and C ++. Especially for Pascal, there are a few compilers but indeed not many, such as Turbo Pascal (http://www.innrise.com), GNU Pascal (http://didaktik.physik.uni-essen.de/-gnu-pascal/ home.html), Free Pascal (http://www.freepascal.sk), etc. Nonetheless, Pascal compiler most widely used is Turbo Pascal. So the book also uses Turbo Pascal as a reference.
The development of programming languages was not until the third-generation language only. There is a next-generation programming language, which is a fourth-generation language or abbreviated 4GL (fourth-generation language). This language is widely used to develop a database application (database). One example is SQL (Structured Query Language). In this language, the commands are used more humane, such as "SELECT Name, Address FROM Employees", to retrieve data Name and address of the database Employees.
From this brief explanation, I hope you can understand where is the actual position of the Pascal language is reviewed in computer programming. In the next section, I will discuss where is the position in computer programming algorithms.

Steps in Computer Programming

In doing an activity, of course we need the measures that must be passed. People sleep alone, according to the doctors, through several stages or phases. In computer programming, we also requires several steps.
The sequence of steps that I present in this book are not absolute, which means you do not have to do it from the first step, the second, third, and so on. There are several steps that must or may be repeated to many times. Furthermore, other books may present measures have been different but essentially the same. Here are some steps to be done in computer programming:

1. Define the problem. The first step is often overlooked by many programmers. Once they got the order to create a program, they immediately write programs without defining the problem first. I quote the words of Henry Ledgard in one of his books about Murphy's Law for programming:
The faster you write the program will be the longer you solve it.

Indeed there is no scientific proof of these words, but from my personal experience as a research assistant at the University of Gadjah Mada in 1995 and 1996, these words turned out to be a lot of truth.

Before stepping in a second step, I remind you once again that the first step, defining the problem, it is very vital but often forgotten. Determine what the problem is, then what should be solved by a computer, the latter is what the input is and how outputs.

2. Determine the solution. Once the problem is clearly defined, input what is given is clear, what is the desired output is clear, the next step is to find a way how the problem is resolved. If the issue is too complex, usually we have to divide it into several small modules to be more easily resolved.
For example, a program to calculate the inverse matrix, can be divided into several small modules. The first module is asking for input from users in the form of a square matrix. The second module is to find the inverse matrix entered by the user. The last module is to show the results to the user. You will learn to divide the problem into several small modules in Chapter 5, The subroutines.

  3. Choose the algorithm. This step is one important step in computer programming. Why? Due to the wrong choice of algorithm would cause the program to have a poor performance. These algorithms which I will discuss throughout this book, so I will not discuss it at length here.

4. Write program. In this step, you start to write a computer program to solve a given problem. As I explained earlier in this chapter, to write a program you can use one of the third-generation language. I do not recommend the machine or assembly language because it is too difficult and a waste of time. Remember the old adage says no time is money. Indeed, many people are happy to use the assembly with high speed reasons, but the time required to write the program is also very long. In other words, the price paid for such high speeds is quite expensive. I also do not recommend the use of fourth-generation language for more in the world of databases.


There are several things to consider when choosing a programming language, among other problems, the programming language you are good, and so on. In this book, you will specifically write programs using Pascal language.

 5. Testing program. After the program is finished, you should test it. The first test is whether the program successfully compiled well? The next test: whether the program can display the desired output?

Furthermore, you also need to test the program with a lot of other cases. It often happens, a good walking program for the case of A, B, C; but produces something that is not desirable for the case of X, Y, and Z.

The fourth and fifth steps can be repeated until the program is believed to actually work as expected.

6. Writing documentation. This is usually done in conjunction with the writing program, meaning that on each line program or every few lines of programming, you add comments that explain the usefulness of a statement.

This documentation seems trivial and largely forgotten people, but its function is very important. In the future, if you need to make changes or improvements to a program, you will feel the importance of good documentation. Nonetheless, most of the examples in this book program does not include documentation. This is because the documentation of the program is none other than the book itself.
Documentation which is integrated into the program, in the form of brief comments, is usually sufficient. But it would be better if you write them in a separate document and then print it out on paper if necessary. I do not really recommend documentation on paper, but the most important thing is to add brief comments in your program. Maybe now you think it is useless, but believe me later you will feel the importance of this.

7. Caring program. The move comes after the program is finished and is already used by your users. It most often occurs here is the emergence of a bug that sebelumnva undetected. Or perhaps a user wants additional new facilities. When things like this happen, you need to revise your program.


What is Algorithm?

Of material from the previous section, you already know where is the position in the world of computer programming algorithms or more generally in the world of computers. Now I will discuss the algorithm itself. There are several definitions of the algorithm, but here I use Microsoft Bookshelf reference:
An algorithm is a finite sequence of steps to solve the problem of logic or mathematics. '

In everyday life, you actually also uses an algorithm to do something. For example, you want to write a letter, then you need to perform the following steps:
1. Prepare paper and envelopes.
2. Prepare stationery, such as pens or pencils.
3. Start writing.
4. Insert the paper into the envelope.
5. Go to the post office to mail the letter.

Steps from number 1 to number 5 above is what is called an algorithm. So your own actual algorithm also uses both consciously and unconsciously.
In many cases, the algorithms do not always sequentially as described above. Sometimes you have to choose two or more options. For example, if you want to eat, you have to specify'll eat in a restaurant or cook yourself. If you choose to eat in a restaurant, you will run different algorithms with if you choose to cook yourself. In the world of algorithms, this sort of thing often called branching.

In other cases, you may need to perform certain steps several times. For example, when you write a letter, before me insert the paper into the envelope, maybe you should check whether the letter is correct or not. If not true, then you have to prepare a new paper and write again. Continue until your letter as expected. In the programming world, this sort of thing often called repetition.

example algorithm

Here is an example of algorithms for solving mathematical problems. Suppose you want to calculate the input of a wide circle of radius of the circle. Formula for the area of a circle is:
L = π.R2
Here is an example of an algorithm to calculate the area of a circle:

1. Insert R.
2. Pi ← 3.14.
3. L ← Pi * R * R.
4. Write L

Notice there are signs ← on the second and third row. That sign means the value on the right given to the operand on the left. For example, for the second row, the value given to the variable Pi 3:14. Next, the value of Pi * R * R given to the variable L.
The first line of the above algorithm expects user input in the form of a circle whose radius is stored in the variable R. In the second line the value of π is stored in the variable named Pi. The third line calculate the area of a circle with a formula π R2 or written Pi * R * R. The area of this circle is stored in the variable L. The last line write area of the circle.

To facilitate understanding of the algorithm, usually people describe a flow diagram (flow Charf). Figure 1.1 is an example of an algorithm flowchart for calculating area of a circle.

pict 11 Algorithm counter area of a circle.
Branching

The next example is an algorithm to write the absolute value of the value entered by the user. The definition of the absolute value is as follows:
│x│ = x, if x> = 0
│x│ = -x if x <0

Here is the algorithm to write the absolute value of the user input:

1. Enter x.
2. If (x <0) then the working line 3, if it is not working on the 4th line.
3. x ← -x.
4. Write x.

The first line asks fill a number of users stored in the variable x.
The second line check the value of the variable x is less than 0 or not. If yes, which is done is the third line, but if it is not done is the fourth line.
The third line makes the value x becomes positive because this line is only done if the value of x is less than 0.
The last line write down the value of x is always positive.


Figure 1.2 is an example flowchart for the algorithm above.

Figure 1.2. Algorithms write absolute value.


Repetition

To clarify the writing of the algorithm, I will present one more example, the algorithm for calculating the average of a set of data that users enter. Formula to find the average of the data x. totaling N is as follows:
   
Here is the algorithm for calculating the average of the data entered by the user:

1. Insert N.
2. i ← l.
3. j ← 0.
4. As long as (i <= N) are working rows 4 to 7.
5. Enter dt.
6. I ← i + l.
7. j ← j + dt
8. Average ← j / N.
9. Write Rata.

The first line asks the user to enter N, the amount of data.

On the second line, the variable i, which is useful as a counter how much data the user has entered, rated 1.
On the third line, the variable j, which is used to store the sum of data, given the value 0.
The fourth line gives the command to repeat the fourth row to the seventh row for i is less than or equal to N. In other words, after i is greater than N, the eighth row run.
Fifth line enter the data requested all i.
Sixth row adds to the variable i by 1. Note the meaning of the command i ← i + 1 is the first value plus 1 and then the results are stored in the variable i return.
Seventh row j add variable data entered by the user. As described above, the variable j is used to store the sum of all the data, so for each input the data, the value of the variable j should be coupled with dt.
Eighth row to calculate the average by dividing the sum by the number of data.
The last line write this average.
Figure 1.3 is an example flowchart for the algorithm above.

Figure 1.3. Algorr'tma menght'tung average of a set of data.

Endnotes

Just a final note, the actual algorithm used to assist you in converting a problem into computer language. Therefore, many experienced programmers who have never writing algorithms on paper anymore. This means that he wrote the algorithms in his head and he immediately reprogrammed by the algorithm is in his head. In the final chapters of this book, you will rarely find writing algorithms explicitly as above because I assume you've been able to write it in the head.
In subsequent chapters, I am not going to draw a flow chart again. If you have difficulty learning algorithm is presented, you can draw them yourself on paper.
Exercise

1. Write an algorithm to find the area of a triangle if the user input is high pedestal clan.

Hint: Area of triangle can be calculated by the formula:
L = 1 / 2.a.t
a: a triangular pedestal and t: height triangle.
2. Write an algorithm to find the hypotenuse of a right triangle if known length of the two sides that form a right angle.


Hint: sloping sides of a right triangle can be calculated using the Pythagorean formula:


with a and b are the sides of the square.

3. Write an algorithm to solicit input two numbers from the user and displays the largest number between the two numbers.
4. Write an algorithm to solicit input three numbers from the user and displays the smallest number among the three numbers.
5. Write an algorithm to display the text "Turbo Pascal" 100 times.
6. Write an algorithm to find the mode of a set of data entered by the user.
Hint: The mode is the data that is most frequently arise.

0 Response to "BASICS ALGORITHM AND PROGRAMMING"

Posting Komentar