Create a template class for a Matrix(2d array) and write the following functions in it.
1. Print the left diagonal
2. Print the right diagonalÂ
3. Print specified row
4. Print specified column
5. Print overall matrix.
#include <iostream>
using namespace std;
template <class T>
class Matrix
{
private:
T arr[100][100];
public:
void printLeftDnl(){
  for (int i = 0; i <5; i++) {
    for (int j = 0; j <5; j++) {
      if (i == j)
        cout << arr[i][j] << ", ";
    }
  }
  cout << endl;Â
}
void printRightDnl(){
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      if ((i + j) == (5 - 1))
        cout << arr[i][j] << ", ";
    }
  }
  cout << endl;
}
void printSpecRow(){
  int r;
  cout<<"\nEnter row to print: ";
  cin>>r;
  for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){
      if (r==i){
        cout<<arr[i][j];
      }
    }
    cout<<endl;Â
  }
}
void printSpecCol(){
  int c;
  cout<<"\nEnter column to print: ";
  cin>>c;
  for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){
      if (j==c){
        cout<<arr[i][j];
      }
    }
    cout<<endl;Â
  }
}
void printAll(){
  for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){
      cout<<arr[i][j];
    }
    cout<<endl;
  }
}
};
int main()
{
  Â
  return 0;
}
Comments
Leave a comment