# c primerplus 第十章编程题答案

//第一题
#include <stdio.h>
#include <stdlib.h>
#define MONTHS 12
#define YEARS  5
int main(void)
{
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;
printf(" YEAR   RAINFALL  (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += *(*(rain + year) + month); //注意1
printf("%5d %15.1f\n", 2010 + year, subtot);
total += subtot;
}
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 (year = 0, subtot = 0; year < YEARS; year++)
subtot += *(*(rain + year) + month);//注意2
printf("%4.1f ", subtot / YEARS);
}
printf("\n");
return 0;
}

//第二题
#include <stdio.h>
#include <stdlib.h>
void copy_arr(double *target1, double *source, int i);
void copy_ptr(double *target2, double *source, int i);
void copy_ptrs(double *target3, double *source, double *i);
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);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, source + 5);

int i;
for(i = 0; i < 5; i++)
printf("%.1f ", target1[i]);
printf("\n");
for(i = 0; i < 5; i++)
printf("%.1f ", target2[i]);
printf("\n");
for(i = 0; i < 5; i++)
printf("%.1f ", target3[i]);
printf("\n");

}

void copy_arr(double *target1, double *source, int i){
int n;
for(n = 0; n < i; n++)
target1[n] = source[n];
}

void copy_ptr(double *target2, double *source, int i){
int n;
for(n = 0; n < i; n++)
*(target2 + n) = *(source + n);
}
//没搞懂给第二个参数有什么用，只用第一个和第三个参数也能做出来
void copy_ptrs(double *target3, double *source, double *i){
int n;
for(n = 4; n >= 0; n--){
*(target3 + n) = *(--i); //注意是--i
}
}

//第三题
#include <stdio.h>
#define h 5
int return_max(int a[], int i);
int main(void)
{
int s;
int a[h] = {2, 4, 1, 9, 5};
s = return_max(a, h);
printf("max = %d", s);
}

int return_max(int *a, int i){
int max = *a;
for(; i > 0; i--){
if(a[i] > max)
max = a[i];
}
return max;
}

//第四题
#include <stdio.h>
#define h 6
int return_max(double *a, int i);
int main(void)
{
double a[h] = {2.9, 4, 1, 9.8, 5.1, 9.7};
printf("max = %d", return_max(a, h));
}

int return_max(double *a, int i){
double max = *a;
int f = 0;
for(; i > 0; i--){
if(a[i] > max){
max = a[i];
f = i;
}
}
return f;
}

//第五题
#include <stdio.h>
#define h 6
double return_differ(double *a, int i);
int main(void)
{
double a[h] = {2.9, 4, 1, 9.8, 5.1, 9.7};
printf("differ = %lf", return_differ(a, h));
}

double return_differ(double *a, int i){
double max = *a, min = *a;
int n;
for(n = 1; n < i; n++){
if(a[n] > max)
max = a[n];

if(a[n] < min)
min = a[n];
}
return (max - min);
}

//第六题
#include <stdio.h>
#define h 6
double reverse_order(double *a, int i);
int main(void)
{
double a[h] = {2.9, 4, 1, 9.8, 5.1, 9.7};
int i;
reverse_order(a, h);
for(i = 0; i < h; i++)
printf("a[%d] = %lf\n", i, a[i]);

return 0;
}

double reverse_order(double *a, int i){
int n, j;
double t = 0;
for(n = 0, j = i - 1; n < i / 2; n++, j--){
t = a[n];
a[n] = a[j];
a[j] = t;
}

}

//第七题
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
//注意下面的第三个变量使用N而不能用YEARS，不然相当于使用了一个常量
void copy(double rain[][MONTHS], double s[][MONTHS], int N);
int main(void)
{
int i, j;
double 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 }
};

double s[YEARS][MONTHS];
copy(rain, s, YEARS);

for(i = 0; i < YEARS; i++){
for(j = 0; j < MONTHS; j++){
printf("%.1f ", s[i][j]);
}
printf("\n");
}
return 0;
}

void copy(double rain[][MONTHS], double s[][MONTHS], int N){
int i, j;
for(i = 0; i < YEARS; i++){
for(j = 0; j < MONTHS; j++){
s[i][j] = rain[i][j];
}
}

}

//第八题
#include <stdio.h>
#define n 7
void copy(int *a, int *b, int num);
int main(void)
{
int a[n] = {0, 1, 2, 3, 4, 5, 6};
int b[3];
copy( a, b, 3);
int i;
for( i = 0; i < 3; i++)
printf("%d ", b[i]);
}

void copy(int *a, int *b, int num){
int i;
for(i = 0; i < num; i++)
*(b + i) = *(a + 3 + i);
}

