Skip to main content
Fixed formatting
Source Link
SirGuy
  • 10.7k
  • 2
  • 38
  • 66

declaring 2D array dynamically:

    #include<iostream>
    using namespace std;
    int main()
    {
        int x = 3, y = 3;
    
        int **ptr = new int *[x];
    
        for(int i = 0; i<y; i++)
        {
            ptr[i] = new int[y];
        }
        srand(time(0));
    
        for(int j = 0; j<x; j++)
        {
            for(int k = 0; k<y; k++)
            {
                int a = rand()%10;
                ptr[j][k] = a;
                cout<<ptr[j][k]<<" ";
            }
            cout<<endl;
        }
     }

    

Now in the above code we took a double pointer and assigned it a dynamic memory and gave a value of the columns. Here the memory allocated is only for the columns, now for the rows we just need a for loop and assign the value for every row a dynamic memory. Now we can use the pointer just the way we use a 2D array. In the above example we then assigned random numbers to our 2D array(pointer).Its all about DMA of 2D array.

declaring 2D array dynamically:

#include<iostream>
using namespace std;
int main()
{
int x = 3, y = 3;

int **ptr = new int *[x];

for(int i = 0; i<y; i++)
{
    ptr[i] = new int[y];
}
srand(time(0));

for(int j = 0; j<x; j++)
{
    for(int k = 0; k<y; k++)
    {
        int a = rand()%10;
        ptr[j][k] = a;
        cout<<ptr[j][k]<<" ";
    }
    cout<<endl;
}
     }

Now in the above code we took a double pointer and assigned it a dynamic memory and gave a value of the columns. Here the memory allocated is only for the columns, now for the rows we just need a for loop and assign the value for every row a dynamic memory. Now we can use the pointer just the way we use a 2D array. In the above example we then assigned random numbers to our 2D array(pointer).Its all about DMA of 2D array.

declaring 2D array dynamically:

    #include<iostream>
    using namespace std;
    int main()
    {
        int x = 3, y = 3;
    
        int **ptr = new int *[x];
    
        for(int i = 0; i<y; i++)
        {
            ptr[i] = new int[y];
        }
        srand(time(0));
    
        for(int j = 0; j<x; j++)
        {
            for(int k = 0; k<y; k++)
            {
                int a = rand()%10;
                ptr[j][k] = a;
                cout<<ptr[j][k]<<" ";
            }
            cout<<endl;
        }
    }

    

Now in the above code we took a double pointer and assigned it a dynamic memory and gave a value of the columns. Here the memory allocated is only for the columns, now for the rows we just need a for loop and assign the value for every row a dynamic memory. Now we can use the pointer just the way we use a 2D array. In the above example we then assigned random numbers to our 2D array(pointer).Its all about DMA of 2D array.

Source Link

declaring 2D array dynamically:

#include<iostream>
using namespace std;
int main()
{
int x = 3, y = 3;

int **ptr = new int *[x];

for(int i = 0; i<y; i++)
{
    ptr[i] = new int[y];
}
srand(time(0));

for(int j = 0; j<x; j++)
{
    for(int k = 0; k<y; k++)
    {
        int a = rand()%10;
        ptr[j][k] = a;
        cout<<ptr[j][k]<<" ";
    }
    cout<<endl;
}
     }

Now in the above code we took a double pointer and assigned it a dynamic memory and gave a value of the columns. Here the memory allocated is only for the columns, now for the rows we just need a for loop and assign the value for every row a dynamic memory. Now we can use the pointer just the way we use a 2D array. In the above example we then assigned random numbers to our 2D array(pointer).Its all about DMA of 2D array.