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;
}
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:
- We first ask the user to input the size of the matrix m x n.
- We declare a matrix variable of size m x n to store the input values.
- We then ask the user to input the elements of the matrix and store them in the matrix variable using nested loops.
- Finally, we display the matrix on the screen using nested loops.
No comments:
Post a Comment
If you have any doubts, please let me know