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

C Primer Plus第6版编程练习答案

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

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] = {,2,3,4,12,,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++) {

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, 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\COLS, stuff);

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

printf(\printf(\

printf(\return 0;

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

{ int i;

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

printf(\scanf(\ }

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

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 ; }

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 = ;

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

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; }

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; n\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'); n\ } } if (ok)

while ((ch = getchar() ) != EOF) {

switch(mode) {

case 'p' : putchar(ch); break;

case 'u' : putchar(toupper(ch)); break;

case 'l' : putchar(tolower(ch)); } }

return 0; }

C Primer Plus第6版编程练习答案

intmain(void){intorig[LEN]={1,2,3,4,12,6,7,8,9,10};intmax;show_arr(orig,LEN);max=max_arr(orig,LEN);printf(\largestvalue\\n\
推荐度:
点击下载文档文档为doc格式
13cub6euwq9s4tl8lgrm6o2vt5lzj600cs5
领取福利

微信扫码领取福利

微信扫码分享