C Primer Plus 第10章 數組和指針 編程練習答案

一、修改程序清單10.7中的程序rain,使它不使用數組下標,而是使用指針進行計算(程序中仍然須要聲明並初始化數組)。數組

#include <stdio.h>
#define MONTHS 12
#define YEARS 5
int main (void)
{
    //把數組初始化爲2000年到2004年的降水量數據
    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.lf\n",2000+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 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;

}

二、編寫一個程序,初始化一個double數組,而後把數組內容複製到另外兩個數組(3個數組都須要在主程序中聲明)。製做第一份拷貝的函數使用數組符號。製做第二份拷貝的函數使用指針符號,並使用指針的增量操做。把目標數組名和要複製的元素數目作爲參數傳遞給函數。也就是說,若是給定了下列聲明,函數調用應該以下面所示:函數

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);
#include <stdio.h>
void copy_arr(double source[],double target1[],int n);
void copy_ptr(double *source,double *target2,int n);
int main(void)
{
 double source[] = {1.1, 2.2, 3.3, 4.4, 5.5};
 double target1[5] = {0};
 double target2[5] = {0};
 printf("Before operation:\n");
 printf("source :\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4]);
 printf("target1:\t%g\t%g\t%g\t%g\t%g\n",target1[0],target1[1],target1[2],target1[3],target1[4]);
 printf("target2:\t%g\t%g\t%g\t%g\t%g\n",target2[0],target2[1],target2[2],target2[3],target2[4]);
 copy_arr(source, target1, 5);
 copy_ptr(source, target2, 5);
 printf("After operation:\n");
 printf("source :\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4]);
 printf("target1:\t%g\t%g\t%g\t%g\t%g\n",target1[0],target1[1],target1[2],target1[3],target1[4]);
 printf("target2:\t%g\t%g\t%g\t%g\t%g\n",target2[0],target2[1],target2[2],target2[3],target2[4]);

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

三、編寫一個函數,返回一個int數組中存儲的最大值,並在一個簡單的程序中測試這個函數。測試

#include<stdio.h>
#define WIDTH 6
int max(int ar[WIDTH],int n);
int main(void)
{
    int ar[WIDTH]={4,3,6,2,8,6};

    printf("The max is %d\n",max(ar,WIDTH));
    return 0;
}
int max(int ar[WIDTH],int n)
{
    int i,max;
    for(i=1,max=ar[0];i<n;i++)
       {
          if(max < ar[i])
            max=ar[i];
       }
    return max;
}

四、編寫一個函數,返回一個double數組中存儲的最大值的索引,並在一個簡單的程序中測試這個函數。指針

#include<stdio.h>
#define WIDTH 6
int max(float ar[WIDTH],int n);
int main(void)
{
    float ar[WIDTH]={4.4,3.3,6.6,2.2,8.8,9.9};

    printf("The max number's index is %d\n",max(ar,WIDTH));
    return 0;
}
int max(float ar[WIDTH],int n)
{
    int i,max;
    for(i=1,max=0;i<n;i++)
       {
          if(ar[max] < ar[i])
            max=i;
       }
            return max;
}

五、 編寫一個函數,返回一個double數組中最大值和最小值之間的差值,並在一個簡單的程序中測試這個函數。code

#define WIDTH 6
float gap(float ar[WIDTH],int n);
int main(void)
{
    float ar[WIDTH]={4.4,3.3,6.6,2.2,8.8,9.9};

    printf("The gap between max and min is %.2f\n",gap(ar,WIDTH));
    return 0;
}
float gap(float ar[WIDTH],int n)
{
    int i;
    float min,max;
    for(i=1,max=ar[0],min=ar[0];i<n;i++)
       {
          if(max< ar[i]) max=ar[i];
          if(min> ar[i]) min=ar[i];
       }
    return (max-min);
}

六、編寫一個程序,初始化一個二維數組,並利用練習2中的任一函數來把這個二維數組複製到另外一個二維數組(由於二維數組是數組的數組,因此能夠使用處理一維數組的函數來複制數組的每一個子數組)。索引

#include <stdio.h>
#define ROWS 2
#define COLS 3
void copy_2d(double source[][COLS], double target[][COLS], int );
void copy_1d(double a1[], double a2[], int n);

int main(void)
{
 int i,j;
 double source[ROWS][COLS] = {1, 2, 3, 4, 5, 6};
 double target[ROWS][COLS] = {0};
 copy_2d(source, target, ROWS);
 for (i=0; i<ROWS; i++)
 {
  for (j=0; j<COLS; j++)
   printf("%g\t",target[i][j]);
  printf("\n");
 }

    return 0;
}  

void copy_2d(double ( *source )[COLS], double target[][COLS], int n)
{
 int i;
 for(i=0;i<n;i++)
  copy_1d( *(source+i),target[i],COLS);
}


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

七、利用練習2中的複製函數,把一個包含7個元素的數組內第3到第5元素複製到 一個包含3個元素的數組中。函數自己不須要修改,只須要選擇合適的實際參數(實際參數不須要是數組名和數組大小,而只須是數組元素的地址和須要複製的元素數目)。get

#include <stdio.h>
void copy_arr(double source[],double target1[],int n);
void copy_ptr(double *source,double *target2,int n);
int main(void)
{
 double source[] = {1.1, 2.2, 3.3, 4.4, 5.5};
 double target1[3] = {0};
 double target2[5] = {0};
 printf("Before operation:\n");
 printf("source :\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4]);
 printf("target1:\t%g\t%g\t%g\n",target1[0],target1[1],target1[2]);
 printf("target2:\t%g\t%g\t%g\t%g\t%g\n",target2[0],target2[1],target2[2],target2[3],target2[4]);
 copy_arr(&source[2], target1, 3);
 copy_ptr(source, target2, 5);
 printf("After operation:\n");
 printf("source :\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4]);
 printf("target1:\t%g\t%g\t%g\n",target1[0],target1[1],target1[2]);
 printf("target2:\t%g\t%g\t%g\t%g\t%g\n",target2[0],target2[1],target2[2],target2[3],target2[4]);

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

八、編寫一個程序,初始化一個3x5的二維double數組,並利用一個基於變長數組的函數把該數組複製到另外一個二維數組。還要編寫一個基於變長數組的函數來顯示兩個數組的內容。這兩個函數應該可以處理任意的NXM數組。input

#include <stdio.h>
#define ROWS 3
#define COLS 5
void copy_2d(int n,int m,double source[n][m], double target[n][m]);
void copy_1d(double a1[], double a2[], int n);
void show_arr(int n,int m,double ar[n][m]);

int main(void)
{
 int i,j;
 double source[ROWS][COLS] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
 double target[ROWS][COLS] = {0};
 printf("Before operation :\n");
 show_arr(ROWS,COLS,source);
 copy_2d(ROWS,COLS,source,target);
printf("After operation :\n");
 show_arr(ROWS,COLS,target);

    return 0;
}

void copy_2d(int n,int m,double source[n][m], double target[n][m])
{
 int i;
 for(i=0;i<n;i++)
  copy_1d( *(source+i),target[i],COLS);
}


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

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

九、編寫一個程序,把兩個數組內的相應元素相加,結果存儲到第三個數組內。函數的數組名包含3個數組名和數組大小,並在一個簡單的程序中測試這個函數。io

#include<stdio.h>
#define COLS 4
void sum(int a1[],int a2[],int target[],int n);
int main(void)
{
    int a1[COLS]={2,4,5,8};
    int a2[COLS]={1,0,4,6};
    int target[COLS]={0};

    sum(a1,a2,target,COLS);
    printf("a1[0]=%d\t a1[1]=%d\t a1[2]=%d\t a1[3]=%d\n",a1[0],a1[1],a1[2],a1[3]);
    printf("a2[0]=%d\t a2[1]=%d\t a2[2]=%d\t a2[3]=%d\n",a2[0],a2[1],a2[2],a2[3]);
    printf("target[0]=%d\t target[1]=%d\t target[2]=%d\t target[3]=%d\n",target[0],target[1],target[2],target[3]);
    return 0;
}
void sum(int a1[],int a2[],int target[],int n)
{
    int i;
    for(i=0;i<COLS;i++)
        target[i]=a1[i]+a2[i];
    return 0;
}

十、編寫一個程序,聲明一個3x5的數組並初始化,具體數值能夠隨意。程序打印出數值,而後數值翻一番,接着再次打印出新值。編寫一個函數來顯示數組的內容,再編寫另外一個函數執行翻倍功能。數組名和數組行數做爲參數由程序傳遞給函數。class

#include<stdio.h>
#define ROWS 3
#define COLS 5
void show_arr(int ar[][COLS],int n);
void twice(int (*pt)[COLS],int n);
int main(void)
{
    int ar[ROWS][COLS] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
    printf("Before:\n");
    show_arr(ar,ROWS);
    twice(ar,ROWS);
    printf("After:\n");
    show_arr(ar,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("%d\t",ar[i][j]);
        printf("\n");
    }
    return 0;
}
void twice(int (*pt)[COLS],int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<COLS;j++)
        *(*(pt+i)+j)=2*(*(*(pt+i)+j));
    }
    return 0;

}

十一、重寫程序清單10.7的程序rain,main()中的主要功能改成由函數來執行。

/*rain.c 針對若干年的降水量數據,計算年降水總量、年降水平均量、以及月降水平均量*/
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
void display(float arr[][MONTHS],int years);
int main (void)
{
    //把數組初始化爲2000年到2004年的降水量數據
    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}
    };
    display(rain,YEARS);
    return 0;
}
void display(float arr[][MONTHS],int years)
{
    int year,month;
    float subtot,total;

    printf("YEAR  RAINALL(inches) \n");
    for(year=0,total=0;year<years;year++)
    {
        for(month=0,subtot=0;month<MONTHS;month++)
            subtot+=arr[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 AVERAGES:\n\n");
    printf(" Jan  Feb  Mar   Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec\n");

    for(month=0;month<MONTHS;month++)
    {    //每月,各年該月份的總降水量
        for(year=0,subtot=0;year<years;year++)
            subtot+=arr[year][month];
        printf("%4.1f ",subtot/years);
    }
    printf("\n");
    return 0;
}

十二、編寫一個程序,提示用戶輸入3個數集,每一個數集包括5個double值。程序應當實現下列全部功能:

a.把輸入信息存儲到一個3*5的數組中;

b.計算出每一個數集(包含5個數值)的平均值;

c.計算全部數值的平均值;

d.找出這15個數的最大值;

e.打印結果

每一個任務須要用一個獨立的函數來完成(使用傳統C處理數組的方法)。對於任務b,須要編寫計算並返回一維數組平均值的函數,循環3次調用該函數來實現任務b。對於其餘任務,函數應當把整個數組作爲參數,而且完成任務c和d的函數應該向它的調用函數返回答案。

#include <stdio.h>
#define ROWS 3
#define COLS 5
void store(double ar[][COLS],int rows);
double average_row(double ar[COLS]);
double average_tot(double ar[][COLS],int rows);
double f_max(double ar[][COLS],int rows);
void display(double ar[][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;
    store(array,ROWS);
    average0=average_row(&array[0]);
    average1=average_row(&array[1]);
    average2=average_row(&array[2]);
    average_total=average_tot(array,ROWS);
    max = f_max(array,ROWS);
    display(array,ROWS,average0,average1,average2,average_total,max);
    return 0;
}
void store(double ar[][COLS],int rows)
{
    int i,j;
    printf("Please input 3 array of 5 numbers:");
    for(i=0;i<rows;i++)
    {
        for(j=0;j<COLS;j++)
            scanf("%lf",&ar[i][j]);
    }
}
double average_row(double ar[COLS])
{
    int i;
    double sum=0;
    for(i=0;i<COLS;i++)
       sum += ar[i];
    return sum/COLS;
}
double average_tot(double ar[][COLS],int rows)
{
    int i,j;
    double sum=0;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<COLS;j++)
            sum+=ar[i][j];
    }
    return sum/(ROWS*COLS);
}
double f_max(double ar[][COLS],int rows)
{
    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];

    return max;
}
void display(double ar[][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",ar[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);
}
相關文章
相關標籤/搜索