//第九章
#include <stdio.h>
#define N 3
#define M 5
void copy(int n, int m, double a[n][m], double b[n][m]);
void show(int n, int m, double a[n][m]);//先写n、m，不然会报错
int main(void)
{
double a[N][M] =
{
{ 4.3, 4.3, 4.3, 3.0, 2.0 },
{ 8.5, 8.2, 1.2, 1.6, 2.4 },
{ 9.1, 8.5, 6.7, 4.3, 2.1 },
{ 7.2, 9.9, 8.4, 3.3, 1.2 },
{ 7.6, 5.6, 3.8, 2.8, 3.8 }
};
double b[N][M];
copy(N, M, a, b); //传递地址（为什么函数不用使用地址，不懂）
show(N, M, a);
printf("\n");
show(N, M, b);

return 0;
}

void copy(int n, int m, double a[n][m], double b[n][m]){ //数组里写设定的形参，不能写前面定义的N、M
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
b[i][j] = a[i][j];
}
}
}

void show(int n, int m, double a[n][m]){
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
printf("%.2f ", a[i][j]);
}
printf("\n");
}
}

#第十题
#include <stdio.h>
#define n 4
void f(int *a, int *b, int *c, int contain);
int main(void)
{
int i;
int a[n] = {2, 4, 5, 8};
int b[n] = {1, 0, 4, 6};
int c[n];

f(a, b, c, n);

for(i = 0; i < n; i++)
printf("%d ", c[i]);

return 0;
}

void f(int *a, int *b, int *c, int contain){
int i;

for(i = 0; i < contain; i++)
c[i] = a[i] + b[i];
}

//第十一题
#include <stdio.h>
void pr(int a[][5], int row);
void db(int a[][5], int row);
int main(void)
{
int a[3][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

pr(a, 3);//传地址！（还是不懂为什么，想明白再来改）
db(a, 3);
pr(a, 3);
db(a, 3);

return 0;
}

void pr(int a[][5], int row){
int i, j;
for(i = 0; i < row; i++){
for(j = 0; j < 5; j++){
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\n");
}

void db(int a[][5], int row){
int i, j;
for(i = 0; i < row; i++){
for(j = 0; j < 5; j++){
a[i][j] *= 2;
}
}
}

//第十二题
#include <stdio.h>
#include <stdlib.h>
#define MONTHS 12
#define YEARS  5
void yearaver(const float rain[YEARS][MONTHS]);
void monthaver(const float rain[YEARS][MONTHS]);
int main(void)
{
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 }
};
yearaver(rain);
monthaver(rain);
return 0;
}

void yearaver(const float rain[YEARS][MONTHS])
{
int year, month;
float subtot, total;
printf(" YEAR   RAINFALL  (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += *(*(rain + year) + month);
printf("%5d %15.1f\n", 2010 + year, subtot);
total += subtot;
}
printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);
}

void monthaver(const float rain[YEARS][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 (year = 0, subtot = 0; year < YEARS; year++)
subtot += *(*(rain + year) + month);
printf("%4.1f ", subtot / YEARS);
}
printf("\n");
}

//第十三题
#include <stdio.h>
#define row 3
#define col 5
void a(double m[][col], int a);
double b(double m[][col], int i);
double c(double m[][col], int n);
double d(double m[][col], int n);
void e(double m[][col], int n);
int main(void)
{
double m[row][col];
int i;
printf("请输入3X5组数据:\n");
a(m, row);

for(i = 0; i < 3; i++)
b(m, i);

c(m, i);
d(m, row);
e(m, row);

return 0;
}

void a(double m[][col], int a){
int i, j;
for(i = 0; i < a; i++){
for(j = 0; j < col; j++){
scanf("%lf", &m[i][j]);
}
}
}

double b(double m[][col], int i){
int j;
double s = 0;
for(j = 0; j < col; j++){
s += m[i][j];
}

return s/col;
}

double c(double m[][col], int n){
int i, j;
double s = 0;
for(i = 0; i < n; i++){
for(j = 0; j < col; j++){
s += m[i][j];
}
}

return s/(n * col);
}

double d(double m[][col], int n){
double max = m[0][0];
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < col; j++){
if(m[i][j] > max)
max = m[i][j];
}
}

return max;
}

void e(double m[][col], int n){
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < col; j++){
printf("%lf ", m[i][j]);
}
printf("\n");
}
printf("\n");

for(i = 0; i < n; i++)
printf("第%d组的平均值为%lf\n", i, b(m, i));

printf("\n");

printf("全部平均值为%lf\n", c(m, i));

printf("max = %lf\n", d(m, n));
}

//第十四题
//第十三题 每个函数换成三个变量

08-09 4332

06-01 3570
10-02 5395
08-03 676
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试