Algorithm and flowchart (flowchart and NS)

Algorithm and flow chart

2.1.1 Algorithm

Computer language is just a tool. Light learn the rules of the language is not enough, the most important is to learn for each type of problem, to work out effective solutions and steps namely algorithms. With the proper and efficient algorithms that can take advantage of any high-level language computer program that allows the computer to work. Therefore, the design algorithm is the core programming.

Not only "calculation" issue only algorithms. Broadly speaking, methods and steps taken to solve a problem, known as "algorithm." Do not "calculated" (computational method) and "algorithm" (algorithm) the two words confused. The former refers to the approximate numerical solution method, which means a step by step problem solving process. In solving a numerical problem, in addition to select the appropriate method of calculation, but also how to write a computer algorithm to solve executed step by step according to this calculation method. For the uninitiated computer, they can just use ready-made algorithms others have been designed simply to give the necessary input in accordance with requirements of the algorithm, we can get the result output. For them, the algorithm as a "black box", they can not understand the structure of the "black box" in the just understanding the role of algorithms from the external characteristics, you can easily use the algorithm. But for programmers, you will have to design algorithms and programming according to an algorithm.

The same problem, can have different problem-solving methods and procedures. For example, seeking 1 + 3 + 2 + ... + 100, 1 + 2 may be performed first, plus 3, plus 4, has been added to 100, may also take 100 + (1 + 99) + (2 + 98) + ... + (49 + 51) + 50 = 100 + 50 + 49 × 100 = 5050. There may be other methods. Of course, the method has good or bad. Some methods With just a few steps, and some methods more steps are needed. In general, desirable to employ simple, less calculating steps of the method. Therefore, in order to effectively solve problems, not only we need to ensure that the correct algorithm, but also consider the quality of the algorithm, select the appropriate algorithm.

A computational problem solving process generally comprises the following steps:

          Establish required to solve the problem and, finally, the requirements to be met. We must ensure that the task outset it has a detailed and precise understanding, to avoid ambiguity and ambiguities.

          Structural model analysis of the problem. After getting a basic physical model, it is described by the language of mathematics, for example, lists of mathematical formulas or problem solving simultaneous equations, i.e., a mathematical model.

          Selecting calculation method. The definite integral evaluated problem, various methods can be rectangular method, or a method of Simpson trapezoidal method. Thus solving computer to determine which should be calculated using a method. There is a special discipline "calculation method", is to study what means the most effective, most closely to achieve a variety of numerical calculation, in other words, the calculation method is an approximate method of numerical calculation of.

          Determination algorithm and draw flowcharts. Before writing program, it should be collated ideas, imagine how good step by step operation or treatment is "algorithm." By drawing it out of the box, it represents one or several steps to be completed with a frame, which represents the work flow, referred to as a flowchart. It enables people to think clearly, to reduce programming errors.

          Programming.

          Debugging, namely spreadsheet. More often than not a complicated procedure once the machine can pass and get the right result, you need to repeatedly modify the spreadsheet, only to get the right to run for a formal program.

           Official running the necessary result of the operation.

2.1.2 flow chart

To represent an algorithm can be used in different ways. Commonly used are: natural language; conventional flowchart; structured flowchart; pseudo code; the PAD diagrams. Here we introduce a flow chart.

a) Traditional flowchart

Algorithm is represented by the flow chart in FIG. It is a flowchart showing some of the frame to various types of operation, in each frame writing step, followed by arrowed lines connecting them to represent the order of execution. Representation algorithm, visual image graphically, easy to understand.

American National Standards Institute (ANSI) has defined a number of common flowchart symbols, is widely used around the world worker program. The most commonly used symbols flowchart shown in Figure.

        Processing block (rectangle), represented by a general processing.

        Decision block (diamond), expressed on a given condition determined according to a given condition is satisfied determine how to implement the subsequent operations. It has an entrance, two outlet.

        Input-output block (block parallelogram).

        Start and end block (circular arc-shaped frame), it represents the flow start or end.

        Connection point (circle), for drawing in different parts of the flow line connected. As shown, there are two connection points to a flag (at the connection point circle write "l") indicates that two points are connected together, as equivalent to a point. Connection point, to avoid the flow lines cross or too long, so flowchart clear.

        Flow line (point line), and indicates the direction of the flow path.

        Comment box, in order to operate on some of the blocks of the flow chart to make the necessary supplement to help people read the flow chart to better understand the role of the flowchart. It is not a necessary part of a flowchart, and the operation flow is not reflected.

