C Primer Plus第6版 第10章 练习答案

我的博客主要记录一下自己的学习代码过程。
大家可以在自己电脑上运行看看。
编译器为VS2019
第10章的运行结果如下:
10.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;

    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);
    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");

    return 0;
}

运行结果:在这里插入图片描述

10.2


#include <stdio.h> 
void copy_arr(double target1[], double source[], int n);
void copy_ptr(double* target2, double* source, int n);
void copy_ptrs(double* target3, double* source, double* source_plus5);
int main(void)
{
    int i;
    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);
    return 0;
}
void copy_arr(double target1[], double source[], int n)
{
    int i;
    for (i = 0;i < n;i++)
    {
        target1[i] = source[i];
        printf("target1[%d] = %.2lf ", i, target1[i]);
    }
    printf("\n");
}
void copy_ptr(double* target2, double* source, int n)
{
    int i;
    for (i = 0;i < n;i++)
    {
        *(target2 + i) = source[i];
        printf("target2[%d] = %.2lf ", i, target2[i]);
    }
    printf("\n");
}
void copy_ptrs(double* target3, double* source, double* source_plus5)
{
    int i=0;
    for(i=0;i<(source_plus5-source);i++)
    {
        *(target3 + i) = source[i];
        printf("target3[%d] = %.2lf ", i, target3[i]);
    }
}

运行结果:
在这里插入图片描述
10.3

#include <stdio.h> 
#define size 20
void test(int n,int ar[]);
int main(void)
{
    int test1[size] = { 1,3,5,8,9,1,20,56,98,12,32 };
    test(size,test1);
    return 0;
}
void test(int n,int ar[])
{
    int i, max;
    max = ar[0];
    for (i = 0;i < n;i++)
    {
        if (max < ar[i])
            max = ar[i];
    }
    printf("数组里面最大的数为%d", max);
}

运行结果:
在这里插入图片描述
10.4


#include <stdio.h> 
#define size 20
void test(int n,double ar[]);
int main(void)
{
    double test1[size] = { 1,3,5,8,9,1,20,56,98,12,32,98.6,99.9,63.5};
    test(size,test1);
    return 0;
}
void test(int n,double ar[])
{
    int i, max1;
    double max;
    max = ar[0];
    max1 = 0;
    for (i = 0;i < n;i++)
    {
        if (max < ar[i])
        {
            max = ar[i];
            max1 = i;
        }
    }
    printf("数组里面最大的数为%.2lf,它的下标为%d", max,max1);
}

运行结果:
在这里插入图片描述
10.5


#include <stdio.h> 
#define size 20
void test(int n,double ar[]);
int main(void)
{
    double test1[size] = { 1,3,5,8,9,1,20,56,98,12,32,98.6,99.9,63.5};
    test(size,test1);
    return 0;
}
void test(int n,double ar[])
{
    int i, max1,min1;
    double max,min;
    max = min = ar[0];
    max1 = min1 = 0;
    for (i = 0;i < n;i++)
    {
        if (max < ar[i])
        {
            max = ar[i];
            max1 = i;
        }
        if (min > ar[i])
        {
            min = ar[i];
            min1 = i;
        }
    }
    printf("数组里面最大的数为%.2lf,它的下标为%d,数据里面最小的数为%.2lf,它的下标为%d,它们的差值为%.2lf", max,max1,min,min1,max-min);
}

运行结果:
在这里插入图片描述
10.6


#include <stdio.h> 
#define size 14
void test(int n,double ar[]);
int main(void)
{
    int i;
    double test1[size] = { 1,3,5,8,9,1,20,56,98,12,32,98.6,99.9,63.5};
    test(size,test1);
    for (i = 0;i < size;i++)
        printf("test1[%d]=%.2lf\n", i, test1[i]);
    return 0;
}
void test(int n,double ar[])
{
    int i,y;
    double x;
    y = n / 2;
    for (i = 0;i <= y;i++)
    {
        x = ar[i];
        ar[i] = ar[i + n - 1];
        ar[i + n - 1] = x;
        n -= 2;//保证ar[i+n-1]每次递减1
    }
}

