-
Notifications
You must be signed in to change notification settings - Fork 15
/
ex.3.71.cpp
51 lines (39 loc) · 1.26 KB
/
ex.3.71.cpp
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
// ex.3.71
// en: Modify Program 3.18 to take the number of vertices as a
// command-line argument, then dynamically allocate the adjacency
// matrix.
// ru: Измените программу 3.18 таким образом, чтобы она принимала
// количество вершин в качестве аргумента командной строки, а затем
// динамически выделяла память под матрицу смежности.
#include <iostream>
#include <psv/array.h>
int usage(const char* bin) {
std::cout << "Usage: " << bin << " <positive int V>\n";
return 1;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
return usage(argv[0]);
}
const int V = std::atoi(argv[1]);
if (V <= 0) {
return usage(argv[0]);
}
auto adj = psv::Array2D<int>(V, V);
int i, j;
for (i = 0; i < V; ++i) {
for (j = 0; j < V; ++j) {
adj.data[i][j] = 0;
}
}
for (i = 0; i < V; ++i) adj.data[i][i] = 1;
while (std::cin >> i >> j) {
adj.data[i][j] = 1;
adj.data[j][i] = 1;
}
for (i = 0; i < V; ++i) {
for (j = 0; j < V; ++j) {
std::cout << adj.data[i][j] << (j + 1 < V ? ' ' : '\n');
}
}
}