ASDV-Cpp/Assignments/CppApplication_Pointers/twoDarrays.cpp

49 lines
1.2 KiB
C++

#include <iostream>
using namespace std;
#include "twoDArrays.h"
/** Create a 2D dynamic array and return it.
Use indexes in creation*/
int** create2DArray1(int rows, int columns) {
int** pp = new int*[rows];
for (int i = 0; i < rows; ++i) {
pp[i] = new int[columns];
}
return pp;
}
/** Create a 2D dynamic array and return it.
Use offset in creation*/
int** create2DArray2(int rows, int columns) {
int** pp = new int*[rows];
for (int i = 0; i < rows; ++i) {
(**pp + i) = new int[columns];
}
return pp;
}
/* Populate the 2D array with arbitrary integers using a loop*/
void populate (int** pp, int rows, int columns) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
pp[i][j] = (i + 1) * j;
}
}
}
/*Traverse the 2D array using indexes inside a loop*/
void traverse(int** pp, int rows, int columns) {
for(int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
cout << pp[i][j] << " ";
cout << endl;
}
}
/*Traverse the 2D array by de-referencing the pointers inside a loop*/
void traverse1( int** pp, int rows, int columns);
/** Free the memory the 2D array*/
void freeMemory( int** pp, int rows);
}