0% found this document useful (0 votes)
14 views6 pages

18 Jan 2024 Lecture PF - 2D Array and Double Pointers - 2

The document discusses passing 2D arrays to functions in C++ including dynamically allocated 2D arrays. It covers initializing 2D arrays, reading from and writing to files, using double pointers, and deleting dynamically allocated 2D arrays.

Uploaded by

faizanmehmood579
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)
14 views6 pages

18 Jan 2024 Lecture PF - 2D Array and Double Pointers - 2

The document discusses passing 2D arrays to functions in C++ including dynamically allocated 2D arrays. It covers initializing 2D arrays, reading from and writing to files, using double pointers, and deleting dynamically allocated 2D arrays.

Uploaded by

faizanmehmood579
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/ 6

Topics:

1-Passing 2D array to function


2-Reading 2D array from file through function
3-Basic of double pointer
4-Passing 2D array (dynamically created ) to double pointer

---- 1-Passing 2D array to function -----------

#include <iostream>
#include <cstring>
#include<fstream>
using namespace std;

void show2DArrayElements(int[][3], int r, int c);


int main() {

//2D array initialization methods


// example
// Quiz 1 Quiz 2 Quiz 3
// student 1 8 6 6
// student 2 5 8 5
// student 3 4 2 9
// student 4 9 10 10

int a[4][3] = { 0 };
show2DArrayElements(a, 4, 3);
int b[4][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
show2DArrayElements(b, 4, 3);
int d[4][3] = { { 10, 20, 30 }, { 40, 50, 60 }, { 70,
80, 90 }, { 100, 110, 120 } };
show2DArrayElements(d, 4, 3);
system("pause");
return 0;
}

void show2DArrayElements(int arr[4][3], int r, int c)


{
cout << "Showding 2D Array elements" << endl;
for (int r = 0; r < 4;r++)
{

for (int c = 0;c < 3;c++)


{
cout << arr[r][c] << " ";
}
cout << endl;
}

2-Reading 2D array from file through function

#include <iostream>
#include <cstring>
#include<fstream>
using namespace std;

void show2DArrayElements(int[][3], int r, int c);


void read2DArray(int g[4][3],int,int,char*);
int main() {

//2D array initialization methods


// example
// Quiz 1 Quiz 2 Quiz 3
// student 1 8 6 6
// student 2 5 8 5
// student 3 4 2 9
// student 4 9 10 10

// Program reading 2d array from file and display its


//contents on console

int g[4][3]; //this array will receive the elements of


array from file input.txt
char fname[20] = "input.txt";
read2DArray(g, 4, 3, fname); //
show2DArrayElements(g, 4, 3);

system("pause");
return 0;
}

void read2DArray(int g[4][3], int r, int c, char* filename)


{
ifstream fin;
fin.open(filename);
if (!fin.is_open())
{
cout << "sorry file input.txt is not opening" <<
endl;
}
else
{
for (int r = 0; r < 4;r++)
{
for (int c = 0; c < 3;c++)
{
fin >> g[r][c];
}
//cout << endl;
}
}

}
void show2DArrayElements(int arr[4][3], int r, int c)
{
cout << "Showding 2D Array elements" << endl;

for (int r = 0; r < 4;r++)


{
for (int c = 0;c < 3;c++)
{
cout << arr[r][c] << " ";
}
cout << endl;
}

3-Basic of double pointer

#include <iostream>
#include <cstring>
#include<fstream>
using namespace std;

int main() {

cout << "----------------Practice of double


pointer-----" << endl;
int y = 90;
int* aptr = &y;
int* bptr = aptr;
cout << "address of aptr= " << aptr << endl;
cout << "adress of btpr=" << bptr << endl;
cout << "value inside btr is: " << *bptr << endl;
int **mydptr = NULL;
//mydptr = bptr; //assigning single pointer directly
to double pointer is an error
//but you can assign address of single pointer to double
pointer
mydptr = &bptr;
cout << "value inside mydptr is: " << *mydptr << endl;
//it will show the address of bptr
cout << "value inside mydptr is: " << **mydptr << endl;
//it will show value inside btpr
system("pause");
return 0;
}

4-Passing 2D array (dynamically created ) to double pointer


#include <iostream>
#include <cstring>
#include<fstream>
using namespace std;
void show2DArrayElements(int**, int r, int c);
void delete2DArray(int**,int r); //we only required to
delete each row than double pointer
int main() {

int r;
cout << "How many rows you want to create in 2D dynamic
array: ";
cin >> r;
int** dArray = new int* [r]; //5

cout << "How many columns you want o create in each row
of array: ";
int c;
cin >> c;
for (int i = 0; i < r; i++)
{
dArray[i] = new int[c]; // creating rows of size c
}

// fill the 2d array with values


for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> dArray[i][j];
}
}
show2DArrayElements(dArray, r, c);
delete2DArray(dArray,r);

//

//delete[] dArray;
system("pause");
return 0;
}

void show2DArrayElements(int** arr, int r, int c)


{
cout << "Showding 2D Array elements" << endl;

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


{

for (int j = 0; j < c; j++)


{
cout << arr[i][j] << " ";
}
cout << endl;
}

void delete2DArray(int** arr, int r)


{

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


{

delete[] arr[i];
}
delete arr;
}

You might also like