/* Initialize matrix with values from 0 to N*N. */
void
init_matrix_seq (unsigned N, float * m)
{
unsigned i;
for (i = 0; i < N*N; ++i)
m[i] = (float) i;
}
I mean, it looks like one loop is being used to go through N*N elements in one dimension. Is this possible in C, without the need for another loop to cycle through columns?
EDIT:
Code which initialises the 2D arrays and calls this function is shown here:
A = (float *) malloc (N * N * sizeof (float));
B = (float *) malloc (N * N * sizeof (float));
init_matrix (N, A, 1.0);
init_matrix (N, B, 1.0);
The answer is yes, but you can’t use the double indexing and you could get issues if you want to index an element, instead of A[i][j] you have to call A[i*N+j].
If you want to do this using double indexes and having all adjacent in memory, then allocate the array this way:
Now you have all adjacent in memory, you can use the function that you’ve written.But you can also use double indexing:
But your method is correct.