C Primer Plus Sixth Edition Programming Exercise
Selected Answers
/* this implementation assumes the character codes */ /* are sequential, as they are in ASCII. */ #include
for (end = let; end >= 'A'; end--) {
for (start = let; start >= end; start--) printf(\ } return 0; }
PE 6---6
/* pe6-6.c */ #include
{ int lower, upper,
index; int square, cube;
printf(\scanf(\ending integer: \&upper);
printf(\\index++)
{ square = index * index; cube = index * square;
printf(\ } return 0; }
PE 6---8
/* pe6-8.c */ #include
printf(\
while (scanf(\ {
res = (n - m) / (n * m);
printf(\printf(\ }
return 0; }
6
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
PE 6---11
/* pe6-11.c */ #include
vals[SIZE]; int i;
printf(\for (i = 0; i < SIZE; i++) scanf(\&vals[i]);
printf(\for (i = SIZE - 1; i >= 0; i--) printf(\printf(\}
PE 6---13
/* pe6-13.c */
/* This version starts with the 0 power */ #include
int twopows[SIZE]; int i;
int value = 1; /* 2 to the 0 */
for (i = 0; i < SIZE; i++) { twopows[i] = value; value *= 2; } i = 0; do {
printf(\i++; } while (i < SIZE);
printf(\return 0; }
PE 6---14
/* pe-14.c */
/* Programming Exercise 6-14 */ #include
{ double arr[SIZE]; double arr_cumul[SIZE]; int i;
printf(\SIZE);
for (i = 0; i < SIZE; i++) {
printf(\scanf(\scanf(\
7
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
}
arr_cumul[0] = arr[0]; /* set first element */ for (i = 1; i < SIZE; i++)
arr_cumul[i] = arr_cumul[i-1] + arr[i]; for (i = 0; i < SIZE; i++) printf(\
printf(\SIZE; i++) printf(\
arr_cumul[i]); printf(\return 0; }
PE 6---16
/* pe6-16.c */
#include
#define RATE_SIMP 0.10 #define RATE_COMP 0.05
#define INIT_AMT 100.0 int main( void ) {
double daphne = INIT_AMT; double deidre = INIT_AMT; int years = 0;
while (deidre <= daphne)
{ daphne += RATE_SIMP *
INIT_AMT; deidre += RATE_COMP * deidre;
++years; }
printf(\printf(\$%.2f\\n\}
Chapter 7 Programming Exercises
PE 7---1
/* Programming Exercise 7-1 */ #include
{ char ch; int sp_ct = 0; int nl_ct = 0;
int other = 0; while ((ch =
getchar()) != '#') {
if (ch == ' ') sp_ct++; else if (ch == '\\n')
nl_ct++; else other++; }
printf(\
8
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
return 0; }
PE 7---3
/* Programming Exercise 7-3 */ #include
{ int n; double sumeven = 0.0; int ct_even = 0; double sumodd = 0.0; int ct_odd = 0;
while (scanf(\ {
if (n % 2 == 0) {
sumeven += n; ++ct_even; }
else // n % 2 is either 1 or -1 {
sumodd += n; ++ct_odd; } }
printf(\if (ct_even > 0)
printf(\putchar('\\n');
printf(\if (ct_odd > 0)
printf(\putchar('\\n'); printf(\ return 0; }
PE 7---5
/* Programming Exercise 7-5 */ #include
{ char ch; int ct1 = 0; int ct2 = 0; while ((ch = getchar()) != '#') {
switch(ch) {
case '.' : putchar('!'); ++ct1; break; case '!' : putchar('!'); putchar('!');
9
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
++ct2; break; default : putchar(ch); } }
printf(\printf(\ return 0; }
PE 7---7
// Programming Exercise 7-7 #include
#define BASEPAY 10 // $10 per hour
#define BASEHRS 40 // hours at basepay #define OVERTIME 1.5 // 1.5 time
#define AMT1 300 // 1st rate tier #define AMT2 150 // 2st rate tier
#define RATE1 0.15 // rate for 1st tier #define RATE2 0.20 // rate for 2nd tier
#define RATE3 0.25 // rate for 3rd tier int main(void) {
double hours; double gross;
double net; double taxes;
printf(\scanf(\gross = hours * BASEPAY; else
gross = BASEHRS * BASEPAY + (hours - BASEHRS) * BASEPAY * OVERTIME; if (gross <= AMT1) taxes = gross * RATE1; else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2; else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3; net = gross - taxes;
printf(\ return 0; }
PE 7---9
/* Programming Exercise 7-9 */ #include
#include
int limit; int num; int div;
bool numIsPrime; // use int if stdbool.h not available
printf(\while (scanf(\ {
10
(完整word版)CPrimerPlus第6版编程练习答案(已下载)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)