Flowchart and Algorithm

Source: Internet
Author: User
What is a program? Program = Data Structure + algorithm.

For object-oriented programming, the emphasis is on the data structure. For process-oriented programming languages such as C, p a s c a l, f o rt r a n, and so on, the main concern is algorithms. Mastering algorithms is also a solid foundation for object-oriented programming. So what is an algorithm?

People use computers to deal with different problems. To do so, people must analyze various problems in advance and determine the specific methods and steps to solve the problems, compile a group of commands that allow computers to execute, namely programs, and hand them over to computers so that computers can work effectively according to the steps specified by people. These specific methods and steps are actually an algorithm for solving a problem. According to an algorithm, writing a sequence of commands executed by a computer based on certain rules means programming, and the rule to be followed during writing is the syntax of a language.

It can be seen that one of the key points of programming is the method and step of solving the problem, and the algorithm. The focus of learning advanced languages is to master the methods for analyzing and solving problems, that is, to exercise analysis and decomposition, and ultimately summarize the capabilities of algorithms. Correspondingly, a specific language, such as the C language syntax, is a tool and a specific implementation of an algorithm. Therefore, in advanced language learning, on the one hand, you should be familiar with the syntax of the language because it is the basis for algorithm implementation. On the other hand, you must recognize the importance of algorithms and strengthen thinking training, to write high-quality programs.

The following example shows how to design an algorithm:

[Example 1-4] input three numbers and then output the maximum number.

First, we have to install these three numbers first. We define three variables A, B, and C, and input these three numbers to A, B, and C in sequence. In addition, prepare a maximum number of m a x packages. Since the computer can only compare two numbers at a time, we first put the ratio of A to B into M A X, and then compare M A X to C, put a large number into m a x. Finally, output m a x. At this time, m a X is loaded with the largest number of A, B, and C. Algorithm table

As shown below:

1) enter A, B, and C.

2) one of the largest values of A and B is placed in M A X.

3) place one of C and m a x in m a x.

4) output m a x, m a X is the maximum number.

Steps 2) and 3 are still unclear, and cannot be directly converted to program statements. You can continue to refine the process:

2) place one of the largest values of A and B in m a x. If A> B, Max between a; otherwise, Max between B.

3) place one of C and m a x in m a x. If C> m a x, m a X contains C.

The algorithm can be written as follows:

1) Input A, B, and C.

2) If A> B, Max compute;

Otherwise, m a x branch B is used.

3) If C> m a x, m a x 1_c.

4) output m a x, m a X is the maximum number.
This algorithm can be easily converted into corresponding program statements.

[Example 1-5] Monkey peach eating problem: there are a pile of peaches I don't know the number, the monkey eats half on the first day, I think it is not good, and I eat one more, the next day according to this, add another half of the remaining peaches every day. On the morning of the tenth day, the monkey found that there was only one peach left. How many peaches were there?

This question seems a little difficult to start. How can we start? Suppose there are A1 peaches at the beginning of the first day, and A2 peaches at the second day ,..., there are A9 in 9th days, And a1 0 in 1st. In A1, A2 ,..., in a1 0, only a1 0 = 1 is known. Now A1 is required. We can see that A1, A2 ,.., there is a simple relationship between a1 0:

A9 = 2*(a1 0 + 1)

A8 = 2*(A9 + 1)

Bytes

A1 = 2*(A2 + 1)

That is: AI = 2 * (AI + 1 + 1) I = 9, 8, 7, 6,..., 1

This is the mathematical model of this question.

The above calculation process from A9, A8 to A1 is actually a recursive process, which is often used in computer problems. On the other hand, these nine-step operations are in the same form. The difference is only the subscript of AI. As a result, we introduce a cyclical approach and use A0 to represent the number of peaches in the previous day, and A1 to represent the number of peaches in the next day. The algorithm is rewritten as follows:

1) a1 = 1; {1st peach count in 0 days, initial value of A1}

I = 9. {Counter initial value is 9}

2) A0 = 2*(A1 + 1 ). {Calculate the number of peaches for the day}

3) a1 = A0. {Use the peach count of the day as the initial value for the next calculation}

4) I = I-1.

5) if I> = 1, go to 2 ).

6) output the A0 value.

2 )~ 5) steps are loops.

 

This is a process from concrete to abstract. The specific method is as follows:

1) Find out what steps should be taken if they are done by people.

2) summarize these steps and abstract the mathematical model.

3) repeat the steps to obtain a unified form by using the same variables and so on, and then use the loop solution in a concise manner.

Algorithm Description methods include natural language description, pseudocode, flowchart, n-s diagram, and PA d diagram.

1.4.1 Structured description of flowcharts and Algorithms

1. Flowchart

A flow chart is a traditional algorithm representation. It uses boxes in geometric graphs to represent operations of different properties and uses flow lines to indicate the direction of Algorithm Execution. Because it is simple and intuitive, it is widely used, especially in the early language stage. Only through flowcharts can we express algorithms concisely. Flowcharts have become an important means for programmers to communicate, the dependency on flowcharts is reduced only when the structured programming language appears.

The following describes common flowchart symbols and flowchart examples.
The flowchart 1-2 of the algorithm in Example 1-1 is shown in this chapter. The flowchart 1-3 of the algorithm in Example 1-2 is shown in this chapter. In the flowchart, the process line on the left of the judgment box indicates the process when the judgment condition is true, and the process line on the right indicates the process when the condition is false, sometimes "true", "false", "T", "F", "Y", and "N" are marked on the top of the left and right flow lines ".



