

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I declare a 2d array in C++ using new
A dynamic 2D array is basically an array of pointers to arrays. Here is a diagram of a 2D array with dimenation 3 x 4.
Algorithm
Begin Declare dimension of the array. Dynamic allocate 2D array a[][] using new. Fill the array with the elements. Print the array. Clear the memory by deleting it. End
Example Code
#include <iostream> using namespace std; int main() { int B = 4; int A = 5; int** a = new int*[B]; for(int i = 0; i < B; ++i) a[i] = new int[A]; for(int i = 0; i < B; ++i) for(int j = 0; j < A; ++j) a[i][j] = i; for(int i = 0; i < B; ++i) for(int j = 0; j < A; ++j) cout << a[i][j] << " "; for(int i = 0; i < A; ++i) delete [] a[i]; delete [] a; return 0; }
Output
0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
- Related Questions & Answers
- How do I declare a two-dimensional array in C++ using new?
- How do I declare a namespace in JavaScript?
- How do I declare and initialize an array in Java?
- How do I declare global variables on Android using Kotlin?
- How do I declare a global variable in Python class?
- How do I declare global variables on Android?
- how can I declare an Object Array in Java?
- How to store a 2d Array in another 2d Array in java?
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- How do I change focus to a new popup tab in Selenium?
- How do we declare variable in Python?
- How can I find the index of a 2d array of objects in JavaScript?
- How do I sort a two-dimensional array in C#
- How do I empty an array in JavaScript?
- Declare a const array in C#
Advertisements