C primer plus 中文版(第六版) 第十章课后编程答案(自己学自己编的,有错请指出)

10.1

/* rain.c  -- finds yearly totals, yearly average, and monthly
 average for several years of rainfall data */
//计算每年的总降水量,年平均降水量和 5年中每月的平均降水量    (使用指针进行计算)
#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);   //rain[year][month];
        printf("%5d %15.1f\n", 2010 + year, subtot);
        total += subtot; // total for all years 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++)
    {             // for each month, sum rainfall over years 每个月,5年的总降水量 
        for (year = 0, subtot =0; year < YEARS; year++)
            subtot += *(*(rain + year) + month);   //rain[year][month];
        printf("%4.1f ", subtot/YEARS);
    }
    printf("\n");
    
    return 0;
}

10.2

//用3种不同的方法拷贝内容至3个数组中
#include<stdio.h>
#define SIZE 5
void copy_arr(double ar[], double al[], int n);  //使用带数组表示法进行的第一份拷贝
void copy_ptr(double ar[], double al[], int n);  //使用带指针表示法和指针递增进行第二份拷贝  
void copy_ptrs(double *ar, double *start, double *end);  //以目标组函数,源树组名和指向源数组最后一个元素的指针 
int main(void)
{
	double source[SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5};
	double target1[SIZE];
	double target2[SIZE];
	double target3[SIZE];
	
	copy_arr(target1, source, SIZE);
	copy_ptr(target2, source, SIZE);
	copy_ptrs(target3, source, source + SIZE); 
	
	return 0;
} 

void copy_arr(double ar[], double al[], int n)
{
	int i;
	for(i = 0; i < n; i++)
	{
	    ar[i] = al[i];
	    printf("%5g",ar[i]);
    }
    printf("\n");
}

void copy_ptr(double ar[], double al[], int n)
{
	int i;
	for(i = 0; i < n; i++)
	{
		*(ar + i) = *(al + i);
		printf("%5g",*(ar + i));
	}
	printf("\n");
}  

void copy_ptrs(double *ar, double *start, double *end)
{
	while(start < end)
	{
        *ar = *start;
		printf("%5g",*ar);
		*ar++;
		*start++;
	}
}

10.3

​//函数返回int类型数组中的最大值
#include<stdio.h>
#define LEN 10
 
int max_arr(const int ar[], int n);  //函数声明 
void show_arr(const int ar[], int n);

int main(void)
{
	int origin[LEN] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  //初始化数组
	int original[LEN] = {10, 34, 23, 98, 26, 36, 16, 59, 29, 55};
	int max;
	
	show_arr(origin, LEN);   //显示数组
	max = max_arr(origin, LEN);   //得出最大值
	printf("\nlargest value = %d\n",max);
	
	show_arr(original, LEN);
	max = max_arr(original, LEN);
	printf("\nlargest value = %d\n",max);
	
	return 0; 
}

void show_arr(const int ar[], int n)
{
	int i;
	for(i = 0; i < n; i++)
	    printf("%5d",ar[i]);
}

int max_arr(const int ar[], int n)
{
	int max = 0;;
	int i;
	for(i = 0; i < n; i++)
	{
		if(ar[i] > max)
		    max = ar[i];
	}
	return max;
}
​

10.4

//返回double类型数组中的最大值的下标
#include<stdio.h>
#define LEN 10

int  max_arr(const double [], int);   //函数声明 
void show_arr(const double [], int); 

int main(void)
{
	double origin[LEN] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1};  //数组初始化
	double ori[LEN] = {9.5, 3.4, 6.9, 1.3, 5.6, 3.6, 9.6, 7.4, 4.7, 2.4};
	int max;
	
	show_arr(origin, LEN);
	max = max_arr(origin, LEN);
	printf("\nThe under of largest value ia %d.\n",max);
	
	show_arr(ori, LEN);
	max = max_arr(ori, LEN);
	printf("\nThe under of largest value ia %d.\n",max);
	
	return 0; 
} 

void show_arr(const double ar[], int n)
{
	int i;
	for(i = 0; i < n; i++)
	    printf("%5g",ar[i]);
	printf("\n");
}

int max_arr(const double ar[], int n)
{
	int i;
	double max = 0;
	int under = 0;
	for(i = 0; i < n; i++)
	{
		if(max < ar[i])
		{
			max = ar[i];
			under = i;
		}
	}
	return under;
}

10.5

//返回double类型数组中的最大值和最小值的差值 
#include<stdio.h>
#define LEN 10

double minus_arr(const double [], int);   //函数声明 
void show_arr(const double [], int); 

