BookExercises-ch1-10 - Answers to Book Exercises Chapters...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Answers to Book Exercises – Chapters 2-10, 22.3, 23.1-3 Here are the answers to exercises from the second edition of C Programming: A Modern Approach . These answers do not necessarily conform to the coding standards for this course. Note: Some of these exercises were originally the even-numbered exercises in the first edition . For the benefit of those who have the first edition, the original exercise number is given in square brackets. For example, the notation [was #4] indicates that the number of the exercise was 4 in the first edition. If an answer is different because of second-edition changes, the word "modified" will appear inside the brackets: [was #4; modified]. Answers to Exercises (Chapter 2) 1. Using the -Wall option, gcc displays the warning "control reaches end of non-void function." To make this warning go away, add a return statement: #include <stdio.h> int main(void) { printf("hello, world\n"); return 0; } 2. [was #2] (a) The program contains one directive (#include) and four statements (three calls of printf and one return). (b) Parkinson's Law: Work expands so as to fill the time available for its completion. 3. [was #4] #include <stdio.h> int main(void) { int height = 8, length = 12, width = 10, volume; volume = height * length * width; printf("Dimensions: %dx%dx%d\n", length, width, height); printf("Volume (cubic inches): %d\n", volume); printf("Dimensional weight (pounds): %d\n", (volume + 165) / 166); return 0; } 4. [was #6] Here's one possible program: #include <stdio.h> int main(void) { int i, j, k; float x, y, z; printf("Value of i: %d\n", i); printf("Value of j: %d\n", j); printf("Value of k: %d\n", k); printf("Value of x: %g\n", x);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
printf("Value of y: %g\n", y); printf("Value of z: %g\n", z); return 0; } When compiled using GCC and then executed, this program produced the following output: Value of i: 5618848 Value of j: 0 Value of k: 6844404 Value of x: 3.98979e-34 Value of y: 9.59105e-39 Value of z: 9.59105e-39 The values printed depend on many factors, so the chance that you'll get exactly these numbers is small. 5. [was #10] (a) is not legal because 100_bottles begins with a digit. 6. As the example demonstrates, it can be difficult to determine the exact number of adjacent underscores in an identifier. 7. (a) Yes (b) No (if is a keyword) (c) No (d) No (e) Yes 8. [was #12] There are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;. 9. answer = (3 * q - p * p) / 3; 10. Apart from the #include preprocessing directive, which needs to be on its own line, only three spaces are essential: one between int and main, one between int and height, and one between return and 0. Answers to Exercises (Chapter 3) 1. In the answers below, • represents a space: (a) ••••86,1040 (b) •3.02530e+01 (c) 83.1620 (d) 1e-06• 2. [was #2] (a) printf("%-8.1e", x); (b) printf("%10.6e", x); (c) printf("%-8.3f", x); (d) printf("%6.0f", x); 3. (a) Equivalent (b) Not equivalent; the string on the right will skip any
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

BookExercises-ch1-10 - Answers to Book Exercises Chapters...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online