C language programming: modern methods (version 2nd) Chapter 2 all exercises answer, C language programming exercises
Preface
I learned the C language through "C language programming: modern methods (version 2nd)" and found that there is no complete answer to the exercises in the book in China, so I want to share my answers in the learning process for your reference. These answers are answered by myself. For more information, see share on GitHub and Chegg.com. Because there is no authoritative answer source, there may be errors. If there are any errors, I hope you can help us to point them out.
Chapter 2 exercise questions and programming answers exercise questions section 2.2
1. Create and run the famous "hello world" program compiled by Kernighan and Ritchie:
1 #include <stdio.h>
2 int main(void)
3 {
4 printf("hello world!\n");
5 }
Is there warning information during compilation? If so, how can we improve it?
A: If some old compilers such as turbo C are used, a warning message is generated, indicating that the main function does not return any value. This is because int is used to declare the main function, indicating that the function will return an integer value, which is arbitrary (because the main is followed by void ). However, the above Code does not have a return statement, so it does not return any value. After the return statement is added, warnings can be cleared.
1 / * Version after adding return statement * /
2 #include <stdio.h>
3 int main (void)
4 {
5 printf ("Hello world! \ N");
6 return 0;
7}
If the compiler in IDE is used and the IDE version is relatively new (for example, the current Code: Blocks), the compiler will not generate a warning even if there is no return statement.
2. Think about the following procedure:
1 #include <stdio.h>
2 int main(void)
3 {
4 printf("Parkinson's Law:\nWork expands so as to ");
5 printf("fill the time\n");
6 printf("available for its completion.\n");
7 return 0;
8 }
(A) Indicate the commands and statements in the program.
(B) What is the output of the program?
A: (a) the program contains a command # include and three printf statements and a return statement.
(B) the output result of the program is
Parkinson's Law:
Work expands so as to fill the time
available for its completion.
Section 2.4
3. use the following method to abbreviated the program dweight. c: (1) Use the initialization method to replace the values of height, length, and width. (2) Remove the weight variable and calculate it in the final printf Statement (volume + 165) /166.
A:
1 #include <stdio.h>
2 int main (void)
3 {
4 / * Use initialization instead of assigning a value to a variable is directly assigning a value to the variable when it is declared * /
5 int height = 8, length = 12, width = 10, volume = height * length * width;
6 printf ("Dimensions:% dx% dx% d \ n", length, width, height);
7 printf ("Volume (cubic inches):% d \ n", volume);
8 / * Here, the original weight is omitted, and the calculation formula is directly placed in the printf statement * /
9 printf ("Dimensional weight (pounds):% d \ n", (volume + 165) / 166);
10 return 0;
11}
4. Write a program to declare several int and float variables, do not initialize these variables, and then directly display their values. Are these values regular? (Not usually ).
A:
1 #include <stdio.h>
2 int main(void)
3 {
4 int a, b, c;
5 float e, f, g;
6 printf("a:%d\nb:%d\nc:%d\ne:%f\n%f:f\ng:%f\n"a, b, c, d, e, f, g);
7 return 0;
8 }
In fact, there is no rule. The above variables are neither initialized nor assigned values in the program. The value displayed for the variables depends on many factors, so the final display result cannot be determined.
Section 2.7
5. Which of the following C language identifiers are valid?
(A) 100_bottles
(B) _ 100_bottles
(C) one_hundred_bottles
(D) bottles_by_the_hundred _
A: Invalid identifiers are only (a). Because identifiers can only start with letters or underscores, and 100_bottles cannot start with a number. In addition, the C language also specifies that the underscores (_) followed by an uppercase letter are reserved words and should not be used in programs (as mentioned later in the book ).
6. Why is it inappropriate to use multiple adjacent underscores (such as current ___ balance) in the identifier?
A: When reading a program, it is difficult to tell whether there are several adjacent underlines, such as current ____ balance and current _____ balance. The former is 4 underscores, and the latter is 5 underscores. In future maintenance programs, no matter the creator or others, it is easy to mix the numbers of underscores, resulting in unnecessary confusion.
7. determine which of the following are the keywords of C language?
(A)
(B) If
(C) main
(D) printf
(E) while
A: Only (a) for and (e) while are keywords, and none of them are. (B) If is case sensitive in C, so If and if are different. if is a keyword but If is not. (C) main and (d) printf are not keywords. The main () function is a function automatically called by c language, but we can still name the variable main (not recommended ). Printf () Is the function contained in our header file <stdio. h>. If this header file is not called, we can name the variable printf. In general, only words in the keyword table are keywords, and others are not keywords.
Section 2.8
8. How many marks are there in the following statements?
Answer = (3 * q-p * p)/3;
Answer: 14 marks: answer, =, (, 3, *, q,-, p, *, p,),/, 3, and ,;.
9. Insert a space between the marks of Exercise 8 to make the statement easier to read.
Answer: answer = (3 * q-p * p)/3;
10. What spaces are essential in the dweight. c program (Section 2.4?
A: there must be no less space after the int that appears twice in the program, and return 0; there must be no less space in the middle. Spaces in other parts of the program exist for ease of reading. Even if the program itself can be removed, it can still be executed normally, but it increases reading difficulties.
Programming questions
1. Write a program and use printf to display the following figure on the screen:
*
*
*
**
**
*
A:
1 #include <stdio.h>
2
3 int main(void)
4 {
5 printf(" *\n");
6 printf(" *\n");
7 printf(" *\n");
8 printf("* *\n");
9 printf(" * *\n");
10 printf(" *\n");
11
12 return 0;
13 }
2. Compile a program for calculating the volume of a sphere. The radius of the sphere is 10 m. See the formula v = 4/3 π r ³. Note that 4/3 should be written as 4.0f/3.0f. (If the score is 4/3, what will happen ?) Tip: the C language does not have an exponential operator. Therefore, you need to multiply the r operator twice to calculate the r operator.
A:
1 #include <stdio.h>
2 / * Define macros named PI and SCALE_FACTOR * /
3 #define PI 3.14f
4 #define SCALE_FACTOR 4.0f / 3.0f
5
6 int main (void)
7 {
8
9 / * Declare variables and initialize them, r is the radius, volume is the calculation result, r_3 is the cube result of r * /
10 float r = 10.0f;
11 float volume = 1.0f;
12 float r_3 = 0.0f;
13
14 / * Calculate the volume of a sphere with a radius of 10m * /
15 r_3 = r * r * r;
16 volume = SCALE_FACTOR * PI * r_3;
17
18 / * show result * /
19 printf ("Result is:% f \ n", volume);
20
21 return 0;
twenty two }
If you program the score 4/3 in 4.0f/3.0f, the calculation result is incorrect because the 4/3 result is 1 rather than 1.33333333. The integer division in C language will remove the decimals.
3. modify the program in the above question so that you can enter the sphere radius by yourself.
A:
1 #include <stdio.h>
2 / * Define macros named PI and SCALE_FACTOR * /
3 #define PI 3.14f
4 #define SCALE_FACTOR 4.0f / 3.0f
5
6 int main (void)
7 {
8
9 / * Declare variables and initialize them, r is the radius, volume is the calculation result, r_3 is the cube result of r * /
10 float r = 1.0f;
11 float volume = 1.0f;
12 float r_3 = 0.0f;
13
14 / * Enter the sphere radius. Note that the variable r must be preceded by an ampersand * /
15 printf ("Enter number:");
16 scanf ("% f", & r);
17
18 / * Calculate the volume of a sphere with a radius of 10m * /
19 r_3 = r * r * r;
20 volume = SCALE_FACTOR * PI * r_3;
twenty one
22 printf ("Result is:% f \ n", volume);
twenty three
24 return 0;
25}
4. Write a program that requires the user to enter a dollar number and then display the corresponding amount after the 5% tax rate is increased. The format is as follows:
Enter an amount: 100.00
With tax added: $105.00
A:
1 #include <stdio.h>
2
3 int main (void)
4 {
5 / * Declare and initialize the variables money and taxed_money. The former represents the amount and the latter represents the amount after taxation. * /
6 float money = 0.0f;
7 float taxed_money = 0.0f;
8
9 / * Amount entry * /
10 printf ("Enter an amount:");
11 scanf ("% f", & money);
12
13 / * Amount after tax calculation * /
14 taxed_money = money * 1.05;
15
16 / * Display the result, use .2f because the amount after tax in the title only retains two decimal places * /
17 printf ("With tax added: $%. 2f \ n", taxed_money);
18
19 return 0;
20}
5. Write a code that requires the user to enter the value of x and then display the value of the following polynomial:
3x5 + 2x4-5x3-x2 + 7x-6
Tip: the C language does not have an exponential operator, so we need to perform auto-multiplication on x to calculate its power. (For example, x * x is the cubic power of x .)
A:
1 #include <stdio.h>
2
3 int main (void)
4 {
5 / * Declare and initialize the variable x and the result of the calculation. The float type is selected because the title does not say that the user cannot enter decimals.
6 float x = 0.0f;
7 float result = 0.0f;
8
9 / * The user enters the value of x and writes the value into the variable x * /
10 printf ("Enter the value of x:");
11 scanf ("% f", & x);
12
13 / * result of calculation formula * /
14 result = 3 * x * x * x * x * x + 2 * x * x * x * x-5 * x * x * x-x * x + 7 * x-6;
15
16 printf ("The result is:% f", result);
17
18 return 0;
19}
6. Modify the above question and evaluate the polynomial using the following formula:
(3x + 2) X-5) x-1) x + 7) x-6
Note that the number of multiplication times required by the modified program is reduced. This polynomial evaluate method isHorner rule(Horner's Rule).
A:
1 #include <stdio.h>
2
3 int main (void)
4 {
5 float x = 0.0f;
6 float result = 0.0f;
7
8 printf ("Enter the value of x:");
9 scanf ("% f", & x);
10
11 / * Modified calculation formula * /
12 result = ((((3 * x + 2) * x-5) * x-1) * x + 7) * x-6;
13
14 printf ("The result is:% f", result);
15
16 return 0;
17}
7. Write a program that requires the user to enter a dollar number and then shows how to pay with the minimum $20, $10, $5, and $1:
Enter a dollar amount: 93
$20 bills: 4
$10 bills: 1
$5 bills: 0
$1 bills: 3
A:
1 #include <stdio.h>
2
3 int main (void)
4 {
5 / * Declare and initialize the total amount amount and dollar denomination variables * /
6 int amount = 0;
7 int b20 = 0, b10 = 0, b05 = 0, b01 = 0;
8
9 printf ("Enter a dollar amount:");
10 scanf ("% d", & amount);
11
12 / * Because the requirement is to pay with the minimum number of sheets, first divide the total amount by 20 to get the required amount of $ 20, then subtract the total amount from the required $ 20 and divide by 10 to get the required $ 10 Quantity, and so on to get all results * /
13 b20 = amount / 20;
14 amount = amount-20 * b20;
15 b10 = amount / 10;
16 amount = amount-10 * b10;
17 b05 = amount / 5;
18 amount = amount-5 * b05;
19 b01 = amount / 1;
20
21 / * show result * /
22 printf ("\ n $ 20 bills:% d \ n $ 10 bills:% d \ n $ 5 bills:% d \ n $ 1 bills:% d \ n", b20, b10, b05, b01);
twenty three
24 return 0;
25}
8. Calculate the remaining loan amount after the first, second, and third months of the program:
Enter amount of loan: 20000.00
Enter interest rate: 6.0
Enter monthly payment: 386.66
Balance remaining after first payment: $19713.34
Balance remaining after second payment: $19425.25
Balance remaining after third payment: $19135.71
Retain two decimal places when displaying the balance after each repayment. Tip: After the loan balance of each month minus the repayment amount, you must add the product of the loan balance and monthly interest rate. The monthly interest rate is calculated by dividing the user-input interest rate into a hundred points by 12.
A:
1 #include <stdio.h>
2
3 int main (void)
4 {
5 / * Declare and initialize variables, loan is loan amount, y_inter is annual interest rate, m_inter monthly interest rate, and m_pay is monthly repayment amount * /
6 float loan = 0.0f;
7 float y_inter = 0.0f;
8 float m_inter = 0.0f;
9 float m_pay = 0.0f;
10
11 / * User enters loan amount, annual interest rate, monthly repayment amount * /
12 printf ("Enter amount of loan:");
13 scanf ("% f", & loan);
14 printf ("Enter interest rate:");
15 scanf ("% f", & y_inter);
16 printf ("Enter monthly payment:");
17 scanf ("% f", & m_pay);
18
19 / * Calculate the monthly interest rate, plus 1 because if the monthly interest rate is multiplied by the loan amount to get the interest, the interest will be added to the loan amount. After adding 1, you can directly get the result of the loan amount plus interest * /
20 m_inter = y_inter / 100/12 + 1;
twenty one
22 / * Find the remaining amount after the first month's repayment * /
23 loan = loan * m_inter-m_pay;
24 printf ("Balance remaining after first payment:% .2f \ n", loan);
25
26 / * Find the remaining amount after the second month repayment * /
27 loan = loan * m_inter-m_pay;
28 printf ("Balance remaining after second payment:% .2f \ n", loan);
29
30 / * Find the remaining amount after the third month repayment * /
31 loan = loan * m_inter-m_pay;
32 printf ("Balance remaining after third payment:% .2f \ n", loan);
33
34 return 0;
35}
36 / * The repayment in the title is the method of equal principal and interest, a loan of 20,000, an annual interest rate of 6%, a total of 5 years of loan * /