|
|
|
Chapter 7Answers to Selected Exercises3. [was #4] (b) is not legal.
4. [was #6] (d) is illegal, since
10. [was #14]
12. [was #16] The value of
14. [was #18] No. Converting Answers to Selected Programming Projects
1. [was #2] 2. [was #8] #include <stdio.h> int main(void) { int i, n; char ch; printf("This program prints a table of squares.\n"); printf("Enter number of entries in table: "); scanf("%d", &n); ch = getchar(); /* dispose of new-line character following number of entries */ /* could simply be getchar(); */ for (i = 1; i <= n; i++) { printf("%10d%10d\n", i, i * i); if (i % 24 == 0) { printf("Press Enter to continue..."); ch = getchar(); /* or simply getchar(); */ } } return 0; } 5. [was #10] #include <ctype.h> #include <stdio.h> int main(void) { int sum = 0; char ch; printf("Enter a word: "); while ((ch = getchar()) != '\n') switch (toupper(ch)) { case 'D': case 'G': sum += 2; break; case 'B': case 'C': case 'M': case 'P': sum += 3; break; case 'F': case 'H': case 'V': case 'W': case 'Y': sum += 4; break; case 'K': sum += 5; break; case 'J': case 'X': sum += 8; break; case 'Q': case 'Z': sum += 10; break; default: sum++; break; } printf("Scrabble value: %d\n", sum); return 0; } 6. [was #12] #include <stdio.h> int main(void) { printf("Size of int: %d\n", (int) sizeof(int)); printf("Size of short: %d\n", (int) sizeof(short)); printf("Size of long: %d\n", (int) sizeof(long)); printf("Size of float: %d\n", (int) sizeof(float)); printf("Size of double: %d\n", (int) sizeof(double)); printf("Size of long double: %d\n", (int) sizeof(long double)); return 0; } Since the type of a Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved. |