C language - study notes 2

Table of contents

 2.1 The concept of algorithm

2.2 Representation method of algorithm

1. Natural language representation algorithm

2. Traditional flowchart representation algorithm

3. Use the NS structured block diagram to represent the algorithm

 2.3 Characteristics of the algorithm

1. Feasibility

2. Finiteness

3. Certainty

4. Effectiveness

5. Have enough information

6. With or without input

7. At least one output

2.4 Requirements for Algorithm Design

1. Correctness

2. Readability

3. Robustness

4. High efficiency and low storage requirements

2.5 Basic methods of computer programming


 2.1 The concept of algorithm

The methods and steps taken to solve a problem are called algorithms. There are different algorithms for different problems, and there can be many different algorithms for the same problem.

2.2 Representation method of algorithm

1. Natural language representation algorithm

Natural language is the language that people use every day, and algorithms are described in natural language, which is easy to read.

[Example 1] Input three numbers arbitrarily, and sort the output in ascending order.

S1: Enter a, b, c.

S2: If a>b, exchange the contents of a and b, otherwise the contents of a and b remain unchanged.

S3: If a>c, exchange the contents of a and c, otherwise the contents of a and c remain unchanged.

S4: If b>c, exchange the contents of b and c, otherwise, the contents of b and c remain unchanged.

S5: Output a, b, c in the order of a, b, c.

S6: End.

The natural language representation algorithm is easy to read, but the text is cumbersome, and sometimes the representation is not rigorous, which is prone to ambiguity. Especially for complex programs, it is not clear enough to express in natural language. Therefore, complex algorithms are generally not expressed in natural language.

2. Traditional flowchart representation algorithm

The traditional flow chart uses some picture frames and text descriptions to represent the various operation steps of the algorithm, and uses arrows to represent the direction of program execution, which is intuitive, vivid and easy to understand. Common block diagram symbols are shown in the figure.

 【Example 2】Enter a year number and judge whether the output year is a leap year.

 

 The advantage of this kind of flowchart representation algorithm is that it is intuitive and the process is clear. The disadvantage is that it occupies a large area, and because the process line is allowed to be used, the process can be transferred arbitrarily, which makes it difficult for people to understand the idea of ​​​​the program for complex programs.

3. Use the NS structured block diagram to represent the algorithm

Features: The process line is canceled, and the process is not allowed to be transferred arbitrarily, and can only be carried out sequentially from top to bottom.

 2.3 Characteristics of the algorithm

1. Feasibility _

Algorithms designed for practical problems should be able to obtain satisfactory results after execution.

2. Finiteness _

An algorithm should complete in finite time, i.e. the algorithm should terminate after performing a finite number of steps of operations.

3. Certainty _

Every step in the algorithm should have a clear description, and there should be no ambiguous interpretation and ambiguity.

4. Effectiveness _

Each step in the algorithm should be executed efficiently.

5. Have enough information

For an algorithm to be effective, it must be provided with sufficient information.

6. With or without input

An algorithm can have one or more algorithms, or no inputs.

7. At least one output

An algorithm with no output is meaningless.

2.4 Requirements for Algorithm Design

1. Correctness

The correctness of the algorithm means that the algorithm should at least have input, output and processing without ambiguity, can correctly respond to the needs of the question, and can get the correct answer to the question.

2. Readability

Algorithms are mainly for the convenience of human reading and communication, followed by execution.

3. Robustness

When the input data is illegal, the algorithm can also deal with it instead of producing abnormal or inexplicable results.

4. High efficiency and low storage requirements

When designing an algorithm, try to meet the requirements of high time efficiency and low storage capacity.

2.5 Basic methods of computer programming

Guess you like

Origin blog.csdn.net/m0_66411584/article/details/122756779