C programming - Chapter 2 - learning records of the first day

Posted by JennyG on Sat, 22 Jan 2022 16:45:26 +0100

    A 30-and-a-half-year-old man, record his study records. Half of life has passed, but there is nothing. Life has no value and pursuit, and the body is not healthy. Start the first day of life and try again. I hope I can stick to it.
    2021 June 20th. Tan Haoqiang Fourth Edition c Language Programming Chapter II learning records.

Then, July 4th. It's too slow to finish a chapter.
Please correct the mistakes one by one. Thank you so much. kiss you.

exercises

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

Description of the operation. The steps that require the computer to operate, that is, the algorithm. Broadly speaking, the methods and steps of colored jade to solve a problem become algorithms.
Example 1: for a meeting from Beijing to Tianjin, 1 - buy a train ticket first, 2 - take the subway to Beijing station, 3 - get on the train, 4 - get off at Tianjin station, 5 - transfer to the venue, 6 - attend the meeting.
Example 2: buy a TV. 1 - pick the goods, 2 - give the money, 3 - hold the TV, 4 - go home
Example 3: college entrance examination. 1 - prepare for the college entrance examination. 2 - fill in the integrity test form. 3 go to the examination room. 4 - take the college entrance examination. 5 the exam is over. 6 - fill in the registration form. 7 - letter of acceptance. 8 - go to college.

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

key:
The algorithm composed of the basic structure (sequence, selection and cycle structure) is a structured algorithm. It does not have irregular steering. Only in this basic structure can there be branches and forward and backward jumps.
It is composed of some basic structures such as sequence, selection and circulation, and the process is transferred to the basic scope of existence.
Structured algorithm is easy to write, high readability, simple to modify and maintain, which can reduce the chance of program error, improve the reliability of the program and ensure the quality of the program. Therefore, structured algorithm is advocated.

    3.Try to describe the characteristics of three basic structures. Please design two other basic structures to meet the characteristics of the basic structure.

The three basic structures are sequence structure, selection structure and loop structure (when the while loop structure and until loop structure). Sequential structure is a linear and ordered structure, which executes each statement module in turn. Selection structure: the selection structure is to select the path of program execution according to whether the condition is true or not. Loop structure is to repeatedly execute one or several modules until a certain condition is met.
The basic design conditions and structure shall meet the following points:
There is only one entrance; There is only one exit; Every part of the structure has the opportunity to be implemented; There is no dead cycle in the structure.

4. Use the traditional flow chart to represent the algorithm for solving the following problems.
(1) There are two bottles A and B, which contain vinegar and soy sauce respectively. They are required to be exchanged. That is, bottle A used to hold soy sauce, but now put vinegar. Bottle B vice versa. No,

(2) Input 10 numbers in turn and output the largest number
key:
Assign the first number input to max, and the numbers input in turn are compared with max. If it is greater than max, the value is assigned to max, and if it is less than max, it remains unchanged. Finally, output max.

(3) There are three numbers a, B and C. they are required to be output in size order.

(4) Find 1 + 2 + 3 +... + 100.
(5) Judge whether a number n can be divided by 3 and 5 at the same time.

(6) Output prime numbers between 100 and 200.

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

Rolling phase division

a. If n is greater than m, swap m and n
b. Cycle through the following operations:
m%n is 0? If yes, the maximum common divisor is m and the output m ends.
Otherwise, the result of m%n is given to r, the value of n is given to m, the value of r is given to N, and n==0 is returned
Divide the larger number by the smaller number, remove the divisor with the remainder (the first remainder), and remove the first remainder with the remainder (the second remainder). Repeat until the last remainder is 0. If it is to find the greatest common divisor of two numbers, the last divisor is the greatest common divisor of the two numbers.

Correct the wrong picture. You should enter m and N. if n > m, exchange the values of N and m.


(8) Find the root of equation ax^2+bx+c=0. Consider separately:
There are two unequal real roots and two equal real roots.

5. Use NS diagram to represent the algorithm of each question in question 4.
key:
Since I have never used NS diagram, and it is clear that I will not use it in future articles, I will not draw NS here. If I draw in the future, I will accept my slap in the face.

    6.Use pseudo code to represent the algorithm of each question in question 4.
    Question 1:
begin
  a=Vinegar
  b=soy sauce
  a=c
  a=b
  b=c
end

Question 2:

begin
	int a,b,c
	b=0
	c=0
	while b<=10
		{
		Enter integer a
		if a>c
			{
			c=a}
		b+=1
		}
	print c
end

Question 3:

begin
	input a,b,c
	if a<b
	{
	t=a
	a=b
	b=t
    }
    if a<c
    {
    t=a
    a=c
    c=t
    }
    if b<c
    {
    t=b
    b=c
    c=t
	}
	print a,b,c
end
	

Question 4

begin 
	i=1
	n=1
	sum=0
	while(n<=1000)
	{
	sum=sum+i
	}
	print sum
end
	

Question 5

begin
	input n
	if (n%3==0)
	{
	    if(n%5==0)
	    {
	        print("n Can be divided by 3 and 5")
	     }
	     else
	     print("n Cannot be divided by 3 and 5")
	 }
	 else
     print("n Cannot be divided by 3 and 5")

Question 6

begin
	t=100
	n=2
	if(t=100;t++;t<=200)
	{
		while(n<t)
		{
		r=t%n
		if(r==0)
			{
				n=t
				pirnt("t Not prime")
			}
			n+=1
		}
	}
		

Question 7

begin 
	if (n>m)
	{
		a=m
		m=n
		n=a
	}
	if(n==0)
	print("n Is the greatest common divisor")
    else
    {
    	r=m%n
		while(r!=0)
		{
		   m=n
		   n=r
		   r=m%n
		}
		print("n Is the greatest common divisor")
	}
end	

Question 8

begin
	input a,b,c
	n=b*b-4*a*c
	m=b*b+4*a*c
	if(n>0)
	{
		print("x1=(-n)/(2a)")
		print("x2=(-m)/(2a)")
	}
	if(n=0)
	{
		pirnt("x1=x2=(-n)/(2q)")
	}
	if(n<0)
	{
	print("No real root")
	}
end
    7.What is structured programming? What is its main content?
    key:

Structured design: it is the basic principle of detailed design focusing on module function and processing process design. The idea of structured programming improves the efficiency of program execution, which is an important milestone in software development. The main content is to solve a complex problem in stages, and the problems dealt with in each stage are controlled within the range that people can easily understand and deal with. Its main idea is to adopt the programming method of top-down and gradual refinement; Each module is connected through the control structure of "sequence, selection and circulation", and there is only one inlet and one outlet.

    8.The following algorithms are designed in a top-down and step-by-step manner:
     (1)Output 1900~2000 The year in the year is a leap year, which can be divided by 4 but can not be divided by 100; Or the year that can be divided by 100 and divided by 400 is a leap year.
     key:
 Design a 1900-2000 Cycle of
 Divisible by 4 and not divisible by 100 or divisible by 100 and divisible by 400
 Output leap year

     (2)seek ax^2+bx+c=0 Root of. Consider separately d=b^2-4ac=Greater than zero, equal to zero and less than zero.
     key:
  
  input a,b,c Value of
  seek d=b*b-4*a*c,n=b*b+4*a*c
  If, d>0,Then there are two real roots, x1=(-d)/(2a),x2=(-n)/(2a)
  If, d=0,Then there is a real root,x=(-n)/(2a)
  If, d<0,Then there is no real root

     (3)Input 10 numbers and output the largest one.
     key:

max = first number
Each input number is compared with max. if it is larger, it is assigned to max
Output max

Topics: C