I am looking to define two 2d matrices: f and f_transpose of the type: std::array <std::array <float, 3>, dim> and  std::array <std::array <float, dim>, 3>. The value of dim is 23.
I want elements f[0][0], f[1][1], f[2][2], f_transpose[0][0], f_transpose[1][1] and f_transpose[2][2] to be 1 and the rest of the elements 0.
These arrays are global variables and I tried the following:
static array <array <float, 3>, dim> f = {{{{1}}, {{0, 1}}, {{0, 0, 1}}}};
static array <array <float, dim>, 3> f_transpose = {{{{1, 0, 0}}, {{0, 1, 0}}, {{0, 0, 1}}}};
This does give me 1 in necessary places, but some of the values that should be 0 are 1 or -1. My understanding is that whatever is not defined will be considered 0, but clearly that's not what is happening. How do I get around this problem?
Edit:
I had to remove the github link present here earlier. But for the answer (and comment) to make sense, I have added the relevant function below:
void print_ffamily(){
    cout << "F Matrix" << endl;
    for (int i = 0; i < 3; i++){
            for (int j = 0; j < dim; j++){
                printf("%0.2f ", f[i][j]);
            }
            cout << endl;
    }
    cout << "F transposed Matrix" << endl;
    for (int i = 0; i < dim; i++){
            for (int j = 0; j < 3; j++){
                printf("%0.2f ", f_transpose[i][j]);
            }
            cout << endl;
    }
}
                A two-dimensional array in C++ is the simplest form of a multi-dimensional array. It can be visualized as an array of arrays. The image below depicts a two-dimensional array. 2D Array Representation. A two-dimensional array is also called a matrix.
Here is an example of how to initialize a 2D array: int a[2][3] = { {0, 2, 1} , /* row at index 0 */ {4, 3, 7} , /* row at index 1 */ }; In above example, we have a 2D array which can be seen as a 2×3 matrix. There are 2 rows and 3 columns.
std::array is a container that encapsulates fixed size arrays. This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member.
The declaration of std::array is as follows: std::array<datatype,array_size> array_name; This is a simple declaration of an std::array.
I’m pretty sure both are correct.
For both you started list-initialization which is an aggregate initialization in this case.
For f_transpose you started an initializer for each of the 3 sub-arrays, the missing elements of each array are value-initialized which means zero-initialized for float.
For f you specify initializers for 3 arrays, for their elements the above applies too. The remaining arrays are indirectly (via value-initializaion,
 < C++11) or directly (≥ C++11) aggregate-initialized which value-initializes and therefore zero-initializes all their elements because the initializer is empty. 
Are you verifying the contents correctly?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With