Chapter 2
Answers to Selected Exercises
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
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
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);
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.
8. [was #12] There are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3,
and ;.
Answers to Selected Programming Projects
4. [was #8; modified]
#include
int main(void)
{
float original_amount, amount_with_tax;
printf("Enter an amount: ");
scanf("%f", &original_amount);
amount_with_tax = original_amount * 1.05f;
printf("With tax added: $%.2f\n", amount_with_tax);
return 0;
}
The amount_with_tax variable is unnecessary. If we remove it, the program
is slightly shorter:
#include
int main(void)
{
float original_amount;
printf("Enter an amount: ");
scanf("%f", &original_amount);
printf("With tax added: $%.2f\n", original_amount * 1.05f);
return 0;
}
Chapter 3
Answers to Selected Exercises
2. [was #2]
(a) printf("%-8.1e", x);
(b) printf("%10.6e", x);
(c) printf("%-8.3f", x);
(d) printf("%6.0f", x);
5. [was #8] The values of x, i, and y will be 12.3, 45, and .6, respectively.
Answers to Selected Programming Projects
1. [was #4; modified]
#include
int main(void)
{
int month, day, year;
printf("Enter a date (mm/dd/yyyy): ");
scanf("%d/%d/%d", &month, &day, &year);
printf("You entered the date %d%.2d%.2d\n", year, month, day);
return 0;
}
3. [was #6; modified]
#include
int main(void)
{
int prefix, group, publisher, item, check_digit;
printf("Enter ISBN: ");
scanf("%d-%d-%d-%d-%d", &prefix, &group, &publisher, &item,
&check_digit);
printf("GS1 prefix: %d\n", prefix);
printf("Group identifier: %d\n", group);
printf("Publisher code: %d\n", publisher);
printf("Item number: %d\n", item);
printf("Check digit: %d\n", check_digit);
/* The five printf calls can be combined as follows:
printf("GS1 prefix: %d\nGroup identifier: %d\nPublisher
code: %d\nItem number: %d\nCheck digit: %d\n",
prefix, group, publisher, item, check_digit);
*/
return 0;
}
Chapter 4
Answers to Selected Exercises
2. [was #2] Not in C89. Suppose that i is 9 and j is 7. The value of (-i)/j
could be either –1 or –2, depending on the implementation. On the other
hand, the value of -(i/j) is always –1, regardless of the implementation.
In C99, on the other hand, the value of (-i)/j must be equal to the value
of -(i/j).
9. [was #6]
(a) 63 8
(b) 3 2 1
(c) 2 -1 3
(d) 0 0 0
13. [was #8] The expression ++i is equivalent to (i += 1). The value of
both expressions is i after the increment has been performed.
Answers to Selected Programming Projects
2. [was #4]
#include
int main(void)
{
int n;
printf("Enter a three-digit number: ");
scanf("%d", &n);
printf("The reversal is: %d%d%d\n", n % 10, (n / 10) % 10, n / 100);
return 0;
}
Chapter 5
Answers to Selected Exercises
2. [was #2]
(a) 1
(b) 1
(c) 1
(d) 1
4. [was #4] (i > j) - (i < j)
6. [was #12] Yes, the statement is legal. When n is equal to 5, it does
nothing, since 5 is not equal to –9.
10. [was #16] The output is
onetwo
since there are no break statements after the cases.
Answers to Selected Programming Projects
2. [was #6]
#include
int main(void)
{
int hours, minutes;
printf("Enter a 24-hour time: ");
scanf("%d:%d", &hours, &minutes);
printf("Equivalent 12-hour time: ");
if (hours == 0)
printf("12:%.2d AM\n", minutes);
else if (hours < 12)
printf("%d:%.2d AM\n", hours, minutes);
else if (hours == 12)
printf("%d:%.2d PM\n", hours, minutes);
else
printf("%d:%.2d PM\n", hours - 12, minutes);
return 0;
}
4. [was #8; modified]
#include
int main(void)
{
int speed;
printf("Enter a wind speed in knots: ");
scanf("%d", &speed);
if (speed < 1)
printf("Calm\n");
else if (speed <= 3)
printf("Light air\n");
else if (speed <= 27)
printf("Breeze\n");
else if (speed <= 47)
printf("Gale\n");
else if (speed <= 63)
printf("Storm\n");
else
printf("Hurricane\n");
return 0;
}
6. [was #10]
#include
int main(void)
{
int check_digit, d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5,
first_sum, second_sum, total;
printf("Enter the first (single) digit: ");
scanf("%1d", &d);
printf("Enter first group of five digits: ");
scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5);
printf("Enter second group of five digits: ");
scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5);
printf("Enter the last (single) digit: ");
scanf("%1d", &check_digit);
first_sum = d + i2 + i4 + j1 + j3 + j5;
second_sum = i1 + i3 + i5 + j2 + j4;
total = 3 * first_sum + second_sum;
if (check_digit == 9 - ((total - 1) % 10))
printf("VALID\n");
else
printf("NOT VALID\n");
return 0;
}
10. [was #14]
#include
int main(void)
{
int grade;
printf("Enter numerical grade: ");
scanf("%d", &grade);
if (grade < 0 || grade > 100) {
printf("Illegal grade\n");
return 0;
}
switch (grade / 10) {
case 10:
case 9: printf("Letter grade: A\n");
break;
case 8: printf("Letter grade: B\n");
break;
case 7: printf("Letter grade: C\n");
break;
case 6: printf("Letter grade: D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: printf("Letter grade: F\n");
break;
}
return 0;
}
Chapter 6
Answers to Selected Exercises
4. [was #10] (c) is not equivalent to (a) and (b), because i is incremented
before the loop body is executed.
10. [was #12] Consider the following while loop:
while (…) {
…
continue;
…
}
The equivalent code using goto would have the following appearance: