C Primer Plus 第十章 数组 指针 编程练习答案(自作)


10.13.01

// 10.13.01

#include <stdio.h>
#define MONTHS 12
#define YEARS 5
float sum_totalyear( const float ar[][MONTHS], int n);
void sum_oneyear( const float ar[][MONTHS], int n);
void sum_totalmonth( const float ar[][MONTHS], int n);
void star(void);
int main()
{
    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 }
    };
    star();
    putchar('\n');
    printf("五年总降水量:%f\n",sum_totalyear( rain, YEARS));
    putchar('\n');
    star();
    putchar('\n');
    sum_oneyear( rain, YEARS);
    putchar('\n');
    star();
    putchar('\n');
    sum_totalmonth( rain, YEARS);
    putchar('\n');
    star();
    putchar('\n');
    
    return 0;
}

float sum_totalyear( const float ar[][MONTHS], int n)
{
    float total_totalyear = 0;
    int i, j;
    for( i = 0; i < n; i++){
        for( j = 0; j < MONTHS; j++){
            total_totalyear += ar[i][j];
        }
    }
    
    return total_totalyear;
}

void sum_oneyear( const float ar[][MONTHS], int n)
{
    int i, j;
    for( i = 0; i < n; i++){
        float total_oneyear = 0;
        for( j = 0; j < MONTHS; j++){
            total_oneyear += ar[i][j];
        }
        
        printf("第 %d 年总降水量:%f\n", i + 1, total_oneyear);
    }
    
    return;
}

void sum_totalmonth( const float ar[][MONTHS], int n)
{
    int i, j;
    for( j = 0; j < MONTHS; j++){
        float total_totalmonth = 0;
        for( i = 0; i < n; i++){
            total_totalmonth += ar[i][j];
        }
        
        printf("第 %d 月五年总降水量:%f\n", j+1, total_totalmonth);
    }
    
    return;
}

void star(void)
{
    printf("****************************\n");
    return;
}

10.13.02 

// 10.13.02

#include <stdio.h>
void copy_arr(double ar1[], double ar_s[], int n);
void copy_ptr(double *ar2, double *ar_s, int n);
void copy_ptrs(double *ar3, double *ar_s, double *ptrs);
int main()
{
    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);
    
    return 0;
}
void copy_arr(double ar1[], double ar_s[], int n)
{
    int i;
    for(i = 0; i < n; i++){
        ar1[i] = ar_s[i];
        printf("%lf %lf\n", ar1[i], ar_s[i]);
    }
        
    putchar('\n');
    return;
}
void copy_ptr(double *ar2, double *ar_s, int n)
{
    int i;
    for(i = 0; i < n; i++){
        *(ar2+i) = *(ar_s+i);
        printf("%lf %lf\n", *(ar2+i), *(ar_s+i));
    }
    
    putchar('\n');
    return;
}
void copy_ptrs(double *ar3, double *ar_s, double *ptrs)
{
    while(ar_s < ptrs){
        *ar3 = *ar_s;
        printf("%lf %lf\n", *ar3, *ar_s);
        ar_s++;
        ar3++;
    }
    
    putchar('\n');
    return;
}

10.13.03 

// 10.13.03

#include <stdio.h>
int max(int ar[], int n);
#define NUM 10
int main()
{
    int array[NUM] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,};
    printf("The biggest number is %d\n", max(array, NUM));
    
    return 0;
}
int max(int ar[], int n)
{
    int i;
    int max = ar[0];
    for(i = 1; i < n; i++){
        if(max < ar[i])
            max = ar[i];
    }
    
    return max;
}

10.13.04

// 10.13.04

#include <stdio.h>
int array1(double ar[], int n);
#define NUM 9
int main()
{
    double array[NUM] = {1.1, 2.2, 4.4, 3.3, 6.6, 5.5, 7.7, 9.9, 8.8, };
    printf("The index of biggest numer is %d\n", array1(array, NUM));
    
    return 0;
}
int array1(double ar[], int n)
{
    int i;
    int max = ar[0];
    int index = 0;
    for(i = 1; i < n; i++){
        if(ar[0] < ar[i]){
            max = ar[i];
            index = i - 1;
        }
    }
    
    return index;
}

10.13.05

// 10.13.05

#include <stdio.h>
#define NUM 10
double value(double ar[], int n);
int main()
{
    double array[NUM] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 8.8, 7.7, 9.9, 10.1};
    double ret = value(array, NUM);
    printf("%lf\n", ret);
    
    return 0;
}
double value(double ar[], int n)
{
    int i;
    double max = ar[0];
    double min = ar[0];
    for(i = 1; i < n; i++){
        if(max < ar[i])
            max = ar[i];
        else if(min > ar[i])
            min = ar[i];
    }
    
    return max - min;
}

10.13.06

// 10.13.06

#include <stdio.h>
#define NUM 10
void rec(double ar[], int n);
int main()
{
    double array[NUM] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10, };
    rec(array, NUM);
}
void rec(double ar[], int n)
{
    int i;
    for(i = n - 1; i >= 0; i--){
        printf("%f", ar[i]);
        putchar(' ');
    }
    putchar('\n');
    
    return;
}

