第一题
#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; printf("YEAR PAINFALL (inches) "); for (year = 0, total = 0; year < YEARS; year++) { for (month = 0, subtot = 0; month < MONTHS; month++) { // 修改取值放hi是,用指针取值 subtot += *(*(rain + year) + month); } printf("%5d %15.1f ", 2010 + year, subtot); total += subtot; } printf(" The yearly average is %.1f inches. ", total / YEARS); printf("MONTHLY AVERAGES: "); printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct "); printf(" Nov Dec "); for (month = 0; month < MONTHS; month++) { for (year = 0, subtot = 0; year < YEARS; year++) { // 修改取值放hi是,用指针取值 subtot += *(*(rain + year) + month); } printf("%4.1f ",subtot / YEARS); // 每个月的平均降水量 } printf(" "); return 0; }
第二题
#include <stdio.h> void copy_arr(double target1[], double source[], int n); void copy_ptr(double target2[], double source[], int n); void copy_ptrs(double [], double [], double * last); int main(void) { 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); printf("source %p; target1 %p, target2 %p target3 %p. ", source, target1, target2, target3); printf("All index 1 is source %f; taeget1 %f; target2 %f target3 %f. ", *source, target1[0], *target2, target3[0]); return 0; } void copy_arr(double target1[], double source[], int n) { int i; for (i = 0; i < n; i++) { target1[i] = source [i]; } } // 记住一点,C语言函数对任何一个传进来的参数,都会进行一次拷贝,这样就可以保证,函数不会改变外部的参数。 void copy_ptr(double target2[], double source[], int n) { int i; for (i = 0; i < n; i++, source++) { *(target2 + i) = *source; } } void copy_ptrs(double target3[], double source[], double * last) { for (; source < last; target3++, source++) { *(target3) = *(source); } }
输出
source 0x7ffeefbff460; target1 0x7ffeefbff430, target2 0x7ffeefbff400 target3 0x7ffeefbff3d0.
All index 1 is source 1.100000; taeget1 1.100000; target2 1.100000 target3 1.100000.
第三题
#include <stdio.h> #define MEMBER_NUMBER 5 int max_number(int [], int); int main(void) { int ar[MEMBER_NUMBER] = {1, 9, 2, 4, 1}; int number; number = max_number(ar, MEMBER_NUMBER); printf("The max number is %d. ", number); return 0; } int max_number(int ar[], int n) { int max_num , i; for (i = 0, max_num = ar[i]; i < n; i++) { max_num = (max_num > ar[i]) ? max_num : ar[i]; } return max_num; }
输出
The max number is 9.
Program ended with exit code: 0
第四题
#include <stdio.h>
#define MEMBER_NUMBER 5
int max_index(double [], int);
int main(void)
{
double ar[MEMBER_NUMBER] = {1., 91.2, 12., 4., 11.12};
int index;
index = max_index(ar, MEMBER_NUMBER);
printf("The max index is %d. ", index);
return 0;
}
int max_index(double ar[], int n)
{
int i, index;
double max_num;
for (i = index = 0, max_num = ar[i]; i < n; i++) {
if (ar[i] > max_num) {
index = i;
max_num = ar[i];
}
}
return index;
}
输出
The max index is 1.
第五题
#include <stdio.h> #define MEMBER_NUMBER 5 double max_diff(double [], int); int main(void) { double ar[MEMBER_NUMBER] = {3.99, 9., 12., 4., 11.12}; double res; res = max_diff(ar, MEMBER_NUMBER); printf("The max gap is %.2f. ", res); return 0; } double max_diff(double ar[], int n) { int i; double max_number, min_number; for (max_number = min_number = *ar, i = 0; i < n; i++) { max_number = (max_number > ar[i]) ? max_number : ar[i]; min_number = (min_number < ar[i]) ? min_number : ar[i]; } return max_number - min_number; }
输出
The max gap is 8.01.
第六题
#include <stdio.h> #define MEMBER_NUMBER 4 void reveres(double [], int); void show(double ar[], int n); int main(void) { double ar[MEMBER_NUMBER] = {91.2, 12., 4., 11.12}; reveres(ar, MEMBER_NUMBER); printf("The max index is . "); show(ar, MEMBER_NUMBER); return 0; } // 用一个临时变量接受 void reveres(double ar[], int n) { int i; double temporary_number; for (i = 0; i < n / 2; i++) { temporary_number = ar[i]; ar[i] = ar[n-i-1]; ar[n-i-1] = temporary_number; } } void show(double ar[], int n) { int i; for (i = 0; i < n; i++) { printf("%f ", ar[i]); } printf("end. "); }
第七题
#include <stdio.h> void copy_arr(double target1[], double source[], int n); void copy_ptr(double target2[], double source[], int n); void copy_ptrs(double [], double [], double * last); int main(void) { double source[5] ={1.1, 2.2, 3.3, 4.4, 5.5}; double target1[5]; double target2[5]; double target3[5]; double source_two[2][3]= {1, 2, 3, 4, 5, 1}; double target4[2][3]; copy_arr(target1, source, 5); copy_ptr(target2, source, 5); copy_ptrs(target3, source, source + 5); // 分两次处理,第一次出来第一行,第二次处理第二行。 copy_ptr(target4[0], source_two[0], 3); copy_ptr(target4[1], source_two[1], 3); printf("%f ", target4[1][2]); printf("source %p; target1 %p, target2 %p target3 %p. ", source, target1, target2, target3); printf("All index 1 is source %f; taeget1 %f; target2 %f target3 %f. ", *source, target1[0], *target2, target3[0]); return 0; } void copy_arr(double target1[], double source[], int n) { int i; for (i = 0; i < n; i++) { target1[i] = source [i]; } } // 记住一点,C语言函数对任何一个传进来的参数,都会进行一次拷贝,这样就可以保证,函数不会改变外部的参数。 void copy_ptr(double target2[], double source[], int n) { int i; for (i = 0; i < n; i++, source++) { *(target2 + i) = *source; } } void copy_ptrs(double target3[], double source[], double * last) { for (; source < last; target3++, source++) { *(target3) = *(source); } }
第八题
double source_7[7] = {1, 2, 3, 4, 5, 6, 7}; double target_3[3]; copy_arr(target_3, source_7+2, 3); printf("%f ", target_3[2]);
复制了中间一小段代码
第九题
#include <stdio.h> #define ROWS 3 #define COLUMNS 5 void copy_arr(int r, int c, double soucre[r][c], double target[r][c]); void show_arr(int r, int c, double soucre[r][c], double target[r][c]); int main(void) { int row = 3, col = 5; double s_arr[ROWS][COLUMNS] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; double t_arr[row][col]; copy_arr(row, col, s_arr, t_arr); show_arr(row, col, s_arr, t_arr); return 0; } // 拷贝数组 void copy_arr(int r, int c, double soucre[r][c], double target[r][c]) { int nr, nc; for (nr = 0; nr < r; nr++) { for (nc = 0; nc < c; nc++) { target[nr][nc] = soucre[nr][nc]; } } } // 显式数组 void show_arr(int r, int c, double soucre[r][c], double target[r][c]) { int nr, nc; for (nr = 0; nr < r; nr++) { for (nc = 0; nc < c; nc++) { printf("%f ", soucre[nr][nc]); } } putchar(' '); for (nr = 0; nr < r; nr++) { for (nc = 0; nc < c; nc++) { printf("%f ", target[nr][nc]); } } putchar(' '); }
第十题
#include <stdio.h> #define COUNT 4 void sum_arr(int add1[], int add2[], int res[], int n); void show(int ar[], int n); int main(void) { int ar1[COUNT] = {1, 2, 3, 4}; int ar2[COUNT] = {4, 3, 2, 1}; int res[COUNT]; sum_arr(ar1, ar2, res, COUNT); show(res, COUNT); return 0; } void sum_arr(int add1[], int add2[], int res[], int n) { int i; for (i = 0; i < n; i++) { res[i] = add1[i] + add2[i]; } } void show(int ar[], int n) { int i; for (i = 0; i < n; i++) { printf("%d ", ar[i]); } printf(" *** end *** "); }
第十一题
#include <stdio.h> #define ROWS 3 #define COLUMNS 5 void arr_double(int ar[][COLUMNS], int n); void show(int ar[][COLUMNS], int n); int main(void) { int ar[ROWS][COLUMNS] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; show(ar, ROWS); arr_double(ar, ROWS); show(ar, ROWS); return 0; } // 原数值翻倍 void arr_double(int ar[][COLUMNS], int n) { int r, c; for (r = 0; r < n; r++) { for (c = 0; c < COLUMNS; c++) { ar[r][c] *= 2; } } } // 显式数值 void show(int ar[][COLUMNS], int n) { int r, c; for (r = 0; r < n; r++) { for (c = 0; c < COLUMNS; c++) { printf("%d ", ar[r][c]); } printf(" "); } printf(" *** end *** "); }
第十二题
#include <stdio.h> #define MONTHS 12 #define YEARS 5 void show_year_info(const double rain[][MONTHS], int row); void show_month_info(const double rain[][MONTHS], int row); int main(void) { const double 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} }; show_year_info(rain, YEARS); show_month_info(rain, YEARS); return 0; } // 记得函数定义const,因为传入的指针就定义了const void show_year_info(const double rain[][MONTHS], int row) { float subtot, total; int year, month; printf("YEAR PAINFALL (inches) "); for (year = 0, total = 0; year < row; year++) { for (month = 0, subtot = 0; month < MONTHS; month++) { subtot += rain[year][month]; } printf("%5d %15.1f ", 2010 + year, subtot); total += subtot; } printf(" The yearly average is %.1f inches. ", total / YEARS); } void show_month_info(const double rain[][MONTHS], int row) { float subtot; int year, month; printf("MONTHLY AVERAGES: "); printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct "); printf(" Nov Dec "); for (month = 0; month < MONTHS; month++) { for (year = 0, subtot = 0; year < row; year++) { subtot += rain[year][month]; } printf("%4.1f ",subtot / YEARS); // 每个月的平均降水量 } printf(" "); }
第十三题
#include <stdio.h> #define ROWS 3 #define COLUMNS 5 void scanf_elemet(double ar[][COLUMNS], int row); double average_serier(const double ar[], int columns); double average_all(double ar[][COLUMNS], int row); double max_number(double ar[][COLUMNS], int row); void show(double ar[][COLUMNS], int n); int main(void) { int i; double ar[ROWS][COLUMNS]; scanf_elemet(ar, ROWS); for (i = 0; i < ROWS; i++) { average_serier(ar[i], COLUMNS); } average_all(ar, ROWS); max_number(ar, ROWS); show(ar, ROWS); return 0; } //找出最大值 double max_number(double ar[][COLUMNS], int row) { int r, c; double max_num; for (r = 0, max_num = **ar; r < row; r++) { for (c = 0; c < COLUMNS; c++) { max_num = (max_num > ar[r][c]) ? max_num : ar[r][c]; } } printf("The max number is %g. ", max_num); return max_num; } /* 计算所有值的平均值 */ double average_all(double ar[][COLUMNS], int row) { int r, c; double total, aver_num; for (r = 0, total = 0; r < row; r++) { for (c = 0; c < COLUMNS; c++) { total += ar[r][c]; } } aver_num = total / (row * COLUMNS); printf("All average is %g. ", aver_num); return aver_num; } // 计算以为数组的平均值,调用三次 double average_serier(const double ar[], int colums) { int i; double total, aver_num; for (i = 0, total = 0; i < colums; i++) { total += ar[i]; } aver_num = total / colums; printf("average is %g. ", aver_num); return aver_num; } // 接受15个数组的输入 void scanf_elemet(double ar[][COLUMNS], int row) { int r, c; for (r = 0; r < row; r ++) { printf("Please enter the %d float number: ", COLUMNS); for (c = 0; c < COLUMNS; c++) { scanf("%lf", &ar[r][c]); } } } void show(double ar[][COLUMNS], int n) { int r, c; for (r = 0; r < n; r++) { for (c = 0; c < COLUMNS; c++) { printf("%g ", ar[r][c]); } printf(" "); } printf(" *** end *** "); }
第十四题
#include <stdio.h> #define ROWS 3 #define COLUMNS 5 void scanf_elemet(int row, int column,double ar[row][column]); double average_serier(const double ar[], int columns); double average_all(int row, int column,double ar[row][column]); double max_number(int row, int column,double ar[row][column]); void show(int row, int column,double ar[row][column]); int main(void) { int i; double ar[ROWS][COLUMNS]; scanf_elemet(ROWS, COLUMNS, ar); for (i = 0; i < ROWS; i++) { average_serier(ar[i], COLUMNS); } average_all(ROWS, COLUMNS, ar); max_number(ROWS, COLUMNS, ar); show(ROWS, COLUMNS, ar); return 0; } //找出最大值 double max_number(int row, int column,double ar[row][column]) { int r, c; double max_num; for (r = 0, max_num = **ar; r < row; r++) { for (c = 0; c < column; c++) { max_num = (max_num > ar[r][c]) ? max_num : ar[r][c]; } } printf("The max number is %g. ", max_num); return max_num; } /* 计算所有值的平均值 */ double average_all(int row, int column,double ar[row][column]) { int r, c; double total, aver_num; for (r = 0, total = 0; r < row; r++) { for (c = 0; c < column; c++) { total += ar[r][c]; } } aver_num = total / (row * COLUMNS); printf("All average is %g. ", aver_num); return aver_num; } // 计算以为数组的平均值,调用三次 double average_serier(const double ar[], int colums) { int i; double total, aver_num; for (i = 0, total = 0; i < colums; i++) { total += ar[i]; } aver_num = total / colums; printf("average is %g. ", aver_num); return aver_num; } // 接受15个数组的输入 void scanf_elemet(int row, int column,double ar[row][column]) { int r, c; for (r = 0; r < row; r ++) { printf("Please enter the %d float number: ", COLUMNS); for (c = 0; c < column; c++) { scanf("%lf", &ar[r][c]); } } } void show(int row, int column,double ar[row][column]) { int r, c; for (r = 0; r < row; r++) { for (c = 0; c < column; c++) { printf("%g ", ar[r][c]); } printf(" "); } printf(" *** end *** "); }