-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path47.Toeplitz_matrix.cpp
67 lines (58 loc) · 1.11 KB
/
47.Toeplitz_matrix.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* toeplitz matrix
| 3 4 7 2 1 |
| 1 3 4 7 2 |
| 2 1 3 4 7 |
| 7 2 1 3 4 |
| 4 7 2 1 3 |
Total elements = n + n-1 = {3,1,2,7,4,4,7,2,1}
*/
#include<iostream>
using namespace std;
class lt_matrix{
int n;
int *A;
public:
lt_matrix(){
cout<<"Enter dimension: ";
cin>>n;
//number of elements - 2n-1
A = new int[2*n-1];
}
void set_all();
int get(int i, int j);
void display();
~lt_matrix(){
delete []A;
}
};
void lt_matrix::set_all(){
cout<<"Enter "<<2*n-1<<" elements: ";
for(int i=0;i<2*n-1;i++)
cin>>A[i];
}
int lt_matrix::get(int i,int j){
int temp;
//get element at given index
if(i>=j){
//these are stored first - lower(1st column)
return A[i-j];
} else{
//these are stored later - upper
return A[n+j-i-1];
}
}
void lt_matrix::display(){
//display matrix
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<get(i,j)<<" ";
}
cout<<"\n";
}
}
int main(){
lt_matrix m;
m.set_all();
m.display();
}