运行结果:
在这里插入图片描述
10.7

#include <stdio.h> 
void copy_arr(double target1[][5], double source[][5], int n);
int main(void)
{
    int i,j;
    double source [2][5] = { {1.1,2.2,3.3,4.4,5.5},{3.5,5.2,4.9} };
    double target1[2][5];
    copy_arr(target1, source, 2);
    for (i = 0;i < 2;i++)
    {
        for (j = 0;j < 5;j++)
            printf("target1[%d][%d]=%.2lf   ", i, j, target1[i][j]);
        printf("\n");
    }
    return 0;
}
void copy_arr(double target1[][5], double source[][5], int n)
{
    int i, j;
    for (i = 0; i < n; i++)
        for (j = 0; j < 5; j++)
            target1[i][j] = source[i][j];
}

运行结果:
在这里插入图片描述
10.8

#include <stdio.h> 
void copy_arr(double target1[], double source[], int n);
int main(void)
{
    int i;
    double source[7] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7 };
    double target1[3];
    copy_arr(target1, source, 3);
    return 0;
}
void copy_arr(double target1[], double source[], int n)
{
    int i;
    for (i = 0;i < n;i++)
    {
        target1[i] = source[i + 2];
        printf("target1[%d] = %.2lf ", i, target1[i]);
    }
    printf("\n");
}

运行结果:
在这里插入图片描述
10.9
不知道什么原因,我的VS2019似乎不能运行变长数组,所以这里使用的是传统的NX5数组。

#include <stdio.h> 
#define Rows 3
#define Cols 5
void copy_arr(int n, double target1[][5], double source[][5]);
void show_arr(int n,double target1[][5], double source[][5]);
int main(void)
{
    int i;
    double source[3][5] = { {1.1,2.2,3.3,4.4,5.5},{6.6,7.7,8.8,9.9,10.10},{11.11,12.12,13.13,14.14,15.15} };
    double target1[Rows][5];
    copy_arr(Rows,target1, source);
    show_arr(Rows, target1, source);
    return 0;
}
void copy_arr(int n, double target1[][5], double source[][5])
{
    int i, j;
    for (i = 0; i < n; i++)
        for (j = 0; j < 5; j++)
            target1[i][j] = source[i][j];
    return;
}
void show_arr(int n,double target1[][5], double source[][5])
{
    int i, j;
    puts("源数组为:");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 5; j++)
            printf("%8.2lf", source[i][j]);
        if (i >= 0)
            printf("\n");
    }
    puts("目标数组为:");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 5; j++)
            printf("%8.2lf", target1[i][j]);
        if (i >= 0)
            printf("\n");
    }
}

运行结果:
在这里插入图片描述
10.10

#include <stdio.h> 
#define N 4
void sum(int n, double ar[], double ar1[],double result[]);
void show_arr(int n, double ar[], double ar1[], double result[]);
int main(void)
{
    int i;
    double ar_one[N] = {2,4,5,8 };
    double ar_two[N]= {1,0,4,6 };
    double ar_result[N];
    sum(N,ar_one,ar_two,ar_result);
    show_arr(N, ar_one, ar_two, ar_result);
    return 0;
}
void sum(int n, double ar[], double ar1[], double result[])
{
    int i, j;
    for (i = 0; i < n; i++)
            result[i] = ar1[i]+ar[i];
}
void show_arr(int n, double ar[], double ar1[], double result[])
{
    int i;
    puts("数组1为:");
    for (i = 0; i < n; i++)
            printf("%8.2lf", ar[i]);
    printf("\n");
    puts("数组2为:");
    for (i = 0; i < n; i++)
            printf("%8.2lf", ar1[i]);
    printf("\n");
    puts("数组3为:");
    for(i=0;i<n;i++)
        printf("%8.2lf", result[i]);
}