int main(void)
{
	double origin[LEN] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1};  //数组初始化
	double ori[LEN] = {9.5, 3.4, 6.9, 1.3, 5.6, 3.6, 9.6, 7.4, 4.7, 2.4};
	double minus;
	
	show_arr(origin, LEN);
	minus = minus_arr(origin, LEN);
	printf("\nThe minus between largest and smallest ia %g.\n",minus);  //转移说明很重要 
	
	show_arr(ori, LEN);
	minus = minus_arr(ori, LEN);
	printf("\nThe minus between largest and smallest ia %g.\n",minus);
	
	return 0; 
} 

void show_arr(const double ar[], int n)   //显示数组 
{
	int i;
	for(i = 0; i < n; i++)
	    printf("%5g",ar[i]);
	printf("\n");
}

double minus_arr(const double ar[], int n)  //求最大值与最小值的差值 
{ 
	int i;
	double max = ar[0];
	double min = ar[0];
	double minus;
	for(i = 0; i < n; i++)
	{
		if(max < ar[i])
			max = ar[i];
		if(min > ar[i])
		    min = ar[i];
	}
	printf("\nmax = %g\n",max);
	printf("\nmin = %g\n",min);
	minus = max - min;
	printf("\nminud = %g\n",minus);
	return minus;
}

10.6

//将double类型数组中的数据倒序排列
#include<stdio.h>
#define LEN 5

void show_arr(double ar[], int n);  //函数声明  要倒序排列,所以不能使用const 
void reverse_arr(double ar[], int n);

int main(void)
{
	double ori[LEN] = {5.5, 9.3, 3.4, 8.4, 1.5};
	
	show_arr(ori, LEN); //调用函数
	reverse_arr(ori, LEN);
	show_arr(ori, LEN);
	
	return 0; 
} 

void show_arr(double ar[], int n)
{
	int i;
	for(i =0; i < n; i++)
	    printf("%5g",ar[i]);
	printf("\n"); 
}

void reverse_arr(double ar[], int n)
{
	int i = 0, j = n - 1 - i;
	double temp;
	//for(i = 0; i < n/2; i++)
	//{
		//temp = ar[i];
		//ar[i] = ar[n - 1 - i];
		//ar[n - 1 - i] = temp;
	  
	//}
	while(i < j)
	{
		temp = ar[i];
		ar[i] = ar[j];
		ar[j] = temp;
		i++;
		j--;
	}
}

10.7

//拷贝double类型的二维数组
#include<stdio.h>
#define ROWS 3
#define COLS 2

void copy_arr(double ar1[], double ar2[], int n);   //拷贝数组函数
void show_arr(double ar[][COLS], int n);    //打印数组函数

int main(void)
{
	int rows;
	double target[ROWS][COLS];
	double source[ROWS][COLS] = { {1.1, 2.2}, {3.3, 4.4}, {5.5, 6.6} };  //初始化数组 
	
	printf("The original array:\n"); 
	show_arr(source, ROWS);    //打印原始数组
	for(rows = 0; rows < ROWS; rows++)
	    copy_arr(target[rows], source[rows], ROWS);   //拷贝数组
	printf("The copied array:\n");
	show_arr(target,ROWS);    //打印拷贝数组 
	
	return 0; 
} 

void copy_arr(double ar1[], double ar2[], int n)
{
	int i;
	for(i = 0; i < n; i++)
	    ar1[i] = ar2[i];
}

void show_arr(double ar[][COLS], int n)
{
	int i, j;
	for(i = 0; i < n; i++)
	{
	    for(j = 0; j < COLS; j++)
	        printf("%5g",ar[i][j]);
	    printf("\n");
	}
}

10.8

//将一个内含7个元素的数组中的第3~5个元素拷贝至内含3个元素的数组中
#include<stdio.h>
#define LEN1 7
#define LEN2 3

void copy_arr(int ar1[], const int ar2[], int n);   //拷贝函数声明
void show_arr(const int ar[], int n);   //打印函数声明

int main(void)
{
	int orig[LEN1] = {1, 2, 3, 4, 5, 6, 7};
	int copy[LEN2];
	
	show_arr(orig, LEN1);
	copy_arr(copy, orig, 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 + 2];
}

void show_arr(const int ar[], int n)
{
	int i;
	for(i =0; i < n; i++)
        printf("%5d",ar[i]);
    printf("\n");
}

10.9

//使用变长数组函数拷贝并显示3乘5的二维数组
#include<stdio.h>
#define ROWS 3
#define COLS 5

void copy_arr(int rows, int cols, double ar1[rows][cols], double ar2[rows][cols]);  //拷贝函数
void show_arr(int rows, int cols, double ar[rows][cols]);   //打印函数

