Answer to Question #201358 in C++ for Arjun

Question #201358

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.



1
Expert's answer
2021-06-01T02:59:53-0400


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog