C Programming Fifth Edition Tan Haoqiang After-Class Answers Chapter 2 Answers

Chapter 2: Algorithms - the soul of programs

1. What is an algorithm? Try to find 3 examples from daily life and describe their algorithms

Algorithm: In short, it is the steps to solve a problem, a description of the steps to solve a specific problem.

For example, examples in life:

  1. college entrance examination

    First of all, fill in the volunteer form, pay the registration fee, get the admission ticket, take the test on time, receive the admission notice, and report to the designated school according to the date.

  2. Go to a concert in Beijing

    First buy tickets online, then take the bus to Beijing on time, and then take the bus to the concert venue.

  3. put the elephant in the fridge

    First open the refrigerator door, then put the elephant in the refrigerator and close the refrigerator.

2. What is a structured algorithm? Why advocate structured algorithms?

  • Structured algorithm: It is composed of some basic structures such as sequence, selection, and cycle in sequence, and the transfer of the process only exists within a basic range.

  • The structured algorithm is easy to write, has high readability, and is easy to modify and maintain, which can reduce the chance of program errors, improve the reliability of the program, and ensure the quality of the program. Therefore, structured algorithms are advocated.

3. Describe the characteristics of the three basic structures, please design two additional basic structures (to conform to the characteristics of the base class structure).

The structured programming method mainly consists of the following three basic structures:

  1. Sequence structure: Sequence structure is a linear and orderly structure, which executes each statement module in sequence
  2. Selection structure: The selection structure is to select the path of program execution according to whether the condition is true or not.
  3. Loop structure: Loop structure is to repeatedly execute one or several modules until a certain condition is met

The redesign of the basic structure should meet the following points:

  1. only one entrance
  2. only one exit
  3. Every part of the structure has the opportunity to perform to
  4. There is no infinite loop in the structure

Therefore, the following review structure is given: while type and until type loop compound and multiple choice structure

C language programming fifth edition Tan Haoqiang third question answer

4. Express the algorithm for solving the following problem using a traditional flowchart.

  1. There are two bottles A and B, which hold vinegar and soy sauce respectively, and they are required to be interchanged (that is, bottle A used to hold vinegar, but now holds soy sauce, and bottle B does the opposite).

    Parse:

    It is obviously difficult to achieve with two bottles, you can use an empty bottle C as a transfer, first import the vinegar in A into C, then import the soy sauce in B into A, and finally import the vinegar in C into B to realize the exchange .

    C language programming fifth edition Tan Haoqiang after-school answers to the third question answer

  2. Enter 10 numbers in turn, and ask to output the largest number among them.

    Parse:

    Input 10 integers first, give the first integer to max, and then compare the remaining integers with max in turn. If an integer is greater than max, give the integer to max until all the remaining integers are compared. It is the largest integer, and the max value is output.

insert image description here

  1. There are 3 numbers a, b, c, and they are required to be output in order of size.

    Parse:

    i: first compare a and b, if a is greater than b, exchange the contents of a and b, otherwise proceed to ii

    ii: compare c with a, if c is greater than a, exchange a and c, otherwise perform iv

    iii: Compare c and b, if c is greater than b, exchange c and b, otherwise perform iv

    iv: output a, b, c, end

C language programming fifth edition Tan Haoqiang after-school answers to question 3 answers

  1. Find 1 + 2 + 3 + ... + 100.

    Parse:

    Given that N is 1 and sum is 0, if N is less than or equal to 100, perform sum += N until N exceeds 100. After the loop operation is completed, sum is the result of adding from 1 to 100.

    C language programming fifth edition Tan Haoqiang after-school answers to question 4 answers

  2. Determine whether a number n is divisible by both 3 and 5.

    Parse:

    i: input data n

    ii: If n can be integered by 3, proceed to iii, otherwise output n cannot be integered by 3 and 5

    iii: If n can be integered by 5, output n can be integered by 3 and 5, otherwise n cannot be integered by 3 and 5

C language programming fifth edition Tan Haoqiang after-school answers to question 5 answers

  1. Output prime numbers between 100 and 200

    Prime number: That is, a prime number in mathematics. A number whose factors are only 1 and itself is called a prime number.

    Do the following for each number between 100 and 200:

    Whether the number is divisible by all numbers between 2 and the number, if yes, it is a prime number output, otherwise take the next number.

Output prime numbers between 100 and 200

  1. Find the greatest common divisor of two numbers m and n

    Analysis: rolling and dividing method

    a. If m is greater than n, exchange m and n

    b. Do the following operations in a loop:

    ​ Is n equal to 0? If yes, the greatest common divisor is m, output m to end.

    ​ Otherwise: use the result of m%n to r, give the value of n to m, and give the value of r to n

    Find the greatest common divisor of two numbers m and n

  2. Find the equation ax 2 + bx + c = 0 ax^2 + bx + c = 0ax2+bx+c=root of 0 . Consider separately:

    • has two unequal real roots;
    • has two equal real roots;

    Parse:

    If b 2 − 4 ac > 0 b^2 - 4ac > 0b24 BC _>0 , the equation has two unequal real roots:x = − b ± b 2 − 4 ac 2 ax=\frac{-b\pm\sqrt{b^2-4ac}}{2a}x=2a _b±b24ac

    If b 2 − 4 ac = 0 b^2 - 4ac = 0b24 BC _=0 then the equation has a real root:x = − b 2 ax=\frac{-b}{2a}x=2a _b

    If b 2 − 4ac < 0 b^2 - 4ac < 0b24 BC _<0 , the equation has no real roots.

    C language programming fifth edition Tan Haoqiang answers after class

