C Primer Plus 第6版 Chapter 10 课后编程练习

ex10.1

// ex_10.1
#include 
#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;
}

ex10.2

// ex_10.2
#include 
void copy_arr(double target1[], double source[], int);
void copy_ptr(double target2[], double source[], int);
void copy_ptrs(double* target3, double* source_start, double* source_end);
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);
    printf("target1[5] = ");
    for (int i = 0; i < 5; i++)
        printf("%.1f ", target1[i]);

    return 0;
}

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

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

void copy_ptrs(double* target3, double* source_start, double* source_end)
{
    for (double* i = source_start; i < source_end; i++)
        *target3++ = *i;
}

ex10.3

// ex_10.3
#include 
#include 

int main(void)
{
    int arr[11] = { 10, 69, 59, 8, 6, 5, 23, 486, 5, 100, 3};

    int result = max_element(arr);

    printf("The max element of array is %d.\n", result);

    return 0;
}

int max_element(int arr[])
{
    int max = arr[0];
    for (int i = 0; i != '\n'; i++)
    {
        if (max < arr[i])
        {
            max = arr[i];
        }
    }

    return max;
}

ex10.4

// ex_10.4
#include 
#include 
int max_element_index(double arr[]);
int main(void)
{
    double arr[] = { 10.3, 69.6, 59.6, 8.3, 6.3, 5.3, 23.3, 486.3, 5.3, 100.3, 3.0};

    int index = max_element_index(arr);

    printf("The index of the largest value in array is %d.\n", index);

    return 0;
}

int max_element_index(double arr[])
{
    double max = arr[0];
    int index = 0;
    for (int i = 0; i != '\n' ; i++)
    {
        if (max < arr[i])
        {
            max = arr[i];
            index = i;
        }
    }

    return index;
}

ex10.5

// ex_10.5
#include 
#include 
double max_element(double arr[]);
double min_element(double arr[]);
double diff(double arr[]);
int main(void)
{
    double arr[] = { 10.3, 69.6, 59.6, 8.3, 6.1, 5.9, 23.3, 486.3, 5.1, 100.3, 3.0};

    double difference = diff(arr);
    printf("The largest value in the array is: %.2f\n", max_element(arr));
    printf("The smallest value in the array is: %.2f\n", min_element(arr));
    printf("The difference between the largest and smallest in array is %.2f.\n", difference);

    return 0;
}

double max_element(double arr[])
{
    double max = arr[0];
    for (int i = 0; i != '\n' ; i++)
    {
        if (max < arr[i])
        {
            max = arr[i];
        }
    }

    return max;
}

double min_element(double arr[])
{
    double min = arr[0];
    for (int i = 0; i != '\n' ; i++)
    {
        if (min > arr[i])
        {
            min = arr[i];
        }
    }

    return min;
}

double diff(double arr[])
{
    return max_element(arr) - min_element(arr);
}

ex10.6

// ex_10.6
#include 
void reverse_arr(double arr[], int len);
int main(void)
{
    double arr[] = { 10.3, 69.6, 59.6, 8.3, 6.1, 5.9, 23.3, 486.3, 5.1, 100.3, 3.0};
    int len_arr = sizeof arr / sizeof(double);
    printf("Before reverse: ");
    for (int i = 0; i < len_arr; i++)
        printf("%.1f ", arr[i]);
    reverse_arr(arr, len_arr);
    printf("\nAfter reverse: ");
    for (int i = 0; i < len_arr; i++)
        printf("%.1f ", arr[i]);

    return 0;
}

void reverse_arr(double arr[], int len)
{
    double temp;
    for (int i = 0; i < len / 2; i++)
    {
        temp = arr[i];
        arr[i] = arr[len-1-i];
        arr[len-1-i] = temp;
    }
}

ex10.7

