-
Notifications
You must be signed in to change notification settings - Fork 3
/
10-14.c
79 lines (77 loc) · 1.9 KB
/
10-14.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 14. 以变长数组作为函数形参,完成编程练习13。
#include <stdio.h>
#define ROWS 3
#define COLS 5
void get_input_arr2d(int rows, int cols, double ar[rows][cols]);
void print_arr2d(int rows, int cols, double ar[rows][cols]);
double get_average(int n, double ar[n]);
double get_average2d(int rows, int cols, double ar[rows][cols]);
double get_max2d(int rows, int cols, double ar[rows][cols]);
int main(void)
{
int i;
double ar[ROWS][COLS];
get_input_arr2d(ROWS, COLS, ar);
printf("The array is: \n");
print_arr2d(ROWS, COLS, ar);
for (i = 0; i < ROWS; i++)
printf("The average of sub array %d is %.2f\n",
i + 1, get_average(COLS, ar[i]));
printf("The average of all value is %.2f\n",
get_average2d(ROWS, COLS, ar));
printf("The max value is %.2f\n",
get_max2d(ROWS, COLS, ar));
return 0;
}
void get_input_arr2d(int rows, int cols, double ar[rows][cols])
{
int i;
for (i = 0; i < ROWS; i++)
{
printf("Please enter 5 digits to fill the %d sub array: ", i + 1);
scanf("%lf %lf %lf %lf %lf",
ar[i] + 0, ar[i] + 1, ar[i] + 2, ar[i] + 3, ar[i] + 4);
}
}
void print_arr2d(int rows, int cols, double ar[rows][cols])
{
int r, c;
printf("[\n");
for (r = 0; r < rows; r++)
{
printf(" [ ");
for (c = 0; c < cols; c++)
{
printf("%.2f ", ar[r][c]);
}
printf("]\n");
}
printf("]\n");
}
double get_average(int n, double ar[n])
{
int i;
double total;
for (total = 0,i = 0; i < n; i++)
total += ar[i];
return total / n;
}
double get_average2d(int rows, int cols, double ar[rows][cols])
{
int r, c;
double total = 0;
for (r = 0; r < rows; r++)
for (c = 0; c < cols; c++)
total += ar[r][c];
return total / (rows * cols);
}
double get_max2d(int rows, int cols, double ar[rows][cols])
{
int r, c;
double max = ar[0][0];
for (r = 0; r < rows; r++)
for (c = 0; c < cols; c++)
if (ar[r][c] > max)
max = ar[r][c];
return max;
}