5. Use the NS diagram to represent the algorithm of each question in question 4

  1. There are two bottles A and B, which hold vinegar and soy sauce respectively, and they are required to be interchanged (that is, bottle A used to hold vinegar, but now holds soy sauce, and bottle B does the opposite).

    C language programming fifth edition Tan Haoqiang fifth question answer after class

  2. Enter 10 numbers in turn, and ask to output the largest number among them.

    Answers to Tan Haoqiang's after-school exercises

  3. There are 3 numbers a, b, c, and they are required to be output in order of size.

C Language Programming Fifth Edition Tan Haoqiang Exercises Answers

  1. Find 1 + 2 + 3 + ... + 100.

Find 1 + 2 + 3 + ... + 100

  1. Determine whether a number n is divisible by both 3 and 5.

C language programming fifth edition after-school exercises answers

  1. Find the greatest common divisor of two numbers m and n

C language programming fifth edition Tan Haoqiang answer question six

  1. Find the equation ax 2 + bx + c = 0 ax^2 + bx + c = 0ax2+bx+c=root of 0 . Consider separately:

    • has two unequal real roots;
    • has two equal real roots;

    C Language Programming Fifth Edition Tan Haoqiang Answers

6. Use pseudocode to express the algorithm of each question in question 4

  1. There are two bottles A and B, which hold vinegar and soy sauce respectively, and they are required to be interchanged (that is, bottle A used to hold vinegar, but now holds soy sauce, and bottle B does the opposite).

    begin
        醋 => A
        酱油 => B
        A => C
        B => A
        C => B
    end
    
  2. Enter 10 numbers in turn, and ask to output the largest number among them.

    begin
        1 => i
        0 => max
        while i < 10
        {
          
          
            输入一个整数data
            if data > max
            {
          
          
                data => max
            }
        }
    
        print max
    end
    
  3. There are 3 numbers a, b, c, and they are required to be output in order of size.

    begin
        input a
        input b
        input c
        
        if a > b
        {
          
          
            a => t
            b => a
            t => b
        }
        
        if c > a
        {
          
          
            c => t
            a => c
            t => a
        }
        
        if c > b
        {
          
          
            c => t
            b => c
            t => b
        }
        
        print a
        print b
        print c
    end
    
  4. Find 1 + 2 + 3 + ... + 100.

    begin
        1 => i
        0 => sum
        while i <= 100
        {
          
          
            sum + i => sum
            i + 1 => i
        }
    
        print sum
    end
    
  5. Determine whether a number n is divisible by both 3 and 5.

    begin
        input n
        if n % 3 == 0
        {
          
          
            if n % 5 == 0
            {
          
          
                print n能被35整除
            }
            else
            {
          
          
                print n不能被35整除
            }
        }
        else
        {
          
          
                print n不能被35整除
        }
    end
    
  6. Find the greatest common divisor of two numbers m and n

    begin
        input m
        input n
        
        if m > n
        {
          
          
            m => t
            n => m
            t => n
        }
        
        while n != 0
        {
          
          
            m % n => r
            m => n
            r => n
        }
        
        print m
    end
    
  7. Find the equation ax 2 + bx + c = 0 ax^2 + bx + c = 0ax2+bx+c=root of 0 . Consider separately:

    • has two unequal real roots;
    • has two equal real roots;
    begin
        input a
        input b
        input c
        
        b*b - 4*a*c => p
        if p < 0
        {
          
          
            print 方程没有实根
        }
        
        if p == 0
        {
          
          
            print 方程有一个实根 -b/2a
        }
        
        if p > 0
        {
          
          
            print 方程有两个实根:
            print x1 = {
          
          -b + sqrt(b^2 - 4ac)}/2a
            print x1 = {
          
          -b - sqrt(b^2 - 4ac)}/2a
        }
    end
    

7. What is structured programming? What is its main content?

Structured programming (referred to as SP) is the basic principle of detailed design based on module function and process design . Its concept was first proposed by EW Dijikstra in 1965. The idea of ​​structured program design does improve the efficiency of program execution, which is an important milestone in software development. Its main point of view is to adopt a top-down, step-by-step program design method ; each module passes "sequence, selection, cycle" The control structure is connected, and there is only one entry and one exit.

8. Use the top-down and step-by-step refinement method to design the following algorithms:

  1. Output 1900-2000 is a soft year, and the year that meets one of the following two conditions is a leap year:

    • Divisible by 4 but not by 100
    • Divisible by 100 and divisible by 400.
    算法大体流程
    1. 循环取19002000中的每一个年份
    2. 对于每一个年份判断其是否是闰年
    3. 是闰年则输出
    
    判断一年是否是闰年:
    1. 如果该年份内被4整除但是不能被100整除是闰年,否则不是闰年
    2. 如果年份能被400整除则是闰年,否则不是闰年
    
  2. Find ax 2 + bx + c = 0 ax^2 + bx + c = 0ax2+bx+c=root of 0 . Consider separatelyd = b 2 − 4 acd = b^2 - 4acd=b24 There are three situations where ac is greater than 0, equal to 0 and less than 0.

    1. 获取a b c的值
    2. 计算b^2 - 4ac的结果并给p
    3. 如果p < 0, 则方程没有实根
    4. 如果p == 0,则方程有一个实根-b/2a
    5. 如果p > 0, 则方程有两个实根 x1 = {
          
          -b + sqrt(b^2 - 4ac)}/2a x2 = {
          
          -b - sqrt(b^2 - 4ac)}/2a
    
  3. Input 10 numbers and output the largest one.

    1. 给一个max保存最大值
    2. 分别输入10个数,并对用每个数与max进行比较
       如果该数大于max,则将该数给max
    3. 输出max
    

Guess you like

Origin blog.csdn.net/gjggj/article/details/106982646