C primer plus 第十章

编程练习

1.

#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", 2000 +year, subtot);
        total += subtot;
    }
    printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);
    printf("MONTHLY AVERAGE: \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.

#include <stdio.h>
void copy_arr(double[], double[], int);
void copy_ptr(double *, double *, int);
int main(void)
{
    double source[5] = {1.1, 2.2, 3.3, 4.4 ,5.5};
    double target1[5];
    double target2[5];

    copy_arr(source, target1, 5);
    copy_ptr(source, target2, 5);
    return 0;
}

void copy_arr(double a1[], double a2[], int n)
{
    int i;
    for(i = 0; i<n; i++)
        a2[i] = a1[i];
}
void copy_ptr(double *p1, double *p2, int n)
{
    int i;
    for(i = 0; i<n; i++)
        *(p2 + i) = *(p1 + i);
}

3.

#include <stdio.h> 
#define NUMBER 6
int max(int [], int n);

int main(void)
{
    int array[] = {4, 5, 6, 3, 7, 8};

    printf("The max is:%d", max(array, NUMBER));
    return 0;
} 

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

4.

#include <stdio.h>
#define NUMBER 6
int max(double [], int);

int main(void)
{
    double array[] = {3.1, 3.4, 4.5, 5.6, 39, 5.8};
    printf("The max index is:%d", max(array, NUMBER));
    return 0;
}

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

5.

#include <stdio.h> 
#define NUMBER 6
int value(double[], int);

int main(void)
{
    double array[] = {3.4, 4.5, 5.6, 4.6, 8.4, 3.4};

    printf("The gap value is:%d", value(array, NUMBER));
    return 0;
}

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

6.

#include <stdio.h>
#define ROW 2
#define COL 3
void copy_1d(double [][COL], double[][COL], int);
void copy_2d(double [], double [], int);

int main(void)
{
    double source[ROW][COL] = {1, 2, 3, 4, 5, 6};
    double target[ROW][COL] = {0};
    copy_1d(source, target, ROW);
    return 0;
}
void copy_1d(double (*source)[COL], double target[][COL], int n)
{
    int i;
    for(i = 0; i < n; i++)
        copy_2d(*(source + i), target[i], COL);
}

void copy_2d(double a[], double b[], int n)
{
    int i;
    for(i = 0; i < n; i++)
        a[i] = b[i];
}

7.

#include <stdio.h>
#define NUMBER 3
void copy(double *, double *, int);

int mian(void)
{
    double source[6] = {1, 2, 3, 4, 5, 6};
    double target[3];

    copy(source+2, target, NUMBER);
    printf("target:%g%g%g%g%g%g\n", target[0], target[1], target[2]);
    return 0;
}

void copy(double *p1, double *p2, int n)
{
    int i;

    for(i = 0; i < n; i++)
        *(p2 + i) = *(p1 + 1);
}

8.

#include <stdio.h>
#define COL 5
void copy(double (*)[COL], double (*)[COL], int);
void display(double (*)[COL], int);

int main(void)
{
    double source[3][5] = {{1.1, 3.4, 5.6, 3.5, 3.7},
                           {2.3, 3.5, 4.4, 5.6, 7.5},
                           {3.4, 5.6, 6.7, 3.2, 2.5}};
    double target[6][5] = {0};

    copy(source, target, COL);

    display(source, 3);
    display(target, 3);
    return 0;
}

void copy(double (*source)[COL], double target[][COL], int row)
{
    int i, j;
    for(i = 0; i < row; i++)
        for(j = 0; j < COL; j++)
            target[i][j] = source[i][j];    
}

void display(double (*p)[COL], int rows)
{
    int i,j;
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < COL; j++)
            printf("%g\t", p[i][j]);
        printf("\n");
    }
}

9.

#include <stdio.h> 
#define NUM 4
void plus(int *, int *, int *, int);

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

    plus(source1, source2, target, NUM);
    printf("%g\t", target);
    return 0;
}

void plus(int a[], int b[], int c[], int n)
{
    int i;
    for(i = 0; i <n; i++)
        c[i] = a[i] + b[i];
}

10.

#include <stdio.h>
#define COL 5
void twice(double (*)[COL], double (*)[COL], int);
void display(double (*)[COL], int);

int main(void)
{
    double source[3][5] = {{1.1, 3.4, 5.6, 3.5, 3.7},
                           {2.3, 3.5, 4.4, 5.6, 7.5},
                           {3.4, 5.6, 6.7, 3.2, 2.5}};
    double target[6][5] = {0};

    twice(source, target, COL);

    display(source, 3);
    display(target, 3);
    return 0;
}

void twice(double (*source)[COL], double target[][COL], int row)
{
    int i, j;
    for(i = 0; i < row; i++)
        for(j = 0; j < COL; j++)
            target[i][j] = 2 * source[i][j];    
}

void display(double (*p)[COL], int rows)
{
    int i,j;
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < COL; j++)
            printf("%g\t", p[i][j]);
        printf("\n");
    }
}

11.

#include <stdio.h> 
#define MONTH 12
#define YEARS 5
void display(float [][MONTH], int);

int main(void)
{
    const float rain[YEARS][MONTH] = {
    {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}
};
    display(rain, YEARS);
    return 0;
}

void display( float rain[][MONTH], 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 < MONTH; month++)
        subtot += rain[year][month];
    printf("%5d %15.1f\n", 2000 + 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 < MONTH; 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");
}

12.

#include <stdio.h>
#define ROWS 3
#define COLS 5
void store(double [][COLS], int);
double average(double [], int);
double aver(double [][], int, int);
double max(double [][], int, int);
void display(double p[][COLS], int rows, double average0, double average1, double average2, double average_total,double max);

int main(void)
{
    double array[ROWS][COLS];
    double average0, average1, average2, average_total, max;

    printf("Please enter an array:\n");
    store(array, 3);
    average(array[1], 5);
    average(array[2], 5);
    average(array[3], 5);
    aver(array, 3, 5);
    max(array, 3, 5);
    display(array, 3, average0, average1, average2, average_total, max);
    return 0;
}

void store(double p[][COLS], int rows)
{
    int i,j;
    for(i = 0; i < rows; i++)
        for(j = 0; j < COLS; j++)
            scanf("%lf", &*p[COLS]);
}

double average(double p[], int rows);
{
    double total, i;
    for(i = 0; i < rows; i++)
        total += *p[i];
    return total / rows;
}

double aver(double p[][], int rows, int cols)
{
    double total, i, j;
    for(i = 0; i < rows; i++)
        for(j = 0; j < cols; j++)
            total += *p[i][j];
    return total / (i * j);
}

double max(double p[][], int rows, int cols)
{
    double max = p[0][0];
    int i, j;
    for(i = 0; i < rows; i++)
        for(j = 0; j < cols; j++)   
            if(max < p[i][j])
                max = p[i][j];
    return max;
}

void display(double p[][COLS], int rows, double average0, double average1, double average2, double average_total,double max)
{
    int i,j;
    printf("a.\narray = \n");
    for(i=0;i<rows;i++)
    {
    for (j=0; j<COLS; j++)
        printf("%g\t",p[i][j]);
    printf("\n");
    }
    printf("b.\n");
    printf("average0 = %g\n",average0);
    printf("average1 = %g\n",average1);
    printf("average2 = %g\n",average2);
    printf("c.\n");
    printf("total average = %g\n",average_total);
    printf("d.\n");
    printf("max = %g\n",max);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值