# C programming (Fifth Edition) written by Tan Haoqiang Chapter II answers and explanations (including the original code)

Posted by Ohio Guy on Tue, 19 Oct 2021 20:54:21 +0200

Chapter 2 algorithm - the soul of program

#### First question

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

Algorithm: algorithm is the method and specific steps to solve a problem.

Example: 1. CET-4: accumulate words first, then brush questions, and go to the examination room.

2. Making dumplings: first prepare the stuffing and noodles, then roll the dumpling skin and make dumplings.

3. The following: Boil the water first, and then put the noodles into the boiling water.

#### Second question

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

(1) The algorithm structure composed of sequence, selection, cycle and other basic structures is a structured algorithm.

(2) The structured algorithm has high readability and is not prone to errors, so that the quality of the algorithm can be guaranteed and improved. Therefore, the structured algorithm should be advocated.

#### Question 3

3. Try to describe the characteristics of the three basic structures. Please design two other basic structures (in accordance with the characteristics of the base class structure).

(1) Sequential structure: sequential structure is a linear and orderly structure, which executes various statement modules in turn.

(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 basic structure shall meet the following points:

1. There is only one entrance
2. There is only one exit
3. Every part of the structure has the opportunity to implement
4. There is no dead cycle in the structure
Therefore, the following structure is given: #### Question 4

4. The traditional flow chart is used to represent the algorithm for solving the following problems.

(1) There are two bottles A and B, which contain vinegar and soy sauce respectively. It is required to exchange them (that is, bottle A used to hold vinegar, but now holds soy sauce, and bottle B is the opposite).

Idea: two bottles, in order to exchange content, we must use a new bottle as the medium.

Therefore, the flow chart is as follows: The code is as follows:

```#include<stdio.h>
#include<string.h>
int main()
{
char a="cu";//Vinegar
char b="jiangyou";//soy sauce
char c={0};
strcpy(c,a);
strcpy(a,b);
strcpy(b,c);
printf("%s %s\n",a,b);
return 0;
}
```

(2) Input 10 numbers in turn and output the largest number.

Idea: assign the first number to m, cycle for 9 times, and compare it with the following numbers in turn. If it is less than the following value, replace the value of m with this value. After the cycle ends, the maximum value is m, that is, the largest number among the 10 numbers.

Therefore, the flow chart is as follows: The code is as follows:

```#include<stdio.h>
int main()
{
int arr={0};
for(int i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
int m=arr;
for(int i=1;i<10;i++)
{
if(m<arr[i])
{
m=arr[i];
}
}
printf()
return 0;
}
```

(3) There are three numbers a, B and C. It is required to output them in size order.

Idea: save the three entered numbers, a, b and c. compare a and b first. If a is greater than b, the values of a and b are interchanged. A and c are compared. If a is greater than c, the values of a and c are interchanged. Compare b and c. if b is greater than the value of c, the values of b and c are interchanged. The result is that a, b and c are sorted from small to large.

Therefore, the flow chart is as follows: The code is as follows:

```#include<stdio.h>
int main()
{
int a,b,c,tmp;
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
tmp=a;
a=b;
b=tmp;
}
if(a>c)
{
tmp=a;
a=c;
c=tmp;
}
if(b>c)
{
tmp=b;
b=c;
c=tmp;
}
printf("a=%d b=%d c=%d\n",a,b,c);
return 0;
}
```

(4) Find 1 + 2 + 3 +... + 100.

Idea: This is an accumulation. From 1 to 100, only one cycle is needed.

The flow chart is as follows: The code is as follows:

```#include<stdio.h>
int main()
{
int sum=0;
for(int i=1;i<=100;i++)
{
sum+=i;
}
printf("%d\n",sum);
retrun 0;
}
```

(5) Judge whether a number n can be divided by 3 and 5 at the same time.

Idea: enter an n, first judge whether it can be divided by 3, if not, it means that the conditions are not met. If it can, then judge whether it can be divided by 5, if not, it does not meet the conditions. If it can, it means that this number can be divided by 3 and 5 at the same time.

The flow chart is as follows: The code is as follows:

```int main()
{
int n=0;
scanf("%d",&n);
if(n%3==0)
{
if(n%5==0)
printf("Meet the conditions\n");
else
printf("Conditions not met\n");
}
else
printf("Conditions not met\n");
retrun 0;
}
```

(6) Output prime numbers between 100 and 200

Idea: prime numbers are not divisible by other numbers except themselves and 1. Therefore, you only need to remove the number of 100 ~ 200 to be less than its number (excluding 1), and then judge whether it can be divided.

The flow chart is as follows: ```#include<stdio.h>
int Panduansushu(int n)
{
for(int i=2;i<b;i++)
{
if(n%i==0)
return (-1);
}
return 1;
}
int main()
{
for(int i=100;i<=200;i++)
{
int ret=Panduansushu(i);
if(i!=(-1))
printf("%d\n",i);
}
return 0;
}
```

(7) Find the greatest common divisor of two numbers m and n

Idea: the greatest common divisor of two numbers can be divided by rolling.

The flow chart is as follows: The code is as follows:

```int main()
{
int n,m;
scanf("%d%d",&n,&m);
if(m>n)
{
int tmp=m;
m=n;
n=tmp;
}
for(int i=n;i>0;i--)
{
if((m%i==0)&&(n%i==0))
printf("%d\n",i);
}
}
```

(8) Find the solution of the equation:
a x 2 + b x + c = 0 ; ax^2+bx+c=0; ax2+bx+c=0;
Idea: This is a junior high school math problem.

The flow chart is as follows: The code is as follows:

```#include<stdio.h>
#include<math.h>
int main()
{
float a, b; float x1, x2; float c, t;
while ((scanf("%f %f %f\n", &a, &b, &c)) != EOF)//Multiple sets of input coefficients a, b, c
{
if (a == 0)
else
{
t = (b*b) - 4 * a*c;//t is used to judge the root (delta ~)
if (t>0)//Two roots
{
//The root formula sqrt (t) is square
x1 = (-b - sqrt(t)) / (2 * a);
x2 = (-b + sqrt(t)) / (2 * a);
printf("x1=%.2f;x2=%.2f\n", x1, x2);

}
else if (t<0)//This is a complex root
{
x1 = -b / (2 * a);
x2 = sqrt(-t) / (2 * a);
printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", x1, x2, x1, x2);

}
else //Unique root
{
x1 = (-b) / (2 * a);
printf("x1=x2=%.2f\n", x1);
}

}
}
return 0;
}
```

#### Question 7 