C Primer Plus (第六版)中文版 第十章 编程练习答案

  1. 修改程序清单10.7中的程序rain.c ,用指针进行计算(仍然需要声明并初始化数组)。
#include <stdio.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);
        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);
        printf("%4.1f ", subtot/YEARS);
    }
    printf("\n");

    return 0;
}

2.编写一个程序,初始化一个double类型的数组,然后把该数组拷贝至3个其他数组中(在main()中声明这4个数组)。使用带数组表示法的函数进行第1份拷贝。使用带指针表示法和指针递增函数进行第2份拷贝。把目标数组名、源数组名和待拷贝的元素个数作为前两个函数的参数。第3个函数以目标数组名、源数组名和指向源数组最后一个元素后面的元素的指正。也就是说,给定以下声明,则函数调用如下表示:
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);

#include<stdio.h>
#define SIZE 5

void copy_arr(double ar[], double a[],int n);
void copy_ptr(double *a, double *b,int n);
void copy_ptrs(double *a, double *b,double *end);

int main()
{
    int i;
    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);

    for(i = 0; i < SIZE; i++)
    {
        printf("target3[%d] = %.2lf", i, target3[i]);
        printf("\n");
    }

    return 0;
}

void copy_arr(double ar[], double a[],int n)
{
    int i;
    for(i = 0; i< n; i++)
        ar[i] = a[i];
    for(i = 0; i< n; i++)
    {
        printf("target1[%d] = %.2lf", i, ar[i]);
        printf("\n");
    }
}

void copy_ptr(double *a, double *b,int n)
{
    int i;
    for(i = 0; i< n; i++)
        *(a+i) = *(b+i);
    for(i = 0; i< n; i++)
    {
        printf("target2[%d] = %.2lf", i, a[i]);
        printf("\n");
    }
}

void copy_ptrs(double *a, double *b,double *end)
{
    while(b<end)
    {
        *a = *b;
        b++;
        a++;
    }
}

也可以这样做,但要注意防止越界的问题 thanks for those who provied help in CSDN

#include<stdio.h>
#define SIZE 5

void copy_arr(double ar[], double a[],int n);
void copy_ptr(double *a, double *b,int n);
void copy_ptrs(double *a, double *b,double *end);

int main()
{
    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 a[],int n)
{
    int i;
    for(i=0;i<n;i++)
        ar[i] = a[i];
    for(i=0;i<n;i++)
    {
        printf("target1[%d] = %.2lf\n", i, ar[i]);
    }
}

void copy_ptr(double *a, double *b,int n)
{
    int i;
    for(i=0;i<n;i++)
        *(a+i) = *(b+i);
    for(i=0;i<n;i++)
    {
        printf("target2[%d] = %.2lf\n", i, a[i]);
    }
}


void copy_ptrs(double *a, double *b,double *end)
{
    int i;
    double *temp = a; //替换a,否则while循环结束后,a指向数组末尾,在for循环打印的时候,可能出现数组越界的情况
    while(b<end)
    {
        *temp = *b;
        b++;
        temp++;
    }

    for(i=0;i<SIZE;i++)
    {
        printf("target3[%d] = %.2lf\n", i, *(a+i));
    }
}

3.编写一个函数,返回一个int类型数组中存储的最大值,并在一个简单的程序中测试该函数。

#include<stdio.h>
#define SIZE 8

int max(int a[], int n);

int main()
{
    int a[SIZE];
    int i;
    printf("please input %d integers: \n", SIZE);

    for(i = 0; i < SIZE; i++)
        scanf("%d", &a[i]);
    printf("The largest number in arr a is %d\n", max(a, SIZE));
    return 0;
}

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

4. 编写一个函数,返回一个double类型数组中存储的最大值的下标,并在一个简单的程序中测试该函数。

#include<stdio.h>
#define SIZE 8

int max(double a[], int n);

int main()
{
    int i;
    double a[SIZE];
    printf("please input %d double numbers: \n", SIZE);

    for(i = 0; i < SIZE; i++)
        scanf("%lf", &a[i]);

    printf("The index of the largest double number in arr a is %d\n", max(a, SIZE));
    return 0;
}

int max(double a[], int n)
{
    int i,j=0;
    double max = 0;
    for(i = 0; i< n; i++)
        if(max < a[i])
        {
            max = a[i];
            j = i;
            printf("i = %d, max = %f\n", i, max);
        }
    return j;
}

