-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathmd-array.cpp
38 lines (31 loc) · 947 Bytes
/
md-array.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
#include <iostream>
using namespace std;
// You must tell the function the bound of an array,
// otherwise, elements cannot be accessed
// if the array is a variable-length one, it may be difficult to know the bound
void init_2d_array(float mat[][4], //error, arrays of unknown bound
size_t rows, size_t cols)
{
for (int r = 0; r < rows; r++)
for(int c = 0; c < cols; c++)
mat[r][c] = r * c;
}
int main()
{
int mat1[2][3] = {{11,12,13}, {14,15,16}};
int rows = 5;
int cols = 4;
//float mat2[rows][cols]; //uninitialized array
float mat2[rows][4]; //uninitialized array
//init_2d_array(mat2, rows, cols);
for (int r = 0; r < rows; r++)
for(int c = 0; c < cols; c++)
mat2[r][c] = r * c;
for (int r = 0; r < rows; r++)
{
for(int c = 0; c < cols; c++)
cout << mat2[r][c] << " ";
cout << endl;
}
return 0;
}