Block diagram showing the contents of the program sequence of the steps and their relationships and executed. It illustrates the logical structure of the program. Block diagram should be sufficiently detailed so that you can successfully write the program in accordance with it, rather than temporary conceived at the time of writing, or even logical error. Not only can guide the flow chart programming, but also can be used to check the correctness of the program in the debugger. If the block diagram is not correct and the result is a block diagram of progressive inspection program it is very easy to find it wrong. Flow chart can provide to others as part of a program specification, in order to help others understand the structure of your ideas and write programs.

Example: a positive integer greater than or equal to 3, it is determined that it is not a prime number.

The so-called prime number, and refers to the number l except itself, any other integer number can not be divisible. For example, 13 is a prime number, because it can not be 2, 3, ..., 12 divisible.

Determining a number N (N> 3) whether the method of prime numbers is very simple: the N is a dividend, the 2 to (N-1) in turn as each integer divisor divisible if not, then N is a prime number. Algorithm can be expressed as follows:

 

① input value of N.

② I=2。

③ N I was removed.

④ If the remainder is 0, which means I can be divisible by N, N "not prime" is printed, the algorithm ends. Otherwise continue.

⑤ I=I+1。

⑥ If I≤N-l, return to ③. Otherwise, print N "is a prime number." Then ended.

Indeed. N need not be 2 to (N-1) in addition to an integer, is only 2 to N / 2 Room integer division can even just be an integer between 2 to the other. For example, it is judged whether the prime number 13, 13 simply can be 2,3 addition, such as are indivisible, N must be a prime number. ⑥ step can be changed:

⑥: If I≤, return ③. Otherwise the algorithm ends.

Fortran code file [e_212_01.f] [e_212_02.f] .

 

 

a) three basic configurations

Traditional flowchart flow line indicates the order of execution of the blocks, the use of the flow line is not critical. Thus, the user can make cents unrestricted flow freely and turn the flow chart becomes erratic, the reader takes a lot of effort to track the process, make it difficult to understand the logic algorithm. If we write algorithms can restrict the flow of any erratic steering, and like a book that, by the order of chapter sections composed, as it is very easy to read, will not have any difficulties, just order from start to finish can be read on.

In order to improve the quality of the algorithm, the algorithm design and easy to read, it is necessary to limit the abuse of the arrow, which does not allow the flow irregularly turned to chaos, can only proceed sequentially. However, the algorithm will inevitably contain some branching and looping, and not all of a sequence consisting of a box. Or non-sequential transfer backward forward, the process comprising a number of frame sequence is not above the respective embodiments. To solve this problem, it was envisaged that if several provisions of the basic structure and the basic structure of the composition according to certain rules of an algorithm structure, just as with some basic prefabricated units to house barricaded the same, but the structure of the whole algorithm is made on downward, line up the basic structure of each sequence. In 1966, Bohra and Jacoplni proposed the following three basic structure, with these three basic configurations as well algorithms represent a basic unit.

        Sequential structure: as shown within the dashed box, A and B two blocks are executed sequentially. Sequence structure is a basic structure of the simplest.

       
Select Structure: As shown in a dashed box comprises a decision block. According to the given condition is satisfied and choose to perform p A and B. p condition may be "x> 0" or "x> y" and so on. Note that regardless of p conditions are satisfied, can only do one of A or B, not possible to perform both A and B. execution By whatever path, performed after completion of A or B from the selected structure. A or B may have a two boxes empty, i.e., do nothing.

 

       

       
       


Loop structure: also known repeating structures, i.e. one part of the operation is repeatedly performed. There are two types of loop structure:

          When the type (While): when a given condition p holds, perform the operations of blocks A, then p is determined condition is satisfied. If still valid, then execute A box, and so forth until the p condition is not satisfied so far. At this time, the cyclic structure is not performed from the A block.

          Until type (Until): A first implementation of block, and then determine whether a given condition is satisfied p. If p condition is not satisfied, then perform the A, then p-judge conditions. Repeat this until a date specified conditions are met p. At this departing from the present cyclic structure.

       
       
 

 

