-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbankoavg.c
52 lines (42 loc) · 1.13 KB
/
bankoavg.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
// Construct the average banko board!
#include <stdlib.h>
#include <stdio.h>
#include <error.h>
#include <stdint.h>
#include "bankopladeformat.h"
int main(int argc, char** argv) {
argv=argv;
if (argc != 1) {
error(1, 0, "This program does not take any options.");
}
struct banko_reader r;
struct board b;
double sums[BOARD_ROWS][BOARD_COLS] = { 0 };
int counts[BOARD_ROWS][BOARD_COLS] = { 0 };
banko_reader_open(&r, stdin);
while (banko_reader_board(&r, &b) == 0) {
for (int col = 0; col < BOARD_COLS; col++) {
for (int row = 0; row < BOARD_ROWS; row++) {
if (b.cells[row][col] != 0) {
sums[row][col] += b.cells[row][col];
counts[row][col]++;
}
}
}
}
banko_reader_close(&r);
struct banko_writer w;
banko_writer_open(&w, stdout);
for (int col = 0; col < BOARD_COLS; col++) {
for (int row = 0; row < BOARD_ROWS; row++) {
if (counts[row][col] > 0) {
b.cells[row][col] = sums[row][col] / counts[row][col];
} else {
b.cells[row][col] = 0;
}
}
}
banko_writer_board(&w, &b);
banko_writer_close(&w);
return 0;
}