C primer plus第10章编程练习答案

编程练习答案是我学习过程中所敲,作为一个学渣,答案是以完成习题和易理解为导向,很少有一些五彩缤纷的算法。程序仅作为交流,如有错误与不足还请指出。
所用软件:VS2019

答案中没有第9题和第14题,原因是编译器不支持变长数组
10.10.1

/* rain.c  -- 计算每年的总降水量、年平均降水量和5年中每月的平均降水量 */
//subtot += rain[year][month];改为subtot += *(*(rain + year) + month);
#include <stdio.h>
#define MONTHS 12        // 一年的月份数
#define YEARS   5        // 年数
int main(void)
{
    // 用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 (month = 0, subtot = 0; month < MONTHS; month++)
            subtot += *(*(rain + year) + month);//修改的地方
        printf("%5d %15.1f\n", 2010 + year, subtot);
        total += subtot;  // 5年的总降水量
    }
    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++)
    {                    // 每个月,5年的总降水量
        for (year = 0, subtot = 0; year < YEARS; year++)
            subtot += *(*(rain + year) + month);
        printf("%4.1f ", subtot / YEARS);
    }
    printf("\n");

    return 0;
}

10.10.2

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

	}
}

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

10.10.3

#include "stdio.h"

int arr_max(int* arr,int n);
int main(void)
{
	int n;
	int array[] = { 1,96,3,7,6,2,4,1,3,456,4,43,1,86,4,412 };
	n = sizeof(array) / sizeof(array[0]);
	printf("数组array的最大值为%d", arr_max(array,n));

}
int arr_max(int* arr,int n)
{
	int i = 0;
	int max = 0;
	for (i = 0;i < n;i++)
	{
		if (max < arr[i])
			max = arr[i];
	}
	return max;

}

10.10.4

#include "stdio.h"

int arr_max_t(double* arr, int n);
int main(void)
{
	int n;
	double array[] = { 1.2,29.6,3.8,7.5,6.4,2.2,4.8,1.0,3.8,5.6,4.0,4.3,1.86,4.412 };//最大值为array[1] = 22.6
	n = sizeof(array) / sizeof(array[0]);
	printf("数组array的最大值为下标%d", arr_max_t(array, n));

}
int arr_max_t(double* arr, int n)
{
	int i = 0;
	int max = 0;
	int max_flag = 0;
	for (i = 0;i < n;i++)
	{
		if (max < arr[i])
		{
			max = arr[i];
			max_flag = i;
		}
			
	}
	return max_flag;

}

10.10.5

#include "stdio.h"

int arr_max_min(int* arr, int n);//题目要求为doube,这里用int,一样。
int main(void)
{
	int n;
	int array[] = { 1,96,3,7,6,2,4,1,3,456,4,43,1,86,4,412 };
	n = sizeof(array) / sizeof(array[0]);
	printf("数组array的最大值与最小值的差值为%d\n", arr_max_min(array, n));//456-1 = 455

}
int arr_max_min(int* arr, int n)
{
	int i = 0;
	int max = arr[0];//这里最好是赋一个数组里的值,否则可能出错:
	int min = arr[0];//比如min初值为0,但是数组中的min为1,这样函数执行完毕会认为数组的min为0
	for (i = 0;i < n;i++)
	{
		if (max < arr[i])
			max = arr[i];
		if (min > arr[i])
			min = arr[i];

	}
	return max - min;
}

10.10.6

#include "stdio.h"

void change(double* arr,int n);
int main(void)
{
	int i;
	int num;
	double arr[] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9 };
	num = sizeof(arr) / sizeof(arr[0]);
	
	for (i = 0;i < num;i++)
		printf("arr[%d] = %lf\n", i, arr[i]);
	change(arr, num);
	printf("交换后:\n");
	for (i = 0;i < num;i++)
		printf("arr[%d] = %lf\n", i, arr[i]);
	return 0;
}


void change(double* arr,int n)
{
	int i;
	double temp;
	for (i = 0; i < (int)(n / 2);i++)
	{
		temp = *(arr+i);
		*(arr + i) = *(arr + n - 1 - i);
		*(arr + n - 1 - i) = temp;
	}
}

10.10.7

#include "stdio.h"

copy_ptr(double* target, double* source, int n);//1维给1维
copy_ptr2(double(* target)[5], double* source, int m);//1维给2维 m是行数
int main(void)
{
	int i;
	int j;
	double source[5] = { 1.1,2.2,3.3,4.4,5.5 };
	double target1[3][5];//3*5


	copy_ptr2(target1, source, 3);

	for (i = 0;i < 5;i++)
		printf("source[%d] = %lf\n", i, source[i]);
	for (j = 0;j < 3;j++)
	{
		for (i = 0;i < 5;i++)
		{
			printf("target1[%d][%d] = %.3lf\t", j, i, target1[j][i]);
		}
		printf("\n");
	}
}
copy_ptr(double* target, double* source, int n)
{
	int i;
	for (i = 0;i < n;i++)
		*target++ = *source++;
}
copy_ptr2(double(*target)[5], double* source, int m)//1维给2维
{
	int i;
	for (i = 0;i < m;i++)
		copy_ptr(target[i], source, 5);//调用1维给1维的函数
}

