/* 10.13-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; 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 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; } /* 10.13-2 */ #include<stdio.h> void copy_arr(double source[], double target1[], int n); void copy_ptr(double *ptr, double target2[], int n); int main(void) { int i; double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5}; double target1[5]; double target2[5]; printf("The Original Array:/n"); for(i=0; i<5; i++) printf("%.1lf ", source[i]); printf("/n"); copy_arr(source, target1, 5); copy_ptr(source, target2, 5); return 0; } void copy_arr(double source[], double target1[], int n) { int i; printf("Target1 Array:/n"); for(i=0; i<n; i++) { target1[i] = source[i]; printf("%.1lf ", target1[i]); } printf("/n"); } void copy_ptr(double *ptr, double target2[], int n) { int i; printf("Target2 Array:/n"); for(i=0; i<n; i++) { target2[i] = *ptr++; printf("%.1lf ", target2[i]); } printf("/n"); } // 10.13-3返回一个int 数组中存储的最大数值 #include<stdio.h> #define SIZE 5 int max_in_arr(int array[], int n); int main(void) { int index, max; int arr[SIZE] = {3, 5, 9, 7, 2}; printf("The Original Array: /n"); for(index=0; index<SIZE; index++) printf("%d ", arr[index]); printf("/n/n"); max = max_in_arr(arr, SIZE); printf("The MAX number in Array: %d/n", max); return 0; } int max_in_arr(int array[], int n) { int i, max; max = array[0]; for(i=0; i<n; i++) { if(max < array[i]) max = array[i]; } return max; } // 10.13-4 返回一个double数组中存储的最大数值的索引 #include<stdio.h> #define SIZE 5 int max_index(double array[], int n); int main(void) { double arr[SIZE] = {2.3, 4.5, 9.2, 9.1, 3.4}; int n, maxindex; printf("The Original Array: "); for(n=0; n<SIZE; n++) printf("%.1lf ", arr[n]); printf("/n/n"); maxindex = max_index(arr, SIZE); printf("The Index of Max Number: %d/n", maxindex); return 0; } int max_index(double array[], int n) { int i; int maxindex; maxindex = 0; for(i=0; i<n; i++) if(array[maxindex] < array[i]) maxindex = i; return maxindex; } // 10.13-5.c 返回一个double数组中最大的和最小的数之间的差值 #include<stdio.h> #define SIZE 5 void maxmin_dif(const double array[], int n); int main(void) { double arr[SIZE] = {1.2, 2.3, 8.9, 7.6, 5.2}; int i; printf("The Original Array: "); for(i=0; i<SIZE; i++) printf("%.1f ", arr[i]); printf("/n/n"); maxmin_dif(arr, SIZE); return 0; } void maxmin_dif(const double array[], int n) { int i; double diff; double max = array[0]; double min = array[0]; for(i=1; i<n-1; i++) { if(max<array[i]) max = array[i]; else if(min > array[i]) min = array[i]; } diff = max - min; printf("The maximum: %f/nThe minimum: %f/n", max, min); printf("The difference between maximum and minimum: %f/n", diff); } /* 10.13-6.c 初始化一个二维数组,并复制到到另一个二维数组 */ #include<stdio.h> #define ROWS 2 #define COLS 3 int main(void) { double source[ROWS][COLS] = { {1.1, 2.2, 3.3}, {6.6, 7.7, 8.8} }; double target[ROWS][COLS]; int row, col; printf("The Target Array: /n"); for(row=0; row<ROWS; row++) { for(col=0; col<COLS; col++) { target[row][col] = source[row][col]; printf("%.1f ", target[row][col]); } printf("/n"); } return 0; } /* 10.13-7.c -- 把一个包含7各元素的数组内第3到第5元素复制到一个包含3个元素的数组中 */ #include<stdio.h> void copy_arr(double source[], double target2[], int n); int main(void) { double source[7] = {10, 9, 8, 7, 6, 5, 4}; double target[3]; copy_arr(source+2, target, 3); return 0; } void copy_arr(double source[], double target2[], int n) { int i; printf("Target2 Array:/n"); for(i=0; i<n; i++) { target2[i] = source[i]; printf("%.1lf ", target2[i]); } printf("/n"); } // 10.13-8.c -- 变长数组复制并显示二维数组 #include<stdio.h> #define ROWS 3 #define COLS 5 void show_array(int rows, int cols, double vla[rows][cols]); void copy_array(int rows, int cols, double source[rows][cols], double target[rows][cols]); int main(void) { double arr[ROWS][COLS] = { {1.1, 2.2, 3.3, 4.4, 5.5}, {6.6, 7.7, 8.8, 9.9, 10.10}, {11.11, 12.12, 13.13, 14.14, 15.15} }; double target[ROWS][COLS]; printf("The Original Array:/n"); show_array(ROWS, COLS, arr); copy_array(ROWS, COLS, arr, target); printf("The Target Array:/n"); show_array(ROWS, COLS, target); return 0; } void show_array(int rows, int cols, double vla[rows][cols]) { int r, c; for(r=0; r<rows; r++) { for(c=0; c<cols; c++) { printf("%.1f ", vla[r][c]); } putchar('/n'); } putchar('/n'); } void copy_array(int rows, int cols, double source[rows][cols], double target[rows][cols]) { int r, c; for(r=0; r<rows; r++) for(c=0; c<cols; c++) { target[r][c] = source[r][c]; } }