-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoDMatrix.cpp
executable file
·131 lines (114 loc) · 2.82 KB
/
TwoDMatrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <vector>
using namespace std;
// Allocate Memory to a 2D array using NEW
// Riverbed phone interview
int** allocate ( int row, int col )
{
if (row < 0)
{
cout << "Invalid row" << endl;
return NULL;
}
if (col < 0)
{
cout << "Invalid col" << endl;
return NULL;
}
int** twoDarray = new(std::nothrow) int*[row];
if (twoDarray != NULL)
{
for (unsigned int i = 0; i < row; i++)
{
twoDarray[i] = new (std::nothrow) int[col];
if (twoDarray[i] == NULL)
{
for (unsigned int tmp = 0; tmp < i; tmp++)
{
delete twoDarray[tmp];
}
delete[] twoDarray;
return NULL;
}
}
}
else
{
return NULL;
}
return twoDarray;
}
template <size_t rows, size_t cols>
void printTwoDMatrix(int (&matrix)[rows][cols])
{
for (uint32_t i = 0; i < rows; i++)
{
for (uint32_t j = 0; j < cols; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
void printTwoDPtrToPtr(int** array, uint32_t rows, uint32_t cols)
{
for (uint32_t i = 0; i < rows; i++)
{
for (uint32_t j = 0; j < cols; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
void printTwoDVec(vector< vector<int> > twoDVec)
{
cout << "Two D Vector" << endl;
for (uint32_t i = 0; i < twoDVec.size(); i++)
{
for (uint32_t j = 0; j < twoDVec[i].size(); j++)
{
cout << twoDVec[i][j] << " ";
}
cout << endl;
}
}
int main()
{
// How to allocate a 2-D array in C++
int matrix[3][2] = {{1, 2}, {3, 4}, {5, 6}};
// VERY IMP: The values should be compile time.
// Does not work if the values are got from run time
int twoD[5][5] = {{1, 1, 0, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 1}};
uint32_t rows = 5;
uint32_t cols = 5;
int **arrayTwoD = new int *[rows];
for (uint32_t i = 0; i < rows; i++)
{
arrayTwoD[i] = new int[cols];
}
for (uint32_t i = 0; i < rows; i++)
{
for (uint32_t j = 0; j < cols; j++)
{
arrayTwoD[i][j] = twoD[i][j];
}
}
printTwoDPtrToPtr(arrayTwoD, rows, cols);
// VERY IMP: the below will fail.
// Can't cast 'int (*)[5]' to 'int**'
//printTwoDPtrToPtr((int**)twoD, rows, cols);
// Initialize 2D array to 0
{
rows = 3;
cols = 4;
vector< vector<int> > myTwoDVec(rows, vector<int> (cols, 5));
printTwoDVec(myTwoDVec);
}
cout << endl;
return 0;
}