// ex_10.7
#include 
void copy_arr(double target1[], double source[], int);
int main(void)
{
    double arr[3][2] = {
                        {1.1,2.2}, // arr[0]
                        {3.3,4.4}, // arr[1]
                        {5.5,6.6}  // arr[2]
                        };
    double new_arr[3][2];
    for (int i = 0; i < 3; i++)
        copy_arr(new_arr[i], arr[i], 3);

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            printf("%.1f ", new_arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

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

ex10.8

// ex_10.8
#include 
void copy_ptrs(double* target3, double* source_start, double* source_end);
int main(void)
{
    double arr[7] = {1.1,2.2,3.3,4.4,5.5,6.6,7.7};
    double new_arr[3];
    copy_ptrs(new_arr, arr+2, arr+5);
    for (int i = 0; i < 3 ;i++)
        printf("%.1f ", new_arr[i]);

    return 0;
}

void copy_ptrs(double* target3, double* source_start, double* source_end)
{
    for (double* i = source_start; i < source_end; i++)
        *target3++ = *i;
}

ex10.9

// ex_10.9
#include 
void copy_2d_arr(double target[][5], double source[][5], int n);
void print_2d_arr(double arr[][5], int rows);
int main(void)
{
    double arr[3][5] =
    {
        {1.1, 2.2, 3.3, 4.4, 5.5},
        {6.6, 7.7, 8.8, 9.9, 10.1},
        {11.11, 12.12, 13.13, 14.14,15.15}
    };
    double new_arr[3][5];
    copy_2d_arr(new_arr, arr, 3);
    print_2d_arr(new_arr, 3);

    return 0;
}

void copy_2d_arr(double target[][5], double source[][5], int n)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 5; j++)
            target[i][j] = source[i][j];
    }
}

void print_2d_arr(double arr[][5], int rows)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            printf("%.2f ", arr[i][j]);
        }
        printf("\n");
    }
}

ex10.10

// ex_10.10
#include 
#define SIZE 4
void arr_sum(int arr1[], int arr2[], int arr3[], int size);
int main(void)
{
    int arr1[SIZE] = {2, 4, 5, 8};
    int arr2[SIZE] = {1, 0, 4, 6};
    int arr3[SIZE];
    arr_sum(arr1, arr2, arr3, SIZE);
    for (int i = 0; i < SIZE; i++)
        printf("%d ", arr3[i]);
    return 0;
}

void arr_sum(int arr1[], int arr2[], int arr3[], int size)
{
    for (int i = 0; i < size; i++)
    {
        arr3[i] = arr1[i] + arr2[i];
    }
}

ex10.11

// ex_10.11
#include 
void print_arr(int arr[][5], int rows);
void double_arr(int arr[][5], int rows);
int main(void)
{
    int arr[3][5] = {
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 10},
        {11, 12, 13, 14, 15}
    };
    print_arr(arr, 3);
    double_arr(arr, 3);
    print_arr(arr, 3);

    return 0;
}

void print_arr(int arr[][5], int rows)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}
void double_arr(int arr[][5], int rows)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            arr[i][j] = 2 * arr[i][j];
        }
    }
}

ex10.12

// ex10.12
#include 
void rain_statics(float rain[][12],int YEARS);
int main(void)
{
// initializing rainfall data for 2010 - 2014
    const float rain[5][12] =
    {
        {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}
    };
    rain_statics(rain, 5);

    return 0;
}

void rain_statics(float rain[][12],int YEARS)
{
    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 < 12; 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 < 12; 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");
}

ex10.13

// ex_10.13
#include 
int main(void)
{
    double arr[3][5] =
    {
        {1, 2, 3, 4, 9},
        {1, 2, 3, 4, 5},
        {1, 2, 3, 4, 5}
    };
    avg_arr(arr, 3);
    max_arr(arr, 3);

    return 0;
}

void avg_arr(double arr[][5], int rows)
{
    double subtot, total;
    printf(" SET   SET_AVG\n");
    total = 0;
    for (int i = 0; i < rows; i++)
    {
        subtot = 0;
        for (int j = 0; j < 5; j++)
            {
                subtot += arr[i][j];
            }
        printf("%3d %10.2f\n", 1 + i, subtot/5);
        total += subtot;
    }
    printf("\nThe array average is %.2f .\n\n",
       total/(rows * 5));
}

void max_arr(double arr[][5], int rows)
{
    int i,j = 0;
    double max = arr[0][0];
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < 5; j++)
        {
            if (arr[i][j] > max)
            {
                max = arr[i][j];
            }
        }
    }
    printf("The largest value of the array is: %.2f", max);
}

ex10.14

感觉14题的意义并不是很大,就省略了代码。

你可能感兴趣的:(c,C++,C语言,C,Primer,Plus,第六版)