运行结果:
在这里插入图片描述
10.11

#include <stdio.h> 
#define N 3
#define M 5
void sum(int n, int ar[][M]);
void show_arr(int n, int ar[][M]);
int main(void)
{
    int i,j;
    int ar_one[N][M] = { {1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15} };
    puts("数组1为:");
    show_arr(N, ar_one);
    puts("数组1内的值翻倍后为:");
    sum(N, ar_one);
    show_arr(N, ar_one);
    return 0;
}
void sum(int n, int ar[][M])
{
    int i, j;
    int ar1[N][M];
    for (i = 0;i < n;i++)
    {
        for (j = 0;j < M;j++)
        {
            ar1[i][j] = ar[i][j] * 2;
            ar[i][j] = ar1[i][j];
        }
    }
}
void show_arr(int n, int ar[][M])
{
    int i, j;
    for (i = 0;i < n;i++)
    {
        for (j = 0;j < M;j++)
            printf("%5d", ar[i][j]);
        printf("\n");
    }
}

运行结果:
在这里插入图片描述
10.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
float each_month_sum(int n, const float ar[][MONTHS]);//每一年,各月的降水量总和
void each_year_ave(int n, const float ar[][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}
    };
    float total;
    total = each_month_sum(YEARS, rain);
    printf("\nThe yearly average is %.1f inches.\n\n",
        total / YEARS);
    each_year_ave(YEARS, rain);
    return 0;
}

float each_month_sum(int n, const float ar[][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 += *(*(ar + year) + month);
        printf("%5d %15.1f\n", 2010 + year, subtot);
        total += subtot; // total for all years
    }
    return total;
}
void each_year_ave(int n, const float ar[][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 += *(*(ar+ year) + month);
        printf("%4.1f ", subtot / YEARS);
    }
    printf("\n");
}

运行结果:
在这里插入图片描述
10.13

#include <stdio.h>
void input_test(int n, double ar[][5]);
double compute_test(int n, double ar[][5]);
double all_ave_test(int n, double ar[][5]);
double max_test(int n, double ar[][5]);
int main(void)
{
	puts("请输入3组数,每组数包含5个double类型的数.");
	double test[3][5];
	input_test(3, test);
	puts("计算每组(5个)数据的平均值。");
	double average;
	int i;
	for (i = 0;i < 3;i++)
	{
		average = compute_test(i, test);
		printf("第%d组数据的平均值为%.2lf.\n", i, average);
	}  
	double all_average;
	all_average = all_ave_test(3, test);
	printf("所有数据的平均值为%.2lf.\n", all_average);
	double max_Test;
	max_Test = max_test(3, test);
	printf("这15个数据中的最大值为%.2lf.\n", max_Test);
	return 0;
}

void input_test(int n, double ar[][5])
{
	int i,j;
	for (i = 0;i < 3;i++)
		for (j = 0;j < 5;j++)
			scanf_s("%lf", ar[i] + j);
}
double compute_test(int n, double ar[][5])
{
	double x;
	x = 0;
	int i;
	for (i = 0;i < 5;i++)
		x += ar[n][i];
	return x / 5;
}
double all_ave_test(int n, double ar[][5])
{
	double x=0.0;
	int i, j;
	for (i = 0;i < n;i++)
		for (j = 0;j < 5;j++)
			x += ar[i][j];
	return x / 15;
}
double max_test(int n, double ar[][5])
{
	double max;
	int i, j;
	max = ar[0][0];
	for(i = 0;i < n;i++)
		for (j = 0;j < 5;j++)
		{
			if (max < ar[i][j])
				max = ar[i][j];
		}
	return max;
}

运行结果:
在这里插入图片描述
第二次运行结果:
在这里插入图片描述
10.14 因为我的编译器不能使用变长数组,所以没有编写了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值