好文档 - 专业文书写作范文服务资料分享网站

(完整word版)CPrimerPlus第6版编程练习答案(已下载)

天下 分享 时间: 加入收藏 我要投稿 点赞

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

PE 9---3

/* Programming Exercise 9-3 */ #include

void chLineRow(char ch, int c, int r); int main(void)

{ char ch; int col, row;

printf(\while ( (ch = getchar()) != '#') { if (ch == '\\n') continue;

printf(\if (scanf(\chLineRow(ch, col, row);

printf(\ }

printf(\ return 0; }

// start rows and cols at 0 void chLineRow(char ch, int c, int r) {

int col, row;

for (row = 0; row < r ; row++) {

for (col = 0; col < c; col++) putchar(ch); putchar('\\n'); } return; }

PE 9---5

/* Programming Exercise 9-5 */ #include

void larger_of(double *p1, double *p2); int main(void) {

double x, y; printf(\numbers (q to quit): \(scanf(\ {

larger_of(&x, &y);

printf(\printf(\ }

printf(\ return 0; }

void larger_of(double *p1, double *p2) { if (*p1 > *p2) *p2 =

16

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

*p1; else *p1 = *p2; }

// alternatively: /*

void larger_of(double *p1, double *p2) {

*p1= *p2 = *p1 > *p2 ? *p1 : *p2; } */

PE 9---8

/* Programming Exercise 9-8 */ #include

double power(double a, int b); /* ANSI prototype */ int main(void) { double x, xpow; int n; printf(\number and the integer power\

number will be raised. Enter q\while (scanf(\

{ xpow = power(x,n); /* function call */ printf(\xpow); printf(\quit.\\n\

} printf(\bye!\\n\

} double power(double a, int b) /* function definition */

{ double pow = 1; int i; if (b == 0) { if (a == 0)

printf(\pow = 1.0; } else if (a == 0) pow = 0.0; else if (b > 0) for(i = 1; i <= b; i++) pow *= a; else /* b < 0 */ pow = 1.0 / power(a, - b);

return pow; /* return the value of pow */ }

PE 9---10

/* Programming Exercise 9-10 */ #include void to_base_n(int x, int base); int main(void) { int number; int b; int count; printf(\to quit):\\n\== 1)

{ printf(\\&b))== 1

&& (b < 2 || b > 10)) {

printf(\ } if

(count != 1) break;

17

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

printf(\to_base_n(number, b); putchar('\\n');

printf(\ }

printf(\return 0; }

void to_base_n(int x, int base) /* recursive function */ { int r; r = x % base; if (x >= base) to_base_n(x / base, base); putchar('0' + r); return; }

Chapter 10

Programming Exercises

PE 10---1

/* Programming Exercise 10-1 */ #include

#define MONTHS 12 // number of months in a year #define YRS 5 // number of years of data int main(void) {

// initializing rainfall data for 2010 - 2014 const float rain[YRS][MONTHS] = {

{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6}, {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3}, {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4}, {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2}, {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2} }; int year,

month; float subtot, total;

printf(\for (year = 0, total = 0; year < YRS; year++)

{ /* for each year, sum rainfall for each month */ for (month = 0, subtot = 0; month < MONTHS; month++) subtot += *(*(rain + year) + month); printf(\

total += subtot; /* total for all years */ }

printf(\printf(\

printf(\printf(\

for (month = 0; month < MONTHS; month++)

{ /* for each month, sum rainfall over years */ for (year = 0, subtot =0; year < YRS; year++) subtot += *(*(rain + year) + month); printf(\subtot/YRS); }

printf(\return 0; }

18

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

PE 10---3

/* Programming Exercise 10-3 */ #include #define LEN 10

int max_arr(const int ar[], int n); void show_arr(const int ar[], int n);

int main(void) {

int orig[LEN] = {1,2,3,4,12,6,7,8,9,10}; int max;

show_arr(orig, LEN); max = max_arr(orig, LEN); printf(\largest value\\n\ return 0; }

int max_arr(const int ar[], int n) { int i; int max = ar[0];

/* don't use 0 as initial max value -- fails if all array values are neg */

for (i = 1; i < n; i++) if (max < ar[i]) max = ar[i]; return max; }

void show_arr(const int ar[], int n) { int i; for (i = 0; i < n; i++) printf(\putchar('\\n'); }

PE 10---5

/* Programming Exercise 10-5 */ #include #define LEN 10

double max_diff(const double ar[], int n); void show_arr(const double ar[], int n);

int main(void) {

double orig[LEN] = {1.1,2,3,4,12,61.3,7,8,9,10}; double max;

show_arr(orig, LEN); max = max_diff(orig, LEN);

printf(\ return 0; }

double max_diff(const double ar[], int n) { int i; double max = ar[0]; double min = ar[0];

for (i = 1; i < n; i++)

19

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

{

if (max < ar[i]) max = ar[i]; else if (min > ar[i]) min = ar[i]; }

return max - min; }

void show_arr(const double ar[], int n)

{ int i; for (i = 0; i < n; i++) printf(\putchar('\\n'); }

PE 10---8

/* Programming Exercise 10-8 */ #include #define LEN1 7 #define LEN2 3

void copy_arr(int ar1[], const int ar2[], int n); void show_arr(const int [], int); int

main(void) {

int orig[LEN1] = {1,2,3,4,5,6,7}; int copy[LEN2]; show_arr(orig, LEN1);

copy_arr(copy, orig + 2, LEN2); show_arr(copy, LEN2); return 0; }

void copy_arr(int ar1[], const int ar2[], int n) { int i; for (i = 0; i < n; i++) ar1[i] = ar2[i]; }

void show_arr(const int ar[], int n) { int i; for (i = 0; i < n; i++) printf(\putchar('\\n'); }

PE 10---11

/* Programming Exercise 10-11 */ #include #define ROWS 3 #define COLS 5

void times2(int ar[][COLS], int r); void showarr2(int ar[][COLS], int r);

int main(void) { int stuff[ROWS][COLS] =

{ {1,2,3,4,5}, {6,7,8,-2,10},

{11,12,13,14,15} }; showarr2(stuff,

20

(完整word版)CPrimerPlus第6版编程练习答案(已下载)

CPrimerPlusSixthEditionProgrammingExerciseSelectedAnswersPE9---3/*ProgrammingExercise9-3*/#includevoid
推荐度:
点击下载文档文档为doc格式
54zur7kenu5ap1c1kzfj507xn0uyj200qj6
领取福利

微信扫码领取福利

微信扫码分享