5.编写一个函数,返回存储在double类型数组中最大值和最小的数之间的差值,并在一个简单的程序中测试该函数。

#include<stdio.h>
#define SIZE 8

double maxminusmin(double a[], int n);

int main()
{
    double a[SIZE];
    int i;
    printf("please input %d double numbers: \n", SIZE);

    for(i = 0; i < SIZE; i++)
        scanf("%lf", &a[i]);
    printf("The difference between the minimum and maximum values of the arr a is %lf\n",  maxminusmin(a, SIZE));
    return 0;
}

double maxminusmin(double a[], int n)
{
    int i;
    double max , min;
    max = min = a[0];
    for(i = 1; i < n; i++)
    {
        if(max < a[i])
            max = a[i];      
        if(min > a[i])
            min = a[i];
    }
    return (max - min);
}

6.编写一个函数,把double类型数组中的数据倒序排列,并在一个简单的程序中测试该函数。

#include<stdio.h>
#define SIZE 8
void revert_arr(double arr[], int n);

int main()
{
    int i;
    double arr[SIZE];
     printf("Before enter %d double numbers:\n", SIZE);
     for(i = 0; i < SIZE; i++)
         scanf("%lf", &arr[i]);
    printf("Before revert, the array is:\n");
    for (i = 0; i < SIZE; i++)
    {
        printf(" %.2lf", arr[i]);
    }
    printf("\n");

    revert_arr(arr, SIZE);

    printf("After revert, the array is:\n");
    for (i = 0; i < SIZE; i++)
    {
        printf(" %.2lf", arr[i]);
    }
    printf("\n");

    return 0;
}

void revert_arr(double arr[], int n)
{
    int i;
    for (i = 0; i < n / 2; i++)
    {
        double tmp = arr[i];
        arr[i] = arr[n - 1 - i];
        arr[n - 1 - i] = tmp;
    }
}

7.编写一个程序,初始化一个double类型的二维数组,使用练习2中的一个拷贝函数来把该数组中的数据拷贝至另一个二维数组中(因为二维数组是数组的数组,所以可以使用处理一维数组的拷贝函数来处理数组中的每个子数组)。

#include<stdio.h>
#define ROWS 3    //行数
#define COLS 4    //列数
//ar是内含4个double类型值得数组
void copy_arr(double ar[][COLS], double a[][COLS],int n);

int main()
{
    double target[ROWS][COLS];
    double source[ROWS][COLS] = {
        {
   
   1.1, 2.2, 3.3, 4.4},
        {
   
   2.2, 3.3, 4.4, 5.5},
        {
   
   3.3, 4.4, 5.5, 6.6}
    };

    copy_arr(target, source, ROWS);
    return 0;
}

void copy_arr(double ar[][COLS], double a[][COLS],int n)
{
    int i, j;
    for(i = 0; i < n; i++)
        for(j = 0; j < COLS; j++)
            ar[i][j] = a[i][j];

//打印目标数组(拷贝后的)
    for(i =0; i < n; i++)
    {
        for(i = 0; i < n; i++)
        for(j = 0; j < COLS; j++)
        printf("target[%d][%d] = %.2lf ", i, j, ar[i][j]);
    }
}

8.使用编程练习2中的拷贝函数,把一个包含7个元素的数组中第3~第5个元素拷贝至内含3个元素的数组中。该函数本身不需要修改,只需要选择合适的实际参数(实际参数不需要是数组名和数组大小,而只需要是数组元素的地址和待处理元素的个数)

#include<stdio.h>
#define LEN 7
#define SIZE 3
void copy_ptr(double *a, double *b,int n);

int main()
{
    double source[LEN];
    double target[SIZE];
    printf("Please input %d double numbers:\n", LEN);     
    for(i = 0; i < LEN; i++)
        scanf("%lf", &source[i]);
    copy_ptr(target, source, SIZE);
    //或者这样做
    /* copy_ptr(target, source + 2, SIZE); */
    return 0;
}

void copy_ptr(double *a, double *b,int n)
{
    int i;
    for(i = 0; i < n; ++)
        *(a + i) = *(b + i + 2);
        /* *(a + i) = *(b + i); */
    for(i = 0; i < n; i++)
    {
        printf("target[%d] = %.2lf\n", i, a[i]);
    }
}

猜你喜欢

转载自blog.csdn.net/Tanyongyin/article/details/78660909