int main(void)
{
    int i, j;
	int rows = 3;
	int cols = 5;
	double source[rows][cols];
	double target[rows][cols];

	for(i = 0; i < rows; i++)
        for(j = 0; j < cols; j++)
            source[i][j] = i * j + 1.1;  //初始化变长数组

    printf("The original array:\n");
    show_arr(rows,cols,source);     //打印原始数组
	copy_arr(rows,cols,target,source);   //拷贝数组
	printf("The copied array:\n");
	show_arr(rows,cols,target);    //打印复制数组

    return 0;
}

void copy_arr(int rows, int cols, double ar1[rows][cols], double ar2[rows][cols])
{
	int i, j;
	for(i =0; i < rows; i++)
	    for(j = 0; j < cols; j++)
	        ar1[i][j] = ar2[i][j];
}

void show_arr(int rows, int cols, double ar[rows][cols])
{
	int i, j;
	for(i = 0; i < rows; i++)
	{
        for(j =0; j < cols; j++)
            printf("%5g",ar[i][j]);
        printf("\n");
    }
}

10.10

//将两个数组中的对应元素相加,将结果存储到第三个数组中
#include <stdio.h>
#define LEN 4

void show_arr(const int ar[], int n);   //打印数组函数
void sum_arr(const int ar1[], const int ar2[], int ar3[], int n);   //数组相加函数

int main()
{
    int source1[LEN] = {2, 4, 5, 8};
    int source2[LEN] = {1, 0, 4, 6};
    int target[LEN];

    show_arr(source1, LEN);  //打印原数组
    show_arr(source2, LEN);
    sum_arr(source1, source2, target, LEN);  //数组相加函数
    printf("The sum of the array:\n");
    show_arr(target, LEN);   //打印相加数组

    return 0;
}

void show_arr(const int ar[], int n)
{
    int i;
    for(i = 0; i < n; i++)
        printf("%5d",ar[i]);
    printf("\n");
}

void sum_arr(const int ar1[], const int ar2[], int ar3[], int n)
{
    int i;
    for(i = 0; i < n; i++)
        ar3[i] = ar1[i] + ar2[i];
}

10.11

//打印一个int类型的3乘5二维数组,然后将其翻倍
#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 5

void show_arr(int ar[][COLS], int n);
void double_arr(int ar[][COLS], int n);

int main()
{
    int source[ROWS][COLS] =
    {
        {1, 2, 3, 4, 5},
        {2, 3, 4, 5, 6},
        {3, 4, 5, 6, 7}
    };
    printf("The original array:\n");
    show_arr(source, ROWS);   //打印原始数组
    double_arr(source, ROWS);    //翻倍数组
    printf("The double array;\n");
    show_arr(source, ROWS);    //打印翻倍数组

    return 0;
}

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

void double_arr(int ar[][COLS], int n)
{
    int i, j;
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < COLS; j++)
            ar[i][j] *= 2;
    }
}

10.12

//重写程序清单10.7 rain.c程序
#include <stdio.h>
#define MONTHS 12    // number of months in a year
#define YEARS   5    // number of years of data

//年总降水量和年平均降水量
void sum_year(const float ar[][MONTHS], int n);
//5年中每个月的平均降水量
void average_month(const float ar[][MONTHS], int n);
//打印函数
void show_arr(const float ar[][MONTHS], int n);

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

    printf("The whole number is \n");
    show_arr(rain, YEARS);  //打印函数
    sum_year(rain, YEARS);  //年总降水量即平均降水量
    average_month(rain, YEARS);   //每月的平均降水量

    return 0;
}

void show_arr(const float ar[][MONTHS], int n)
{
    int i, j;
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < MONTHS; j++)
            printf("%5g", ar[i][j]);
        printf("\n");
    }
}

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

void average_month(const float ar[][MONTHS], int n)
{
    int i, j;
    float subtot;
    printf("MONTHLY AVERAGES:\n\n");
    printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec\n");
    for(j = 0; j < MONTHS; j++)
    {
        for(i = 0, subtot = 0; i < YEARS; i++)
            subtot += ar[i][j];
        printf("%4.1f ", subtot/YEARS);
    }
    printf("\n");
}

10.13

//提示输入3组数据,然后将其存储到3乘5的数组中
#include <stdio.h>
#define ROWS 3
#define COLS 5
#define NUM 15

void get_num(double ar[][COLS], int n);  //将用户输入的数存储到数组中
void average_row(double ar[][COLS], int n);   //计算每组数据的平均值
double average_arr(double ar[][COLS], int n);  //计算所有数的平均值
double max_arr(double ar[][COLS], int n);  //找出所有元素的最大值
void show_arr(double ar[][COLS], int n);  //打印函数

