In C++, arrays are a useful data structure for storing multiple elements of the same type. While creating arrays, it is often necessary to dynamically allocate memory to accommodate varying sizes of data. In this article, we will explore dynamic memory allocation in 2D arrays, which are arrays with multiple rows and columns. We will learn how to allocate memory dynamically for 2D arrays, access their elements, and deallocate the memory properly.
Dynamic memory allocation for 2D arrays involves creating an array of pointers, where each pointer points to a dynamically allocated row. The steps involved in allocating memory for a 2D array dynamically are as follows:
Let's look at an example to understand this process better:
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
// Step 1: Create an array of pointers
int** arr = new int*[rows];
// Step 2: Allocate memory for each row
for (int i = 0; i < rows; i++) {
arr[i] = new int[cols];
}
// Accessing and modifying array elements
arr[0][0] = 1;
arr[1][2] = 5;
// Step 3: Deallocate memory for each row
for (int i = 0; i < rows; i++) {
delete[] arr[i];
}
// Deallocate memory for the array of pointers
delete[] arr;
return 0;
}
Explanation:
Problems 1: Write a program to find the sum of elements in a dynamically allocated 2D array.
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
int** arr = new int*[rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[cols];
}
cout << "Enter the elements:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum += arr[i][j];
}
}
cout << "Sum of elements: " << sum << endl;
for (int i = 0; i < rows; i++) {
delete[] arr[i];
}
delete[] arr;
return 0;
}
Output:
Enter the number of rows: 2
Enter the number of columns: 3
Enter the elements:
1 2 3
4 5 6
Sum of elements: 21
Problems 2: Write a program to transpose a dynamically allocated 2D array.
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
int** arr = new int*[rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[cols];
}
cout << "Enter the elements:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
cout << "Original array:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << "Transposed array:" << endl;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
cout << arr[j][i] << " ";
}
cout << endl;
}
for (int i = 0; i < rows; i++) {
delete[] arr[i];
}
delete[] arr;
return 0;
}
Output:
Enter the number of rows: 3
Enter the number of columns: 2
Enter the elements:
1 2
3 4
5 6
Original array:
1 2
3 4
5 6
Transposed array:
1 3 5
2 4 6
Dynamic memory allocation in 2D arrays is an essential concept in C++ programming, especially in data structures and algorithms. By allocating memory dynamically, we can create flexible arrays that can accommodate varying sizes of data. Remember to deallocate the memory properly using delete[] to prevent memory leaks. With the knowledge gained from this article, you can now confidently work with dynamically allocated 2D arrays in C++.
153 videos|115 docs|24 tests
|
|
Explore Courses for Software Development exam
|