天天看點

【C Primer Plus】第十章 數組和指針(二)

// 10.13-9.c -- 把兩個數組内的相應元素相加,結果存儲到第3個數組内

#include<stdio.h>

#define SIZE 4

void sum2a(int arr1[], int arr2[], int arr3[], int n);

void show_array(int arr[], int n);

int main(void)

{

int arr1[SIZE] = {2, 4, 5, 8};

int arr2[SIZE] = {1, 0, 4, 6};

int arr3[SIZE];

printf("The Array1:/n");

show_array(arr1, SIZE);

printf("The Array2:/n");

show_array(arr2, SIZE);

sum2a(arr1, arr2, arr3, SIZE);

printf("The Array3:/n");

show_array(arr3, SIZE);

return 0;

}

void show_array(int arr[], int n)

{

int i;

for(i=0; i<n; i++)

printf("%d ", arr[i]);

putchar('/n');

}

void sum2a(int arr1[], int arr2[], int arr3[], int n)

{

int i;

for(i=0; i<n; i++)

{

arr3[i] = arr1[i] + arr2[i];

}

}

// 10.13-10.c -- 列印3*5數組,然後翻一番再次列印

#include<stdio.h>

#define ROWS 3

#define COLS 5

void show_array(int arr[][COLS], int rows);

void mult2d(int source[][COLS], int target[][COLS], int rows);

int main(void)

{

int array1[ROWS][COLS] = {

{1, 3, 5, 7, 9},

{2, 4, 6, 8 ,10},

{11, 12, 13, 14, 15}

};

int array2[ROWS][COLS];

printf("The Array 1:/n");

show_array(array1, ROWS);

mult2d(array1, array2, ROWS);

printf("The Array 2:/n");

show_array(array2, ROWS);

return 0;

}

void show_array(int arr[][COLS], int rows)

{

int i,j;

for(i=0; i<rows; i++)

{

for(j=0; j<COLS; j++)

{

printf("%d ", arr[i][j]);

}

putchar('/n');

}

putchar('/n');

}

void mult2d(int source[][COLS], int target[][COLS], int rows)

{

int i,j;

for(i=0; i<rows; i++)

{

for(j=0; j<COLS; j++)

target[i][j] = source[i][j] * 2;

}

}  

// 10.13-11.c -- 針對若幹年的降水量資料,計算年降水量、年降水準均量,以及月降水準均量

#include<stdio.h>

#define MONTHS 12

#define YEARS 5

void year_rainfall(const float arr[][MONTHS], int years);

void month_avg(const float arr[][MONTHS], int years);

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}

};

year_rainfall(rain, YEARS);

month_avg(rain, YEARS);

return 0;

}

void year_rainfall(const float arr[][MONTHS], int years)

{

int y, m;

double total, subtot;

printf(" YEAR RAINFALL (inches):/n");

for(y=0, total=0; y<years; y++)

{

for(m=0, subtot=0; m<MONTHS; m++)

subtot += arr[y][m];

printf("%5d %15.1f/n", 2000+y, subtot);

total += subtot;

}

printf("/nThe yearly average is %.1f inches./n/n", total/YEARS);

}

void month_avg(const float arr[][MONTHS], int years)

{

int y,m;

double subtot;

printf("MONTHLY AVERAGES: /n/n");

printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/n");

for(m=0; m<MONTHS; m++)

{

for(y=0, subtot=0; y<years; y++)

subtot += arr[y][m];

printf("%4.1f", subtot/YEARS);

}

printf("/n");

}

/*

10.13-12.c -- 提示使用者輸入3個數集,每個數集包括5個double值

a. 把輸入資訊存儲到一個3X5的數組中

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

c. 計算所有數值的平均數

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

e. 列印出結果

*/

#include <stdio.h>

#define ROWS 3

#define COLS 5

void store(double ar[], int n);

void show_arr2d(double arr[][COLS], int rows);

double average(double arr[], int n);

double average2d(double arr[][COLS], int rows);

double max(double arr[][COLS], int rows);

int main(void)

{

double stuff[ROWS][COLS];

int row;

for(row=0; row<ROWS; row++)

{

printf("Enter %d numbers for row %d/n", COLS, row+1);

store(stuff[row], COLS);

}

printf("Array Contents:/n");

show_arr2d(stuff, ROWS);

for(row=0; row<ROWS; row++)

{

printf("Average Value of ROW %d: %g /n",row+1, average(stuff[row], COLS));

}

putchar('/n');

printf("Average Value of all rows: %g/n", average2d(stuff, ROWS));

printf("The largest number: %g/n", max(stuff, ROWS));

return 0;

}

void store(double ar[], int n)

{

int col;

for(col=0; col<n; col++)

{

printf("Enter value #%d: ", col+1);

scanf("%lf", &ar[col]);

}

}

void show_arr2d(double arr[][COLS], int rows)

{

int r,c;

for(r=0; r<rows; r++)

{

for(c=0; c<COLS; c++)

printf("%lf ", arr[r][c]);

printf("/n");

}

putchar('/n');

}

double average(double ar[], int n)

{

int i;

double sum=0.0;

for(i=0; i<n; i++)

sum += ar[i];

if(n>0)

return sum/n;

else

return 0.0;

}

double average2d(double arr[][COLS], int rows)

{

int r, c;

double sum=0.0;

for(r=0; r<rows; r++)

{

for(c=0; c<COLS; c++)

{

sum += arr[r][c];

}

}

if(rows>0)

return sum/(rows*COLS);

}

double max(double arr[][COLS], int rows)

{

int r,c;

double max=arr[0][0];

for(r=0; r<rows; r++)

{

for(c=1; c<COLS; c++)

{

if(max<arr[r][c])

max = arr[r][c];

}

}

return max;

}