In C++, a matrix is a two-dimensional array, representing a collection of elements arranged in rows and columns. It is an essential data structure used in various applications, such as mathematics, computer graphics, and scientific simulations. C++ allows you to create matrices using nested arrays or vector of vectors.
Here’s an example of creating a matrix using a nested array and a vector of vectors, along with code explanations:
Table of Contents
Matrix Using Nested Arrays
#include <iostream>
int main() {
// Define a matrix using a nested array
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Access and display elements of the matrix
std::cout << "Matrix elements using nested array:" << std::endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Explanation:
- In this example, we declare a matrix
matrix
using a nested array. The matrix has 3 rows and 3 columns, and each element is of typeint
. - We initialize the elements of the matrix using the nested curly braces.
- To access and display the elements of the matrix, we use nested loops. The outer loop iterates over the rows, and the inner loop iterates over the columns.
Matrix Using Vector of Vectors
#include <iostream>
#include <vector>
int main() {
// Define a matrix using vector of vectors
std::vector<std::vector<int>> matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Access and display elements of the matrix
std::cout << "Matrix elements using vector of vectors:" << std::endl;
for (size_t i = 0; i < matrix.size(); i++) {
for (size_t j = 0; j < matrix[i].size(); j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Explanation:
- In this example, we declare a matrix
matrix
using a vector of vectors. The matrix has 3 rows, and each row is represented by a vector ofint
. - We initialize the elements of the matrix using the nested curly braces.
- To access and display the elements of the matrix, we use nested loops. The outer loop iterates over the rows, and the inner loop iterates over the elements in each row (columns).
Dynamic Matrix Creation
In C++, you can also create matrices with dynamic sizes using pointers or vectors.
Using Pointers:
#include <iostream>
int main() {
int rows, cols;
std::cout << "Enter the number of rows: ";
std::cin >> rows;
std::cout << "Enter the number of columns: ";
std::cin >> cols;
// Create a dynamic matrix using pointers
int** matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
// Access and display elements of the matrix
std::cout << "Enter the elements of the matrix:" << std::endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cin >> matrix[i][j];
}
}
std::cout << "Matrix elements using pointers:" << std::endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
// Deallocate memory
for (int i = 0; i < rows; i++) {
delete[] matrix[i];
}
delete[] matrix;
return 0;
}
Using Vectors:
#include <iostream>
#include <vector>
int main() {
int rows, cols;
std::cout << "Enter the number of rows: ";
std::cin >> rows;
std::cout << "Enter the number of columns: ";
std::cin >> cols;
// Create a dynamic matrix using vector of vectors
std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols));
// Access and display elements of the matrix
std::cout << "Enter the elements of the matrix:" << std::endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cin >> matrix[i][j];
}
}
std::cout << "Matrix elements using vector of vectors:" << std::endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Explanation:
- In the dynamic matrix creation examples, the user is asked to enter the number of rows and columns for the matrix.
- A dynamic matrix is created using either pointers or vector of vectors based on user input.
- The user is then asked to input the elements of the matrix, and the matrix is displayed using nested loops.
In C++, matrices can be created using nested arrays, vector of vectors, or dynamically using pointers or vectors. The choice of matrix representation depends on factors like the size of the matrix, memory management, and ease of use. Remember to deallocate memory for dynamically allocated matrices to avoid memory leaks.