-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_HW2.cpp
More file actions
71 lines (60 loc) · 1.07 KB
/
3_HW2.cpp
File metadata and controls
71 lines (60 loc) · 1.07 KB
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
#include <iostream>
#include <iomanip>
using namespace std;
int** buildTable(int n);
void make_identity_matrix(int** table, int n);
void printTable(int** m, int n);
int main() {
int n = 0;
cout << "N을 입력하시오 : ";
cin >> n;
if (n < 1) {
cout << "\n행렬을 생성할 수 없습니다.\n" << endl;
exit(EXIT_FAILURE);
}
int** table = buildTable(n);
make_identity_matrix(table, n);
printTable(table, n);
for (int i = 0; i < n; i++)
delete[] table[i];
delete table;
return 0;
}
int** buildTable(int n)
{
int** mat = new int*[n];
for (int i = 0; i < n; ++i)
{
mat[i] = new int[n];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
mat[i][j] = 0;
}
}
return mat;
}
void make_identity_matrix(int** table, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; ++j)
{
if(i==j)
table[i][i] = 1;
}
}
}
void printTable(int** m, int n)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cout << setw(4)<< m[i][j];
}
cout << '\n';
}
}