//本博主所写的代码仅为阅读者提供参考;
//若有不足之处请提出,博主会尽所能修改;
//附上课后编程练习题目;
//若是对您有用的话请点赞或分享提供给它人;
//10.13 - 1.c
#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 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.1f\n", 2010 + 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 ");
printf(" 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.c
#include <stdio.h>
#define LEN 5
void copy_arr(double x[], const double source[], int n);
void copy_ptr(double *x, const double *source, int n);
void copy_ptrs(double *x, const double *source, const double *end);
void show_arr(const double x[], int n);
int main(void)
{
double source[LEN] = {
1.1, 2.2, 3.3, 4.4, 5.5};
double target1[LEN];
double target2[LEN];
double target3[LEN];
printf("Source array:\n");
show_arr(source, LEN);
copy_arr(target1, source, LEN);
printf("Target1:\n");
show_arr(target1, LEN);
copy_ptr(target2, source, LEN);
printf("Target2:\n");
show_arr(target2, LEN);
copy_ptrs(target3, source, source + LEN);
printf("Target3:\n");
show_arr(target3, LEN);
printf("Done.\n");
return 0;
}
void show_arr(const double x[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%-5g", x[i]);
}
putchar('\n');
return;
}
void copy_arr(double x[], const double source[], int n)
{
int i;
for (i = 0; i < n; i++)
{
x[i] = source[i];
}
return;
}
void copy_ptr(double *x, const double *source, int n)
{
int i;
for (i = 0; i < n; i++)
{
*(x + i) = *(source + i);
}
return;
}
void copy_ptrs(double *x, const double *source, const double *end)
{
int i;
for (i = 0; i < end - source; i++)
{
*(x + i) = *(source + i);
}
return;
}
//-------------
//10.13 - 3.c
#include <stdio.h>
#define N 5
int find_max(const int a[], int n);
void show_array(const int a[], int n);
int main(void)
{
int max;
int array[N] = {
1, 4, 3, 2, 5};
printf("Array:\n");
show_array(array, N);
max = find_max(array, N);
printf("The maximum is %d.\n", max);
return 0;
}
int find_max(const int a[], int n)
{
int i;
int max = a[0];
for (i = 1; i < n; i++)
{
max = max < a[i] ? a[i] : max;
}
return max;
}
void show_array(const int a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%-3d", a[i]);
}
putchar('\n');
return;
}
//-------------
//10.13 - 4.c
#include <stdio.h>
#define N 5
int find_max_index(const double a[], int n);
void show_array(const double a[], int n);
int main(void)
{
double t;
int max_index;
double array[N] = {
1.0, 4.0, 3.0, 2.0, 5.0};
printf("Array:\n");
show_array(array, N);
max_index = find_max_index(array, N);
printf("Maximum index is %d.\n", max_index);
return 0;
}
int find_max_index(const double a[], int n)
{
int j, index;
double max = a[0];
for (j = 1; j < n; j++)
{
if (max < a[j])
{
max = a[j];
index = j;
}
}
return index;
}
void show_array(const double a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%-5g", a[i]);
}
putchar('\n');
return;
}
//-------------
//10.13 - 5.c
#include <stdio.h>
#define N 5
int d_value(const double a[], int n);
void show_array(const double a[], int n);
int main(void)
{
double m, n, val;
double array[N] = {
1.0, 4.0, 3.0, 2.0, 5.0};
printf("Array:\n");
show_array(array, N);
val = d_value(array, N);
printf("Maximum minus minimum is %g.\n", val);
return 0;
}
int d_value(const double a[], int n)
{
int i;
double max = a[0];
double min = a[0];
for (i = 1; i < n; i++)
{
max = max < a[i] ? a[i] : max;
min = min > a[i] ? a[i] : min;
}
return max - min;
}
void show_array(const double a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%-5g", a[i]);
}
putchar('\n');
return;
}
//-------------
//10.13 - 6.c
#include <stdio.h>
#define N 5
void reverse(double a[], int n);
void show_array(const double a[], int n);
int main(void)
{
double array[N] = {
1.0, 4.0, 3.0, 2.0, 5.0};
printf("Array:\n");
show_array(array, N);
reverse(array, N);
printf("After reversing:\n");
show_array(array, N);
return 0;
}
void reverse(double a[], int n)
{
int i, t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = t;
}
return;
}
void show_array(const double a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%-5g", a[i]);
}
putchar('\n');
return;
}
//-------------
//10.13 - 7.c
#include <stdio.h>
#define N 2
#define M 3
void copy_arr(const double a[], double b[], int n);
void show_array(double (*x)[M], int n);
int main(void)
{
int i;
double a[N][M] =
{
{
1.0, 2.0, 3.0},
{
4.0, 5.0, 6.0}
};
double b[N][M] = {
0.0};
printf("Array a:\n");
show_array(a, N);
printf("Array b:\n");
show_array(b, N);
for (i = 0; i < N; i++)
{
copy_arr(a[i], b[i], M);
}
printf("Copying array a to array b is:\n");
show_array(b, N);
return 0;
}
void copy_arr(const double a[], double b[], int n)
{
int i;
for (i = 0; i < n; i++)
{
b[i] = a[i];
}
return;
}
void show_array(double (*x)[M], int n)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < M; j++)
{
printf("%-5g", x[i][j]);
}
putchar('\n');
}
return;
}
//-------------
//10.13 - 8.c
#include <stdio.h>
#define LEN1 7
#define LEN2 3
void copy_arr(double ar1[], const double ar2[], int n);
void show_arr(const double ar[], int n);
int main(void)
{
double orig[LEN1] = {
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
double copy[LEN2];
printf("Array:\n");
show_arr(orig, LEN1);
printf("Copy 3 to 5 for target array:\n");
copy_arr(copy, orig + 2, LEN2);
show_arr(copy, LEN2);
return 0;
}
void copy_arr(double ar1[], const double ar2[], int n)
{
int i;
for (i = 0; i < n; i++)
{
ar1[i] = ar2[i];
}
return;
}
void show_arr(const double ar[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%-5g", ar[i]);
}
putchar('\n');
return;
}
//-------------
//10.13 - 9.c
#include <stdio.h>
#define N 3
#define M 5
void show_array(int n, int m, const double x[n][m]);
void copy_array(int n, int m, const double a[n][m], double b[n][m]);
int main(void)
{
const double a[N][M] =
{
{
1.0,2.0,3.0,4.0,5.0},
{
6.0,7.0,8.0,9.0,10.0},
{
11.0,12.0,13.0,14.0,15.0}
};
double b[N][M] = {
0.0};
printf("Array a:\n");
show_array(N, M, a);
printf("Array b:\n");
show_array(N, M, b);
copy_array(N, M, a, b);
printf("Copy array a to array b:\n");
show_array(N, M, b);
return 0;
}
void show_array(int n, int m, const double x[n][m])
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%-5g", x[i][j]);
}
putchar('\n');
}
return;
}
void copy_array(int n, int m, const double a[n][m], double b[n][m])
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
b[i][j] = a[i][j];
}
}
return;
}
//-------------
//10.13 - 10.c
#include <stdio.h>
#define N 4
void add_array(const int a[], const int b[], int c[], int n);
void show_array(const int x[], int n);
int main(void)
{
int a[N] = {
2, 4, 5, 8};
int b[N] = {
1, 0, 4, 6};
int c[N] = {
0};
printf("Array a:\n");
show_array(a, N);
printf("Array b:\n");
show_array(b, N);
printf("Array c:\n");
show_array(c, N);
add_array(a, b, c, N);
printf("Array a + array b to array c is:\n");
show_array(c, N);
return 0;
}
void add_array(const int a[], const int b[], int c[], int n)
{
int i;
for (i = 0; i < n; i++)
{
c[i] = a[i] + b[i];
}
return;
}
void show_array(const int x[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%-3d", x[i]);
}
putchar('\n');
return;
}
//-------------
//10.13 - 11.c
#include <stdio.h>
#define N 3
#define M 5
void show(int (*a)[M], int n);
void two_times(int (*a)[M], int n);
int main(void)
{
int a[N][M] =
{
{
1, 2, 3, 4, 5},
{
6, 7, 8, 9, 10},
{
11, 12, 13, 14, 15}
};
printf("Array:\n");
show(a, N);
two_times(a, N);
printf("Double array is:\n");
show(a, N);
return 0;
}
void show(int (*a)[M], int n)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < M; j++)
{
printf("%-5d", a[i][j]);
}
putchar('\n');
}
return;
}
void two_times(int (*a)[M], int n)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < M; j++)
{
a[i][j] *= 2;
}
}
return;
}
//-------------
//10.13 - 12.c
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
void rainfall_total(const float (*rain)[MONTHS], int years);
void rainfall_aver(const float (*rain)[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},
};
rainfall_total(rain, YEARS);
rainfall_aver(rain, YEARS);
return 0;
}
void rainfall_total(const float (*rain)[MONTHS], int years)
{
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.1lf\n", 2010 + year, subtot);
total += subtot;
}
printf("\nThe yearly average is %.1f inches.\n\n", total / years);
return;
}
void rainfall_aver(const float (*rain)[MONTHS], int years)
{
float subtot;
int month, year;
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" 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);
}
putchar('\n');
return;
}
//-------------
//10.13 - 13.c
#include <stdio.h>
#define ROWS 3
#define COLS 5
void store(double ar[], int n);
double average2d(double ar[][COLS], int rows);
double max2d(double ar[][COLS], int rows);
void showarr2(double ar[][COLS], int rows);
double average(const double ar[], int n);
int main(void)
{
int row;
double stuff[ROWS][COLS];
for (row = 0; row < ROWS; row++)
{
printf("Please enter %d numbers for %d row\n", COLS, row + 1);
store(stuff[row], COLS);
}
printf("Array:\n");
showarr2(stuff, ROWS);
for (row = 0; row < ROWS; row++)
{
printf("Average for row %d is %g.\n", row + 1, average(stuff[row], COLS));
}
printf("Average is %g.\n", average2d(stuff, ROWS));
printf("Maximum is %g.\n", max2d(stuff, ROWS));
printf("Done.\n");
return 0;
}
void store(double ar[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("Please enter a number: ", i + 1);
scanf("%lf", &ar[i]);
}
return;
}
double average2d(double ar[][COLS], int rows)
{
int i, j;
double sum = 0.0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < COLS; j++)
{
sum += ar[i][j];
}
}
if (rows * COLS > 0)
{
return sum / (rows * COLS);
}
else
{
return 0.0;
}
}
double max2d(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++)
{
max = max < ar[i][j] ? ar[i][j] : max;
}
}
return max;
}
void showarr2(double ar[][COLS], int rows)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < COLS; j++)
{
printf("%-5g", ar[i][j]);
}
putchar('\n');
}
return;
}
double average(const 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;
}
}
//-------------
//10.13 - 14.c
#include <stdio.h>
#define ROWS 3
#define COLS 5
void store(int n, double ar[n]);
double average2d(int rows, int cols, double ar[rows][cols]);
double max2d(int rows, int cols, double ar[rows][cols]);
void showarr2(int rows, int cols, double ar[rows][cols]);
double average(int n, const double ar[n]);
int main(void)
{
int row;
double stuff[ROWS][COLS];
for (row = 0; row < ROWS; row++)
{
printf("Please enter %d numbers for %d row\n", COLS, row + 1);
store(COLS, stuff[row]);
}
printf("Array:\n");
showarr2(ROWS, COLS, stuff);
for (row = 0; row < ROWS; row++)
{
printf("Average for row %d is %g.\n", row + 1, average(COLS, stuff[row]));
}
printf("Average is %g.\n", average2d(ROWS, COLS, stuff));
printf("Maximum is %g.\n", max2d(ROWS, COLS, stuff));
printf("Done.\n");
return 0;
}
void store(int n, double ar[n])
{
int i;
for (i = 0; i < n; i++)
{
printf("Please enter a number: ", i + 1);
scanf("%lf", &ar[i]);
}
return;
}
double average2d(int rows, int cols, double ar[rows][cols])
{
int i, j;
double sum = 0.0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
sum += ar[i][j];
}
}
if (rows * cols > 0)
{
return sum / (rows * cols);
}
else
{
return 0.0;
}
}
double max2d(int rows, int cols, double ar[rows][cols])
{
int i, j;
double max = ar[0][0];
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
max = max < ar[i][j] ? ar[i][j] : max;
}
}
return max;
}
void showarr2(int rows, int cols, double ar[rows][cols])
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%-5g", ar[i][j]);
}
putchar('\n');
}
return;
}
double average(int n, const double ar[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;
}
}
//-------------
//----------------------------2020年4月4日 -------------------------------
//致敬英雄,缅怀同胞,愿山河无恙,人间皆安!