10.13.07

// 10.13.07

#include <stdio.h>
#define rows 2
#define columns 5
int main()
{
    double source[rows][columns] = {
   
   {1.1, 2.2, 3.3, 4.4, 5.5},{6.6, 7.7, 8.8, 9.9, 10.10}};
    double copy_source[rows][columns];
    int i, j;
    for(i = 0; i < rows; i++)
        for(j = 0; j < columns; j++){
            copy_source[i][j] = source[i][j];
            printf("source[%d][%d] = %f\n", i, j, source[i][j]);
            printf("copy_source[%d][%d] = %f\n", i, j, copy_source[i][j]);
}
    return 0;
}

10.13.08

// 10.13.08

#include <stdio.h>
#define NUM 7
void transfer(double source[], int n);
int main()
{
    double source[NUM] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
    transfer(source, NUM);
    
    return 0;
}
void transfer(double source[], int n)
{
    double ar[3];
    int i;
    for(i = 2; i < 5; i++){
        ar[i-2] = source[i];
        printf("ar[%d] = %f\n", i-2, ar[i-2]);
        printf("source[%d] = %f\n", i, source[i]);
    }
    
    return;
}

10.13.09

// 10.13.09

#include <stdio.h>
void copy(int rows, int cols, double ar[rows][cols]);
int main()
{
    double ar[3][5] = {
   
   {1.1, 2.2, 3.3, 4.4, 5.5},{6.6, 7.7, 8.8, 9.9, 10.1},{11.1, 12.2, 13.3, 14.4, 15.5}};
    copy(3, 5, ar);
    
    return 0;
}
void copy(int rows, int cols, double ar[rows][cols])
{
    double copy_ar[rows][cols];
    int i, j;
    for(i = 0; i < rows; i++)
        for(j = 0; j < cols; j++){
            copy_ar[i][j] = ar[i][j];
            printf("copy_ar[%d][%d] = %f\n", i, j, copy_ar[i][j]);
            printf("ar[%d][%d] = %f\n", i, j, ar[i][j]);
        }
    
    return;
}

10.13.10

// 10.13.10

#include <stdio.h>
#define NUM 4
void cal(int ar1[], int ar2[], int ar3[], int n);
int main()
{
    int ar1[NUM] = {2, 4, 5, 8};
    int ar2[NUM] = {1, 0, 4, 6};
    int ar3[NUM];
    cal(ar1, ar2, ar3, NUM);
    
    return 0;
}
void cal(int ar1[], int ar2[], int ar3[], int n)
{
    int i;
    for(i = 0; i < n; i++){
        ar3[i] = ar1[i] +ar2[i];
        printf("ar3[%d] = %d\n", i, ar3[i]);
    }
    
    return;
}

10.13.11

// 10.13.11

#include <stdio.h>
#define rows 3
#define cols 5
void print(int ar[][cols], int n);
void twice(int ar[][cols], int n);
int main()
{
    int ar[rows][cols] = {
   
   {1, 2, 3, 4, 5},{6, 7, 8, 9, 10},{11, 12, 13, 14, 15}};
    print(ar, rows);
    twice(ar, rows);
}
void print(int ar[][cols], int n)
{
    int i, j;
    for(i = 0; i < n; i++)
        for(j = 0; j < cols; j++){
            printf("ar[%d][%d] = %d\n", i, j, ar[i][j]);
        }
    
    return;
}
void twice(int ar[][cols], int n)
{
    int ar_t[rows][cols];
    int i,j;
    for(i = 0; i < n; i++)
        for(j = 0; j <cols; j++){
            ar_t[i][j] = ar[i][j] * 2;
            printf("ar_t[%d][%d] = %d\n", i, j, ar_t[i][j]);
        }
    
    return;
}

10.13.13

// 10.13.13

#include <stdio.h>
#define rows 3
#define cols 5
void put(double ar[][cols], int n);
void average(double ar[][cols], int n);
void average_all(double ar[][cols], int n);
void num(double ar[][cols], int n);

int main()
{
    double ar[rows][cols];
    put(ar, rows);
    average(ar, rows);
    average_all(ar, rows);
    num(ar, rows);
    
    return 0;
}
void put(double ar[][cols], int n)
{
    int i, j;
    for(i = 0; i < n; i++)
        for(j = 0; j < cols; j++)
            scanf("%lf", &ar[i][j]);
    
    return;
}
void average(double ar[][cols], int n)
{
    int i, j;
    double total;
    for(i = 0; i < n; i++){
        total = 0;
        for(j = 0; j < cols; j++){
            total += ar[i][j];
        }
        printf("The average of rows %d is %lf\n", i, total/cols);
    }
    
    return;
}
void average_all(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];
        }
    }
    printf("The average of sum is %lf\n", total /(n*cols));
    
    return;
}
void num(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("The biggest value is %lf\n", max);
    
    return;
}

猜你喜欢

转载自blog.csdn.net/fengqy1996/article/details/123431652