안선생의 개발 블로그
메트릭스2 본문
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
|
#include <iostream>
using namespace std;
class Matric { // 대각선 행렬
private:
int* A;
int n; //열수
public:
Matric()
{
n = 2;
A = new int[2];
}
Matric(int n)
{
this->n = n;
A = new int[n];
}
~Matric()
{
delete[] A;
}
public:
void Set(int i, int j, int x);
int Get(int i, int j);
void Display();
};
void Matric::Set(int i, int j, int x)
{
if (i == j)
A[i - 1] = x;
}
int Matric::Get(int i, int j)
{
if (i == j)
return A[i - 1];
else
return 0;
}
void Matric::Display()
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
cout << A[i]<<" "; //0부터 시작하므로 -1을 안해준다.
else
cout << "0 ";
}
cout << "\n";
}
}
int main()
{
Matric m(4);
m.Set(1, 1, 1); m.Set(2, 2, 2); m.Set(3, 3, 3); m.Set(4, 4, 4);
m.Display();
return 0;
};
|
cs |