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

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

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

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

ROWS); putchar('\\n'); times2(stuff, ROWS); showarr2(stuff, ROWS);

return 0; }

void times2(int ar[][COLS], int r) {

int row, col;

for (row = 0; row < r; row++) for (col = 0; col < COLS; col++) ar[row][col] *= 2; }

void showarr2(int ar[][COLS], int r) {

int row, col;

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

{ for (col = 0; col < COLS; col++) printf(\

ar[row][col]); putchar('\\n'); } }

PE 10---14

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

void store(double ar[], int n);

double average2d(int rows, int cols, double ar[rows][cols]); double max2d(int rows, int cols, double ar[rows][cols]); void showarr2(int rows, int cols, double ar[rows][cols]); double average(const double ar[], int n);

int main(void) { double

stuff[ROWS][COLS]; int row;

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

printf(\store(stuff[row], COLS); } printf(\contents:\\n\

showarr2(ROWS, COLS, stuff);

for (row = 0; row < ROWS; row++)

printf(\printf(\

printf(\return 0;

} void store(double ar[], int n)

{ int i;

21

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

for (i = 0; i < n; i++) {

printf(\scanf(\ }

} double average2d(int rows, int cols, double ar[rows][cols]) { int r, c; double sum = 0.0;

for (r = 0; r < rows; r++) for (c = 0; c < cols; c++) sum += ar[r][c]; if (rows * cols > 0) return sum / (rows * cols); else return 0.0; }

double max2d(int rows, int cols, double ar[rows][cols]) { int r, c; double max = ar[0][0];

for (r = 0; r < rows;

r++) for (c = 0; c < cols; c++) if (max <

ar[r][c]) max = ar[r][c]; return max; }

void showarr2(int rows, int cols, double ar[rows][cols]) {

int row, col;

for (row = 0; row < rows; row++) { for (col = 0; col < cols; col++) printf(\

ar[row][col]); putchar('\\n'); } }

double average(const double ar[], int n) { int i; double sum = 0.0;

for (i = 0; i < n; i++) sum += ar[i]; if (n > 0) return sum / n; else return 0.0; }

Chapter 11

Programming Exercises

PE 11---1

/* Programming Exercise 11-1 */ #include #define LEN 10 char * getnchar(char * str, int n);

int main(void) { char input[LEN]; char *check; check = getnchar(input, LEN - 1); if (check == NULL) puts(\failed.\

puts(input); puts(\return 0;

22

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

}

char * getnchar(char * str, int n) { int i; int ch;

for (i = 0; i < n; i++) { ch = getchar(); if (ch != EOF)

str[i] = ch; else break; } if (ch == EOF) return NULL; else { str[i] = '\\0'; return str; } }

PE 11---3

/* Programming Exercise 11-3 */ #include #define LEN 80 char * getword(char * str); int

main(void) { char input[LEN]; while (getword(input) != NULL)

puts(input); puts(\return 0; }

#include

char * getword(char * str)

{ int ch; char * orig = str; // skip over initial whitespace while ((ch = getchar()) != EOF && isspace(ch)) continue; if (ch == EOF) return NULL; else

*str++ = ch; // first character in word // get rest of word

while ((ch = getchar()) != EOF && !isspace(ch)) *str++ = ch; *str = '\\0'; if (ch == EOF) return NULL; else { while (ch != '\\n') ch = getchar(); return orig; } }

PE 11---6

/* Programming Exercise 11-6 */ #include #include #define LEN 80

_Bool is_within(const char * str, char c); char * s_gets(char * st, int n);

int main(void) { char

input[LEN]; char

ch; int found;;

23

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

printf(\

while (s_gets(input, LEN) && input[0] != '\\0') {

printf(\ch = getchar(); while (getchar() != '\\n') continue; found =

is_within(input, ch); if (found == 0)

printf(\else

printf(\printf(\ }

puts(\ return 0; }

_Bool is_within(const char * str, char ch) {

while (*str != ch && *str != '\\0') str++;

return *str; /* = 0 if \\0 reached, non-zero otherwise */ } char * s_gets(char * st, int n) { char *

ret_val; char * find;

ret_val = fgets(st, n, stdin); if (ret_val) {

find = strchr(st, '\\n'); // look for newline if (find) // if the address is not NULL, *find = '\\0'; // place a null character there else

while (getchar() != '\\n') continue; }

return ret_val; }

PE 11---8

/* Programming Exercise 11-8 */ #include #define LEN 20

char * string_in(const char * s1, const char * s2); int main(void) {

char orig[LEN] = \char * find; puts(orig); find = string_in(orig, \if (find) puts(find); else puts(\find = string_in(orig, \if (find) puts(find); else

puts(\ return 0; }

24

C Primer Plus Sixth Edition Programming Exercise

Selected Answers

#include

char * string_in(const char * s1, const char * s2) {

int l2 = strlen(s2);

int tries; /* maximum number of comparisons */ int nomatch = 1; /* set to 0 if match is found */

tries = strlen(s1) + 1 - l2; if (tries > 0)

while (( nomatch = strncmp(s1, s2, l2)) && tries--) s1++; if (nomatch) return NULL; else return (char *) s1; /* cast const away */ }

PE 11---10

/* Programming Exercise 11-10 */ #include

#include // for strchr(); #define LEN 81 int

drop_space(char * s); char * s_gets(char * st, int n);

int main(void) {

char orig[LEN];

puts(\less:\'\\0')

{ drop_space(orig); puts(orig);

puts(\ }

puts(\return 0; }

int drop_space(char * s) { char * pos;

while (*s) /* or while (*s != '\\0') */ {

if (*s == ' ')

{ pos = s; do {

*pos = *(pos + 1); pos++; } while (*pos); } else s++; } }

char * s_gets(char * st, int n) { char *

ret_val; char * find;

25

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

CPrimerPlusSixthEditionProgrammingExerciseSelectedAnswersROWS);putchar('\\n');times2(stuff,ROWS);showarr2(stuff,ROWS);
推荐度:
点击下载文档文档为doc格式
54zur7kenu5ap1c1kzfj507xn0uyj200qj6
领取福利

微信扫码领取福利

微信扫码分享