10.10.8

#include "stdio.h"

copy_ptr(double* target, double* source, int n);//n为待处理元素个数
int main(void)
{
	
	int i;
	int j;
	double source[7] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7 };
	double target[3];
	copy_ptr(target, source, 3);

	for (i = 0;i < 5;i++)
		printf("source[%d] = %lf\n", i, source[i]);
	for (j = 0;j < 3;j++)
		printf("target[%d] = %lf\n", j,target[j]);
}
copy_ptr(double* target, double* source, int n)
{
	int i;
	for (i = 0;i < n;i++)
		*(target+i) = *(source+2+i);
}

10.10.10

#include "stdio.h"

void array_add(int* add, int* source1, int* source2, int n);

int main(void)
{
	int i;
	int arr1[] = { 2,4,5,8 };
	int arr2[] = { 1,0,4,6 };
	int arr3[4];
	array_add(arr3, arr1, arr2, 4);
	for (i = 0;i < 4;i++)
		printf("add3[%d] = %d\n", i, arr3[i]);

}
void array_add(int* add, int* source1, int* source2, int n)
{
	int i;
	for (i = 0;i < n;i++)
		*(add + i) = *(source1 + i) + *(source2 + i);

}

10.10.11

#include "stdio.h"

void array_display(int array[][5],int n);
void array_double(int array[][5], int n);//n为行数

int main(void)
{
	int array[3][5] = { 1,1,1,1,1,2,2,2,2,2,3,3,3,3,3 };
	array_display(array, 3);
	array_double(array, 3);
	array_display(array, 3);
}

void array_display(int array[][5], int n)
{
	int i, j;
	for (i = 0;i < n;i++)
	{
		for (j = 0;j < 5;j++)
			printf("%d\t", array[i][j]);
		printf("\n");
	}
}
void array_double(int array[][5], int n)
{
	int i, j;
	for (i = 0;i < n;i++)
		for (j = 0;j < 5;j++)
			array[i][j] = 2 * array[i][j];
}

10.10.12

/* rain.c  -- 计算每年的总降水量、年平均降水量和5年中每月的平均降水量 */
#include <stdio.h>
#define MONTHS 12        // 一年的月份数
#define YEARS   5        // 年数

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

void total_year(void);//年总降水量
void average_year(void);//年平均降水量
void average_month(void);//月平均降水量
int main(void)
{
    total_year();//年总降水量
    average_year();//年平均降水量
    average_month();//月平均降水量

    return 0;
}

void total_year(void)//年总降水量
{
    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);
    }
}
void average_year(void)//年平均降水量
{
    int year, month;
    float subtot, total;

    for (year = 0, total = 0; year < YEARS; year++)
    {                     // 每一年,各月的降水量总和
        for (month = 0, subtot = 0; month < MONTHS; month++)
            subtot += rain[year][month];
        total += subtot;  // 5年的总降水量
    }
    printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);

}
void average_month(void)//月平均降水量
{
    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++)
    {                    // 每个月,5年的总降水量
        for (year = 0, subtot = 0; year < YEARS; year++)
            subtot += rain[year][month];
        printf("%4.1f ", subtot / YEARS);
    }
    printf("\n");

 
}

10.10.13

#include "stdio.h"

void ReadArray(void);//a.读用户输入
double avr_array(double array[],int m);//b.计算每组平均值
double avr(double array[3][5]);//c.计算所有数据平均值
double max_array(double array[3][5]);//d.找出最大值
void display(void);//e.打印结果

double array[3][5];//把数组设为全局变量,这样就避免在ReadArray()函数中返回二维数组
int main(void)
{
    ReadArray();//读取数列值
	display();
}

void ReadArray(void)
{
	int i;
	printf("请输入三组数,每组5个:\n");
	for (i = 0;i < 3;i++)
	{
		printf("请输入第%d组数:",i);
		scanf_s("%lf %lf %lf %lf %lf",&array[i][0], &array[i][1], &array[i][2], &array[i][3], &array[i][4] );
		printf("\n");
	}
	return array;
}

double avr_array(double array[],int m)
{
	int i;
	double sum = 0;
	for (i = 0;i < m;i++)
		sum += array[i];
	return (sum / (double)m);
}


double avr(double array[3][5])
{
	double sum = 0;
	int i, j;
	for (i = 0;i < 3;i++)
		for (j = 0;j < 5;j++)
			sum += array[i][j];
	return (double)(sum / 15);
}	

double max_array(double array[3][5])
{
	double max = array[0][0];
	int i, j;
	for(i = 0;i < 3;i++)
		for (j = 0;j < 5;j++)
		{
			if (max < array[i][j])
				max = array[i][j];
		}
	return max;
}
void display(void)
{
	int i;
	double zu_avr[3];
	for (i = 0;i < 3;i++)
	{
		zu_avr[i] = avr_array(array[i], 5);
		printf("第%d组的平均值为:%lf\n", i, zu_avr[i]);
	}
	printf("所有数据的平均值为:%lf\n", avr(array));
	printf("所有数据的最大值为:%lf\n", max_array(array));
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值