It is also stipulated that when the flow line is from bottom up or from right to left, an arrow must be included. In addition, no arrow is drawn, and the flow line is always from top down or from left to right.

2. Structured description of the Algorithm

In earlier non-structured languages, g o t o statements allow programs to jump directly from one place to another. The advantage of doing so is that the program design is very convenient and flexible, reducing the complexity of the Manual, but its disadvantage is also very prominent. A large number of Jump statements make the process of the program very complex and disordered, it is difficult to understand and verify the correctness of the program. If there is a mistake, it is even more difficult to troubleshoot it. The chaos and complexity of this transfer flow chart is a vivid illustration of the program personnel situation in the software crisis. The structured program design is to clarify this mess.

After research, we found that any complex algorithm can be composed of three basic structures: sequential structure, selection (Branch) structure, and cyclic structure. Therefore, when we construct an algorithm, these three basic structures are also used as "building units" and comply with the specifications of the three basic structures. The basic structures can be parallel and can contain each other, but cannot be crossed, direct transfer from one structure to another is not allowed. Because the entire algorithm is composed of three basic structures, just like the one built by a module, the structure is clear, correctness verification is easy, and error correction is easy. This method is a structured method. The program design that follows this method is structured program design.

Correspondingly, as long as the flowchart of the three basic structures is defined, the flowchart of any algorithm can be drawn.

(1) Ordered Structure

The ordered structure is a simple linear structure. The boxes are executed in order. The basic form of the flowchart is 1-4. The statement execution sequence is: A → B → C.

(2) Select (Branch) Structure

This structure is used to determine a given condition. When the condition is true or false, the content of different boxes is executed. There are two basic shapes: 1-5 a) and B. Figure 1-5 a) the execution sequence is: execute a when the condition is true; otherwise execute B; Fig 1-5 B) the execution sequence is: execute a when the condition is true, otherwise, do nothing.

1. (3) There are two basic types of cyclic structures: w h I l e type and d o-w h I L E type.
2. A. While loop 1-6 is shown. The execution sequence is as follows: when the condition is true, A is repeatedly executed. Once the condition is false, the loop jumps out and the statement after the loop is tight is executed.

B. Do-while loop 1-7 is shown.

The execution sequence is: execute a first, and then judge the condition. When the condition is true, A is always executed cyclically. Once the condition is false, the loop ends and the next statement is executed after the loop is tight.

In Figure 1-6 and Figure 1-7, A is called a cyclic body and a condition is called a cyclic control condition. Note that:

1) in the loop body, the value to be judged by the condition must be modified, so that after a finite loop, the loop will surely end, And I = I-1 in 1-3. 2) When the loop body in a Type Loop may not be executed at once, and until the Type Loop is executed at least once. 3) the type-to-type cycle can be easily converted into a type-to-type cycle, while the type-to-type cycle may not be converted into a type-to-type cycle.

For example, Figure 1-7 can be converted to Figure 1-8.

2 The algorithm of describing with N-S Diagram

The N-s graph is another algorithm representation of the US. n a s I and B. s h n e I d e r m a n is proposed by the following statement: since any algorithm is composed of the three structures described above, therefore, the flow line between the basic structures is redundant. Therefore, the n-s graph is also a Structured description method of the algorithm.

In the n-s diagram, an algorithm is a large rectangular box that contains several basic boxes. The N-S diagram of the three basic structures is described as follows:

1. The sequence structure is 1-9. The execution sequence is a and B.
2. Select Structure
The N-S Diagram corresponding to Figure 1-5 is 1-1 0. Figure 1-10 a) execute a when the condition is true and B when the condition is false. Figure 1-1 0 B) if the condition is true, execute a. If the condition is false, do nothing.


3. Loop Structure
1) as shown in Figure 1-11 of the while loop, when the condition is true, the loop body a is cyclically executed until the condition is false.
2) In the n-s diagram of the do-while loop 1-1 2, the loop body a is executed cyclically until the condition is false.
In this chapter, the N-S Diagram 1-1 3 in 1-1, and the N-S Diagram 1-1 4 in 1-2. The N-S diagram is more intuitive and concise than the flowchart.



3. Use pad graphs to describe the algorithm
Problem Analysis divisor (PA d) is a graphical representation of algorithms widely used in software development in recent years. Compared with the preceding flowchart and n-s diagram, the flowchart and the n-s diagram are both top-down and sequential descriptions. In addition to top-down, the PA d diagram also expands from left to right. Therefore, if the flowchart and the n-s diagram are one-dimensional algorithm descriptions, the PA d diagram is two-dimensional, which can display the layered structure of the algorithm and be more intuitive and easy to understand.
Below are several basic forms of the PA d diagram:
1. Sequence Structure:
1-1 5.
2. Select Structure
(1) Select a single branch. If the condition is true, execute a, 1-16 ).
(2) select 1-16 B for the two branches. If the condition is true, execute a. If the condition is false, execute B.
(3) multi-branch selection, 1-16 C). When I = I1, execute a, I = I2, execute B, I = I3, and execute C, run D when I = I4.

3. Loop Structure
1-1 7. Figure 1-17 A) is a w h I l e loop, and Figure 1-17 B) is a d o-w h I l e loop.

In this chapter, the PA d Diagram 1-1 8 in Example 1. 1, the PA d Diagram 1-1 9 in Example 1-2.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.