Hide
Know what you're getting – Unlike many sites, all our code is clearly licensed. Join Siafoo Now or Learn More

Print constant tridiagonal matrix Atom Feed 0

In Brief a = diagonal element b = sub-diagonal element c = super-diagonal element
# 's
 1  cout<<endl<<"A"<<endl;
2
3 for (int i = 0; i < n; ++i) {
4
5 for (int j = 0; j < n; ++j) {
6 if(i == j){
7 cout<<" "<<a<<" ";
8 }else if(j == i+1){
9 cout<<" "<<c<<" ";
10 }else if(j == i-1){
11 cout<<" "<<b<<" ";
12 }else{
13 cout<<" "<<0<<" ";
14 }
15
16 }
17 cout<<endl;
18 }
19 cout<<endl;
  • a = diagonal element
  • b = sub-diagonal element
  • c = super-diagonal element