c
1#include <stdio.h>
2#include <math.h>
3#include <memory.h>
4
5#define NUMBER_OF_COLUMNS 3
6#define NUMBER_OF_ROWS 5
7
8#define ALL -1
9
10void input(int table[][NUMBER_OF_COLUMNS]) {
11 for(int i=0; i<NUMBER_OF_ROWS; i++) {
12 for(int j=0; j<NUMBER_OF_COLUMNS; j++) {
13 printf("%d番: 科目%d=", i+1, j+1);
14 fflush(stdout);
15 scanf("%d", &table[i][j]);
16 }
17 }
18}
19
20int average(int table[][NUMBER_OF_COLUMNS], int row, int column) {
21 int rowStart = row == ALL ? 0 : row;
22 int rowEnd = row == ALL ? NUMBER_OF_ROWS : row+1;
23
24 int columnStart = column == ALL ? 0 : column;
25 int columnEnd = column == ALL ? NUMBER_OF_COLUMNS : column+1;
26
27 int total = 0;
28 int count = 0;
29 for(row=rowStart; row<rowEnd; row++) {
30 for(column=columnStart; column<columnEnd; column++) {
31 total += table[row][column];
32 count ++;
33 }
34 }
35
36 return round((double)total / count);
37}
38
39void outputHeader() {
40 for(int column=0; column<NUMBER_OF_COLUMNS; column++) {
41 printf("科目%d ", column+1);
42 }
43 printf("%d科目の平均点\n", NUMBER_OF_COLUMNS);
44}
45
46void outputSeparator() {
47 for(int i=0; i<NUMBER_OF_COLUMNS*8+13; i++) {
48 printf("-");
49 }
50 printf("\n");
51}
52
53void outputRow(int table[][NUMBER_OF_COLUMNS], int row) {
54 for(int column=0; column<NUMBER_OF_COLUMNS; column++) {
55 printf("%4d ", table[row][column]);
56 }
57 printf("%4d\n", average(table, row, ALL));
58}
59
60void outputSummary(int table[][NUMBER_OF_COLUMNS]) {
61 for(int column=0; column<NUMBER_OF_COLUMNS; column++) {
62 printf("%4d ", average(table, ALL, column));
63 }
64 printf("%4d\n", average(table, ALL, ALL));
65}
66
67void output(int table[][NUMBER_OF_COLUMNS]) {
68 printf("\n");
69 outputSeparator();
70
71 outputHeader();
72 for(int row=0; row<NUMBER_OF_ROWS; row++) {
73 outputRow(table, row);
74 }
75 outputSeparator();
76
77 outputSummary(table);
78 outputSeparator();
79}
80
81int main() {
82 int table[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
83 memset(&table, 0, sizeof(table));
84
85 input(table);
86 output(table);
87
88 return 0;
89}