-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmatrix.c
116 lines (84 loc) · 2.44 KB
/
matrix.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* matrix.c
Multiply two matrices.
by: Steven Skiena
begun: July 28, 2005
*/
/*
Copyright 2005 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include <stdlib.h>
#include "bool.h"
/************************************************************/
#define MAXV 100 /* maximum number of vertices */
#define MAXDEGREE 50 /* maximum outdegree of a vertex */
#define MAXINT 100007
typedef struct {
int m[MAXV+1][MAXV+1]; /* adjacency/weight info */
int rows; /* number of rows */
int columns; /* number of columns */
} matrix;
void initialize_matrix(matrix *m) {
int i, j; /* counters */
for (i = 1; i <= m->rows; i++) {
for (j = 1; j <= m->columns; j++) {
m->m[i][j] = 0;
}
}
}
void read_matrix(matrix *m) {
int i, j; /* counters */
scanf("%d %d\n", &(m->rows), &(m->columns));
for (i = 1; i <= m->rows; i++) {
for (j = 1; j <= m->columns; j++) {
scanf("%d", &m->m[i][j]);
}
}
}
void print_matrix(matrix *g) {
int i, j; /* counters */
for (i = 1; i <= g->rows; i++) {
for (j = 1; j <= g->columns; j++) {
printf(" %d",g->m[i][j]);
}
printf("\n");
}
printf("\n");
}
void multiply(matrix *a, matrix *b, matrix *c) {
int i, j, k; /* dimension counters */
if (a->columns != b->rows) {
printf("Error: bounds dont match!\n");
return;
}
c->rows = a->rows;
c->columns = b->columns;
/* [[[ matrix_cut */
for (i = 1; i <= a->rows; i++) {
for (j = 1; j <= b->columns; j++) {
c->m[i][j] = 0;
for (k = 1; k <= b->rows; k++) {
c->m[i][j] += a->m[i][k] * b->m[k][j];
}
}
}
/* ]]] */
}
int main(void) {
matrix a, b, c;
read_matrix(&a);
print_matrix(&a);
read_matrix(&b);
print_matrix(&b);
multiply(&a, &b, &c);
print_matrix(&c);
return 0;
}