0% found this document useful (0 votes)
108 views

2d Array and Double Pointers

This document explains how to create and use a 2D dynamic array (jagged array) in C++. It declares a double pointer to allocate memory for rows of the array. Each row is then allocated separately to allow rows of different lengths. Values are read into the array from a file and the array is displayed. Memory for the 2D array and auxiliary length array is dynamically allocated and later freed.

Uploaded by

Rooshan Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

2d Array and Double Pointers

This document explains how to create and use a 2D dynamic array (jagged array) in C++. It declares a double pointer to allocate memory for rows of the array. Each row is then allocated separately to allow rows of different lengths. Values are read into the array from a file and the array is displayed. Memory for the 2D array and auxiliary length array is dynamically allocated and later freed.

Uploaded by

Rooshan Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Explain double pointer , de reference

#include<iostream>
using namespace std;

void show_2d_dynamicArray(int**,int,int );
int main()
{

int r = 3, c = 4;
int** arr = new int*[r]; //declaring three pointers
for (int i = 0; i < r;i++)
{
arr[i] = new int[c]; //asssigning four memory
location on each row
} //end of

cout << "Enter values in 3 by 4 matrix: " << endl;


for (int i = 0; i < r;i++)
{
for (int j = 0; j < c;j++)
{
cin >> arr[i][j];
cout << " ";
}
cout << endl;
}

show_2d_dynamicArray(arr,r,c);
system("pause");
return 0;
}//end of main

void show_2d_dynamicArray(int** a, int r, int c)


{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout<<a[i][j]<<" ";
}
cout << endl;
}

Code 2D dynamic Jagged array


#include<iostream>
#include<fstream>
using namespace std;
int read(ifstream&);
int** dynamic_2D(int);
int* dynamic_1D(int);
void delete_2D(int**, int);
void show_2d_dynamicArray(int**,int,int* );
int main()
{
int** data = NULL;
int* length = NULL;
int row = 0;
int col = 0;
ifstream fin;
fin.open("myfile.txt");
row = read(fin);
cout << "insie row is: " << row << endl;

data = dynamic_2D(row); //the data which is double


pointer will create the space of pointers required
length = dynamic_1D(row); //this one d dynamic array
will store the number of eleements in each row
cout << data << endl;
cout << length << endl;

for (int i = 0; i < row;i++)


{
col = read(fin);
cout << "inside col is: " << col << endl;
data[i] = dynamic_1D(col); // in first
iteration it returns d[0], ID pointer at d[0]
//in second
iteration it returns d[1], ID pointer at d[1]
//in third
iteration it returns d[2], ID pointer at d[2]

for (int j = 0; j < col;j++) // iteration


one, j=0;j<4 as col is 4 , it read 4 elements from file
myfile.txt
{ // iteration
two, j=0;j<3 as col is 3 , it read 3 elements from file
myfile.txt
data[i][j] = read(fin); // iteration
one, j=0;j<2 as col is 2 , it read 2 elements from file
myfile.txt
}
length[i] = col; //length[0]=4, length[1]=3,
length[2]=2

show_2d_dynamicArray(data,row,length);
delete_2D(data,row);
delete[] length; //delete 1D dynamic array
system("pause");
return 0;
}//end of main

int read(ifstream& f)
{
int n;
f >> n;
return n;
}

int** dynamic_2D(int n)
{

int** rows = new int*[n];


return rows;
}

int* dynamic_1D(int n)
{

int* s = new int[n];


return s;
}
void show_2d_dynamicArray(int** a, int r, int* length)
{
int i = 0;
cout << "inside show_2d_dynamicArray" << endl;
for (; i < r; i++)
{
for (int j = 0; j < length[i]; j++)
{
cout<<a[i][j]<<" ";
}
cout << endl;
}

void delete_2D(int** data, int row)


{
for (int i = 0; i < row;i++)
{
delete[] data[i]; //0,1,2
}
delete[] data;
}

You might also like