int main()
{
    double array[ROWS][COLS];
    double average;
    double maximum;

    get_num(array, ROWS);   //将数据输入数组中
    printf("The input of the array:\n");
    show_arr(array, ROWS);   //打印数组
    average_row(array, ROWS);   //每组数据的平均值
    average = average_arr(array, ROWS);  //所有元素的平均值
    printf("\nThe average of the array is %g.\n", average);
    maximum = max_arr(array, ROWS);   //找出所有元素的最大值
    printf("\nThe maximum of the array is %g.\n",maximum);

    return 0;
}

void get_num(double ar[][COLS], int n)   //将数据输入数组中
{
    int i, j;
    printf("Please enter three groups of numbers, and each group haves 5 numbers.\n");

    for(i = 0; i < n; i++)
        for(j = 0; j < COLS; j++)
            scanf("%lf", &ar[i][j]);

    printf("\n");
}

void average_row(double ar[][COLS], int n)  //计算每组数组的平均值
{
    int i, j;
    double subtot;

    for(i = 0; i < n; i++)
    {
        for(j = 0, subtot = 0; j < COLS; j++)
            subtot += ar[i][j];
        printf("The average of the %d group is %g.\n", i + 1, subtot / COLS);
    }
    printf("\n");
}

double average_arr(double ar[][COLS], int n)   //计算所有元素的平均值
{
    int i, j;
    double total = 0;

    for(i = 0; i < n; i++)
        for(j =0; j < COLS; j++)
            total += ar[i][j];

    return total / (ROWS * COLS);
}

double max_arr(double ar[][COLS], int n)   //找出所有元素的最大值
{
    int i, j;
    double max = ar[0][0];

    for(i = 0; i < n; i++)
        for(j = 0; j < COLS; j++)
            if(max < ar[i][j])
                max = ar[i][j];
    printf("\nThe maximum is %g.\n",max);

    return max;
}

void show_arr(double ar[][COLS], int n)  //打印数组
{
    int i, j;

    for(i = 0; i < n; i++)
    {
        for(j = 0; j < COLS; j++)
            printf("%5g", ar[i][j]);
        printf("\n");
    }
    printf("\n");
}

10.14

//使用变长数组重做10.13
#include <stdio.h>
#include <stdlib.h>

void get_num(int rows, int cols, double ar[rows][cols]);  //将用户输入的数存储到数组中
void average_row(int rows, int cols, double ar[rows][cols]);   //计算每组数据的平均值
double average_arr(int rows, int cols, double ar[rows][cols]);  //计算所有数的平均值
double max_arr(int rows, int cols, double ar[rows][cols]);  //找出所有元素的最大值
void show_arr(int rows, int cols, double ar[rows][cols]);  //打印函数


int main()
{
    int rows = 3, cols = 5;
    double array[rows][cols];
    double average;
    double maximum;

    get_num(rows, cols, array);   //将数据输入数组中
    printf("The input of the array:\n");
    show_arr(rows, cols, array);   //打印数组
    average_row(rows, cols, array);   //每组数据的平均值
    average = average_arr(rows, cols, array);  //所有元素的平均值
    printf("\nThe average of the array is %g.\n", average);
    maximum = max_arr(rows, cols, array);   //找出所有元素的最大值
    printf("\nThe maximum of the array is %g.\n",maximum);

    return 0;
}

void get_num(int rows, int cols, double ar[rows][cols])   //将数据输入数组中
{
    int i, j;
    printf("Please enter three groups of numbers, and each group haves 5 numbers.\n");

    for(i = 0; i < rows; i++)
        for(j = 0; j < cols; j++)
            scanf("%lf", &ar[i][j]);

    printf("\n");
}

void average_row(int rows, int cols, double ar[rows][cols])  //计算每组数组的平均值
{
    int i, j;
    double subtot;

    for(i = 0; i < rows; i++)
    {
        for(j = 0, subtot = 0; j < cols; j++)
            subtot += ar[i][j];
        printf("The average of the %d group is %g.\n", i + 1, subtot / cols);
    }
    printf("\n");
}

double average_arr(int rows, int cols, double ar[rows][cols])   //计算所有元素的平均值
{
    int i, j;
    double total = 0;

    for(i = 0; i < rows; i++)
        for(j =0; j < cols; j++)
            total += ar[i][j];

    return total / (rows * cols);
}

double max_arr(int rows, int cols, double ar[rows][cols])   //找出所有元素的最大值
{
    int i, j;
    double max = ar[0][0];

    for(i = 0; i < rows; i++)
        for(j = 0; j < cols; j++)
            if(max < ar[i][j])
                max = ar[i][j];
    printf("\nThe maximum is %g.\n",max);

    return max;
}

void show_arr(int rows, int cols, double ar[rows][cols])  //打印数组
{
    int i, j;

    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < cols; j++)
            printf("%5g", ar[i][j]);
        printf("\n");
    }
    printf("\n");
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值