Skip to main content

Write a function to read a matrix of size m x n from the keyboard.

Write a function to read a matrix of size m x n from the keyboard.

here's an example program to read a matrix of size m x n from the keyboard:

#include <iostream>
using namespace std;
int main() {
    int m, n;
    cout << "Enter the size of matrix (m x n): ";
    cin >> m >> n;
    int matrix[m][n];
    cout << "Enter the elements of matrix:" << endl;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> matrix[i][j];
        }
    }
    cout << "The matrix is:" << endl;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

Explanation:

  1. We first ask the user to input the size of the matrix m x n.
  2. We declare a matrix variable of size m x n to store the input values.
  3. We then ask the user to input the elements of the matrix and store them in the matrix variable using nested loops.
  4. Finally, we display the matrix on the screen using nested loops.

Comments