// 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; }