-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise14.c
76 lines (60 loc) · 1.57 KB
/
exercise14.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
// C Primer Plus
// Chapter 10 Exercise 14:
// Do Programming Exercise 13, but use variable-length array function
// parameters.
#include <stdio.h>
#define ROWS 3
#define COLUMNS 5
double compute_row_average(double *array, int cols);
double compute_array_average(int rows, int cols, double array[rows][cols]);
double largest_value(int rows, int cols, double array[rows][cols]);
int main(void)
{
double data[ROWS][COLUMNS];
for (int i = 0; i < ROWS; i++)
{
printf("Enter set of %d doubles: ", COLUMNS);
for (int j = 0; j < COLUMNS; j++)
{
scanf("%lf", data[i] + j);
}
}
// print row averages
printf("Row Averages:\n");
for (int i = 0; i < ROWS; i++)
{
printf("\tAverage for row %d: %.3f\n", i + 1,
compute_row_average(data[i], COLUMNS));
}
// print array average
printf("Average for entire array: %.3f\n",
compute_array_average(ROWS, COLUMNS, data));
// print largest value
printf("Maximum array value: %.3f\n",
largest_value(ROWS, COLUMNS, data));
return 0;
}
double compute_row_average(double *array, int cols)
{
double total = 0;
for (int i = 0; i < cols; i++)
total += array[i];
return total / cols;
}
double compute_array_average(int rows, int cols, double array[rows][cols])
{
double total = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
total += array[i][j];
return total / (rows * cols);
}
double largest_value(int rows, int cols, double array[rows][cols])
{
double max = array[0][0];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (array[i][j] > max)
max = array[i][j];
return max;
}