C Primer Plus(第六版)第十章 编程练习答案
Posted 水番正文
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C Primer Plus(第六版)第十章 编程练习答案相关的知识,希望对你有一定的参考价值。
前言:这次真的隔了好久才更,今日翻了几篇DASCTF文章,发现个个都是六边形战士,自己要得更努力,淦!
仅供参考,新手勿喷。
CH09 Code answer 1:
/* rain.c -- finds yearly totals, yearly average, and monthly
average for several years of rainfall data */
#include <stdio.h>
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
int main(void)
{
// initializing rainfall data for 2010 - 2014
const float rain[YEARS][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;
const float (* pi)[MONTHS]; //赋值给一个指针,当然下面用rain也行
pi = rain;
printf(" YEAR RAINFALL (inches)\\n");
for (year = 0, total = 0; year < YEARS; year++)
{ // for each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += *(*(pi+year)+month);
printf("%5d %15.1f\\n", 2010 + year, subtot);
total += subtot; // total for all years
}
printf("\\nThe yearly average is %.1f inches.\\n\\n",
total/YEARS);
printf("MONTHLY AVERAGES:\\n\\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec\\n");
for (month = 0; month < MONTHS; month++)
{ // for each month, sum rainfall over years
for (year = 0, subtot =0; year < YEARS; year++)
subtot += *(*(pi+year)+month);
printf("%4.1f ", subtot/YEARS);
}
printf("\\n");
return 0;
}
CH09 Code answer 2:
#include <stdio.h>
void copy_arr(double ar1[],double ar2[],int);
void copy_ptr(double *,double *,int);
void copy_ptrs(double *,double *,double *);
void show_arr(double *,int);
int main(void)
{
double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
double target3[5];
copy_arr(target1, source, 5);
show_arr(target1,5);
copy_ptr(target2, source, 5);
show_arr(target2,5);
copy_ptrs(target3, source, source+5);
show_arr(target3,5);
}
void show_arr(double * ar,int n)
{
for(int i = 0;i < n;i++)
printf("%-5.1lf",*(ar+i));
printf("\\n");
}
void copy_arr(double ar1[],double ar2[],int n)
{
for(int i = 0;i < n;i++)
*(ar1+i) = *(ar2+i);
}
void copy_ptr(double * ar1,double * ar2,int n)
{
for(int i = 0; i < n; i++)
*(ar1+i) = *(ar2+i);
}
void copy_ptrs(double * ar1,double * ar2,double * ar3)
{
for(int i = 0; *(ar2+i) != *ar3;i++)
*(ar1+i) = *(ar2+i);
}
CH09 Code answer 3:
#include <stdio.h>
int find_max(int *,int);
int main(void)
{
int ar1[]={10,1,2,3,4,5,6,7,8,9};
printf("%d",find_max(ar1,sizeof(ar1)/sizeof(int)));
}
int find_max(int * ar,int n)
{
int max = ar[n];
while(--n >= 0)
{
if(ar[n] > max)
max = ar[n];
}
return max;
}
CH09 Code answer 4:
#include <stdio.h>
int find_max_b(double *,int);
int main(void)
{
double ar[] = {10,1,2,3,4,5,6,7,8,9};
printf("%d",find_max_b(ar,sizeof(ar)/sizeof(double)));
}
int find_max_b(double * ar,int n)
{
int max = ar[n];
int max_b = n;
while(--n >= 0)
{
if(ar[n] > max)
{
max = ar[n];
max_b = n;
}
}
return max_b;
}
CH09 Code answer 5:
#include <stdio.h>
double find_max_min_c(double *,int n);
int main(void)
{
double ar[] = {10,1,2,3,4,5,6,7,8,9};
printf("%.1lf",find_max_min_c(ar,sizeof(ar)/sizeof(double)));
}
double find_max_min_c(double * ar, int n)
{
double max,min;
if(*(ar+n-1) > *(ar+n-2))
{
max = *(ar+n-1);
min = *(ar+n-2);
}
else
{
min = *(ar+n-1);
max = *(ar+n-2);
}
n-=2;
while(--n >= 0)
{
if( *(ar+n) > max)
max = *(ar+n);
if( *(ar+n) < min)
min = *(ar+n);
}
return max-min;
}
CH09 Code answer 6:
#include <stdio.h>
void re_sort_double(double *,int);
void show_ar(double *,int);
int main(void)
{
double ar[] = {0,1,2,3,4,5,6,7,8,9};
re_sort_double(ar,sizeof(ar)/sizeof(double));
show_ar(ar,sizeof(ar)/sizeof(double));
}
void re_sort_double(double * ar,int n)
{
double tmp;
int b = n - 1; //用n--会导致n的值变化,再减1获取最后的下标
for(int i = 0; i < n/2; i++,b--) //不管个数是奇偶,只要到一半就足够了
{
tmp = *(ar+b);
*(ar+b) = *(ar+i);
*(ar+i) = tmp;
}
}
void show_ar(double * ar,int n)
{
for(int i = 0 ;i < n;i++)
printf("%.1lf ",*(ar+i));
}
CH09 Code answer 7:
#include <stdio.h>
#define COLS 2
#define ROWS 3
void copy_ptr(double (*ar1)[COLS],double (*ar2)[COLS],int);
void show_ar(double (*ar)[COLS],int);
int main(void)
{
double source[ROWS][COLS] = {
{0,1},
{2,3},
{4,5}
};
double target[ROWS][COLS];
copy_ptr(target,source,ROWS);
show_ar(target,ROWS);
return 0;
}
void copy_ptr(double (*ar1)[COLS],double (*ar2)[COLS],int rows)
{
for(int i = 0; i <= rows-1; i++) //注意下标从0开始
for(int j = 0; j <= COLS-1; j++)
*(*(ar1+i)+j) = *(*(ar2+i)+j);
// printf("1");
}
void show_ar(double (*ar)[COLS],int rows)
{
for(int i = 0; i <= rows-1; i++)
for(int j = 0; j <= COLS-1; j++)
printf("%0.1lf ",*(*(ar+i)+j));
// printf("1");
}
CH09 Code answer 8:
#include <stdio.h>
void t_s(int *,int *,int);
void show_ar(int *,int n);
int main(void)
{
int source[] = {0,1,2,3,4,5,6};
int target[3];
t_s((source+2),target,3);
show_ar(target,3);
return 0;
}
void t_s(int * ar1,int * ar2,int n)
{
for(int i = 0; i < n; i++)
ar2[i] = ar1[i];
}
void show_ar(int * ar,int n)
{
for(int i = 0; i < n; i++)
printf("%d ",*(ar+i));
}
CH09 Code answer 9:
#include <stdio.h>
#define ROWS 3
#define COLS 5
//void copy_ptr(int rows,int cols,double ar1[rows][cols],double ar2[rows][cols]); //编译器不支持变长数组
//void show_ar(int,int,double ar1[*][*],double ar2[*][*]);
void copy_ptr(int,int,double (*ar1)[COLS],double (*ar2)[COLS]);
void show_ar(int,int,double (*ar1)[COLS],double (*ar2)[COLS]);
int main(void)
{
double source[ROWS][COLS] = {
0,1,2,3,4,
5,6,7,8,9,
8,7,5,4,3
};
double target[ROWS][COLS];
copy_ptr(ROWS,COLS,source,target);
show_ar(ROWS,COLS,source,target);
return 0;
}
void copy_ptr(int rows,int cols,double (*ar1)[COLS],double (*ar2)[COLS])
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
ar2[i][j] = ar1[i][j];
}
void show_ar(int rows,int cols,double (*ar1)[COLS],double (*ar2)[COLS])
{
int i,j;
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
printf("%.1lf ",ar1[i][j]);
printf("\\n");
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
printf("%.1lf ",ar2[i][j]);
}
CH09 Code answer 10:
#include <stdio.h>
#define NUM 4
void three_add(int *,int *,int *,int);
void show_ar(int *,int);
int main(void)
{
int source1[NUM]={2,4,5,8};
int source2[NUM]={1,0,4,6};
int target[NUM];
three_add(source1,source2,target,NUM);
show_ar(target,NUM);
return 0;
}
void three_add(int * ar1,int * ar2, int * ar3,int num)
{
for(int i = 0; i < num; i++)
ar3[i] = ar1[i] + ar2[i];
}
void show_ar(int * ar,int num)
{
for(int i = 0; i < num; i++)
printf("%d ",ar[i]);
}
CH09 Code answer 11:
#include <stdio.h>
#define ROWS 3
#define COLS 5
void ar_ptr(int (*ar)[COLS],int); //之前莫名写成*[COLS] 注意这是错误的
void show_ar(int (*ar)[COLS],int);
int main(void)
{
int ar[ROWS][COLS] = {
1,2,3,4,5,
6,7,8,7,6,
5,4,3,2,1
};
// show_ar((*ar)[COLS],ROWS); 错误写法,只需要传送首地址
show_ar(ar,ROWS);
ar_ptr(ar,ROWS);
show_ar(ar,ROWS);
return 0;
}
void ar_ptr(int (*ar)[COLS],int rows)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < COLS; j++)
ar[i][j] *= 2;
}
void show_ar(int (*ar)[COLS],int rows)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < COLS; j++)
printf("%-3d ",ar[i][j]);
printf("\\n");
}
CH09 Code answer 12:
/* rain.c -- finds yearly totals, yearly average, and monthly
average for several years of rainfall data */
#include <stdio.h>
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
void avg_years(const float (*rain)[MONTHS]);
void avg_months(const float (*rain)[MONTHS]);
int main(void)
{
// initializing rainfall data for 2010 - 2014
const float rain[YEARS][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}
};
const float (* pi)[MONTHS]; //赋值给一个指针,当然下面用rain也行
pi = rain;
avg_years(rain);
avg_months(rain);
return 0;
}
void avg_years(const float (*rain)[MONTHS])
{
int year, month;
float subtot, total;
printf(" YEAR RAINFALL (inches)\\n");
for (year = 0, total = 0; year < YEARS; year++)
{ // for each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += *(*(rain+year)+month);
printf("%5d %15.1f\\n", 2010 + year, subtot);
total += subtot; // total for all years
}
printf("\\nThe yearly average is %.1f inches.\\n\\n",
total/YEARS);
}
void avg_months(const float (*rain)[MONTHS])
{
int year, month;
float subtot, total;
printf("MONTHLY AVERAGES:\\n\\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec\\n");
for (month = 0; month < MONTHS; month++)
{ // for each month, sum rainfall over years
for (year = 0, subtot =0; year < YEARS; year++)
subtot += *(*(rain+year)+month);
printf("%4.1f ", subtot/YEARS);
}
printf("\\n");
}
CH09 Code answer 13:
#include <stdio.h>
#include <ctype.h>
#define ROWS 3
#define COLS 5
void review_data(double (*ar)[COLS]);
double avg_one(double (*ar)[COLS],int);
double avg_all(double (*ar)[COLS]);
double find_max(double (*ar)[COLS]);
void show_msg(double (*ar)[COLS],double *,double,double);
int main(void)
{
int i,j;
double ar[ROWS][COLS];
double avg_o[ROWS];
double avg_a;
double max;
review_data(ar);
for(i = 0; i < ROWS; i++)
avg_o[i] = avg_one(ar,i);
avg_a = avg_all(ar);
max = find_max(ar);
show_msg(ar,avg_o,avg_a,max);
return 0;
}
void review_data(double (*ar)[COLS])
{
printf("please enter three * five data:\\n");
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
scanf("%lf", *(ar+i)+j ); //存入地址
// printf("%-5.1lf ", *(*(ar+i)+j) );
}
}
void show_msg(double (*ar)[COLS],double * avg_o,double avg_a,double max)
{
int i,j;
for(i = 0; i < ROWS; i++)
{
printf("\\nThe %d array data is\\n",i+1);
for(j = 0; j < COLS; j++)
{
printf("%-5.1lf",ar[i][j]);
}
}
for(i = 0; i < ROWS; i++)
printf("\\n%d array avg is %-5.1lf",i+1,avg_o[i]);
printf("\\nAll of data avg is %-5.1lf",avg_a);
printf("\\nThe max is%-5.1lf",max);
}
double avg_one(double (*ar)[COLS],int row)
{
int i;
int total = 0;
for(i = 0; i < COLS; i++)
total += ar[row][i];
return total/COLS;
}
double avg_all(double (*ar)[COLS])
{
int total;
for(int i = 0; i < ROWS; i++)
for(int j = 0; j < COLS; j++)
total += ar[i][j];
return total/ROWS/COLS;
}
double find_max(double (*ar)[COLS])
{
double max = ar[0][0];
for(int i = 0; i < ROWS; i++)
for(int j = 0; j < COLS; j++)
{
if(max < ar[i][j])
max = ar[i][j];
}
return max;
}
CH09 Code answer 14:
#include <stdio.h>
#include <ctype.h>
#define ROWS 3
#define COLS 5
void review_data(int rows,int cols,double ar[rows][cols]); //尝试变长数组
double avg_one(double (*ar)[COLS],int);
double avg_all(double (*ar)[COLS]);
double find_max(double (*ar)[COLS]);
void show_msg(double (*ar)[COLS],double *,double,double);
int main(void)
{
int i,j;
double ar[ROWS][COLS];
double avg_o[ROWS];
double avg_a;
double max;
review_data(ROWS,COLS,ar); //try
for(i = 0; i < ROWS; i++)
avg_o[i] = avg_one(ar,i);
avg_a = avg_all(ar);
max = find_max(ar);
show_msg(ar,avg_o,avg_a,max);
return 0;
}
void review_data(int rows,int cols,double ar[rows][cols]) //会报错
{
printf("please enter three * five data:\\n");
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
scanf("%lf", *(ar+i)+j ); //存入地址
// printf("%-5.1lf ", *(*(ar+i)+j) );
}
}
void show_msg(double (*ar)[COLS],double * avg_o,double avg_a,double max)
{
int i,j;
for(i = 0; i < ROWS; i++)
{
printf("\\nThe %d array data is\\n",i+1);
for(j = 0; j < COLS; j++)
{
printf("%-5.1lf",ar[i][j]);
}
}
for(i = 0; i < ROWS; i++)
printf("\\n%d array avg is %-5.1lf",i+1,avg_o[i]);
printf("\\nAll of data avg is %-5.1lf",avg_a);
printf("\\nThe max is%-5.1lf",max);
}
double avg_one(double (*ar)[COLS],int row)
{
int i;
int total = 0;
for(i = 0; i < COLS; i++)
total += ar[row][i];
return total/COLS;
}
double avg_all(double (*ar)[COLS])
{
int total;
for(int i = 0; i < ROWS; i++)
for(int j = 0; j < COLS; j++)
total += ar[i][j];
return total/ROWS/COLS;
}
double find_max(double (*ar)[COLS])
{
double max = ar[0][0];
for(int i = 0; i < ROWS; i++)
for(int j = 0; j < COLS; j++)
{
if(max < ar[i][j])
max = ar[i][j];
}
return max;
}
以上是关于C Primer Plus(第六版)第十章 编程练习答案的主要内容,如果未能解决你的问题,请参考以下文章