Note the similarities and differences of the two kinds of the cyclic structure: (1) two kinds of processing operations required loop structure can repeatedly performed. (2) When the cycle type is the "first determination (condition is satisfied), performed after (A block)." Until the loop type is "first execution (A block), the determination (Condition)." (3) performs A box type when the loop is established when a given condition is satisfied, the loop is executed until the A type block at a given condition is not satisfied.

 
   

 

The same problem can be treated with either when circulating, may also be used to process until circulating. The same problem, as respectively determined when the determination condition and the box-type structure until the loop-type handle loop structure, then the structure of both is just the reciprocal condition. And 90/95 do not provide standard Fortran77 do until statement, Compaq Visual Fortran does not provide this extension (but provides some computer systems), until it is necessary to convert into a circulating loop structure when the type: type until a cycle equal to A when coupled with a circulating frame, while a given determination condition "inverted."

b) Structure flowchart

1973 American scholar I.Nassi and B.Shneiderman proposed a new flow chart form. In this flowchart, the process removed completely lines with arrows. All algorithms written in a rectangular box. The box may also contain other subordinate its box to the basic block to form a large box. This flow chart is adapted to structured programming said NS structured flowchart following the flowchart of symbols:

 
   


(1) sequential structure: A and B two blocks composed of a sequence structure.

 
   


(2) selection structure: A User performing condition is satisfied when p, B p is not satisfied operation structure is performed.

       
     
   
 


(3) the cyclic structure: when the type of the cyclic structure, after the first execution icon showing determination condition is satisfied when p A operation is repeatedly performed until the condition is not satisfied until the p.

Until the type of the cyclic structure, after performing the first icon indicates that the determination condition is not established when p A operation is repeatedly performed until the condition is satisfied p.

用以上三种N-S流程图中的基本框.可以组成复杂的N-S流程图,以表示算法。

例:将判别素数的算法用N-S流程图表示。

上面的非结构化流程图不是由三种基本结构组成的:图中间的循环部分有两个出口,不符合基本结构的特点。由于不能直接分解为三种基本结构,应当先作必要的变换再用N-S流程图的三种基本结构的符号来表示。即将第一个菱形框的两个出口汇合在一点。其方法是设一个标志值K,它的初始状态为0(表示N为素数),当K≠0时为非素数。注意当型和直到型的判断条件。

Fortran代码文件为[e_212_03.f90]

N-S图表示算法的优点是:比传统流程图紧凑易画,尤其是它废除了流程线。整个算法结构是由各个基本结构按顺序组成的,其上下顺序就是执行时的顺序。写算法和看算法只需从上到下进行就可以了,十分方便。归纳起来,一个结构化的算法是由一些基本结构顺序组成的;在基本结构之间不存在向前或向后的跳转,流程的转移只存在于一个基本结构范围之内(如循环中流程的跳转);一个非结构化的算法可以用一个等价的结构化算法代替,其功能不变。如果一个算法不能分解为若干个基本结构,则它必然不是一个结构化的算法。

c)    伪代码表示的算法

用传统的流程图和N-S图表示算法直观易懂,但画起来比较费事,在设计一个算法时,可能要反复修改,而修改流程图是比较麻烦的。因此,流程图适宜于表示一个算法,但在设计算法过程中使用不是很理想的(尤其是当算法比较复杂、需要反复修改时)。为了设计算法时方便,常用一种称为伪代码的工具。伪代码是用介于自然语言和计算机语言之间的文字和符号来描述算法。它如同一篇文章一样,自上而下地写下来。每一行(或几行)表示一个基本操作。它不用图形符号,因此书写方便、格式紧凑,易懂也便于向计算机语言算法(即程序)过渡。

可以用英文、汉字、中英文混合表示算法,以便于书写和阅读为原则。用伪代码写算法并无固定的、严格的语法规则,只要把意思表达清楚,并且书写的格式要写成清晰易读的形式。例如,对于电子在特殊几何构型材料中的散射问题:[sphere-1.doc][sphere-2.doc][sphere.f]

Guess you like

Origin www.cnblogs.com/GoldCrop/p/11297105.html