Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Dynamic Memory Allocation in 2D Arrays

Dynamic Memory Allocation in 2D Arrays | DSA in C++ - Software Development PDF Download

Introduction

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

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:

  • Determine the number of rows and columns for the 2D array.
  • Create an array of pointers of size equal to the number of rows.
  • For each row, allocate memory dynamically using the new operator and assign the address to the corresponding pointer in the array of pointers.

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:

  • We first prompt the user to enter the number of rows and columns for the 2D array.
  • We then create an array of pointers arr using the new operator. This array will store the addresses of the dynamically allocated rows.
  • Next, we use a loop to allocate memory for each row. The new operator is used to allocate memory for each row, and the address is assigned to the corresponding pointer in the array of pointers.
  • After allocating memory, we can access and modify individual elements of the 2D array using the square bracket notation, as shown in the example.
  • To deallocate memory, we use a loop to delete each row using the delete[] operator. Finally, we delete the array of pointers using delete[].

Sample Problems

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

Conclusion

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++. 

The document Dynamic Memory Allocation in 2D Arrays | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

mock tests for examination

,

Viva Questions

,

Dynamic Memory Allocation in 2D Arrays | DSA in C++ - Software Development

,

past year papers

,

pdf

,

Summary

,

practice quizzes

,

Free

,

ppt

,

shortcuts and tricks

,

Objective type Questions

,

study material

,

Dynamic Memory Allocation in 2D Arrays | DSA in C++ - Software Development

,

video lectures

,

Dynamic Memory Allocation in 2D Arrays | DSA in C++ - Software Development

,

Extra Questions

,

Sample Paper

,

Semester Notes

,

Previous Year Questions with Solutions

,

MCQs

,

Important questions

;