Two dimensional array column major order - c

This question is given in my assignment, but I am very confused about the notation of array declaration. Any idea what that is supposed to express?
Given a two dimensional array A[2:3, 9:18] stored in column major order with base address 100 and size of each element is 4 bytes. Find the address of A[4,12].

When you're calculating addresses of 2D array elements, you need to know how they're organized. Column-major ordering tells you that the 2D array is arranged as a list of columns. So that means each successive element is the next one in a column, which wraps around to the next adjacent column.
If you know the dimensions of this array, the base address, and the element size, you can calculate the address of an element at a specific row and column. Here's the formula for calculating the address of an element in a row-major ordered array. If you can understand that, you could modify it to work for column-major arrays.
addr = base_addr + row * num_cols * elem_size + col * elem_size;

Related

Seg faulting with 4D arrays & initializing dynamic arrays

I ran into a big of a problem with a tetris program I'm writing currently in C.
I am trying to use a 4D multi-dimensional array e.g.
uint8_t shape[7][4][4][4]
but I keep getting seg faults when I try that, I've read around and it seems to be that I'm using up all the stack memory with this kind of array (all I'm doing is filling the array with 0s and 1s to depict a shape so I'm not inputting a ridiculously high number or something).
Here is a version of it (on pastebin because as you can imagine its very ugly and long).
If I make the array smaller it seems to work but I'm trying to avoid a way around it as theoretically each "shape" represents a rotation as well.
https://pastebin.com/57JVMN20
I've read that you should use dynamic arrays so they end up on the heap but then I run into the issue how someone would initialize a dynamic array in such a way as linked above. It seems like it would be a headache as I would have to go through loops and specifically handle each shape?
I would also be grateful for anybody to let me pick their brain on dynamic arrays how best to go about them and if it's even worth doing normal arrays at all.
Even though I have not understood why do you use 4D arrays to store shapes for a tetris game, and I agree with bolov's comment that such an array should not overflow the stack (7*4*4*4*1 = 448 bytes), so you should probably check other code you wrote.
Now, to your question on how to manage 4D (N-Dimensional)dynamically sized arrays. You can do this in two ways:
The first way consists in creating an array of (N-1)-Dimensional arrays. If N = 2 (a table) you end up with a "linearized" version of the table (a normal array) which dimension is equal to R * C where R is the number of rows and C the number of columns. Inductively speaking, you can do the very same thing for N-Dimensional arrays without too much effort. This method has some drawbacks though:
You need to know beforehand all the dimensions except one (the "latest") and all the dimensions are fixed. Back to the N = 2 example: if you use this method on a table of C columns and R rows, you can change the number of rows by allocating C * sizeof(<your_array_type>) more bytes at the end of the preallocated space, but not the number of columns (not without rebuilding the entire linearized array). Moreover, different rows must have the same number of columns C (you cannot have a 2D array that looks like a triangle when drawn on paper, just to get things clear).
You need to carefully manage the indicies: you cannot simply write my_array[row][column], instead you must access that array with my_array[row*C + column]. If N is not 2, then this formula gets... interesting
You can use N-1 arrays of pointers. That's my favourite solution because it does not have any of the drawbacks from the previous solution, although you need to manage pointers to pointers to pointers to .... to pointers to a type (but that's what you do when you access my_array[7][4][4][4].
Solution 1
Let's say you want to build an N-Dimensional array in C using the first solution.
You know the length of each dimension of the array up to the (N-1)-th (let's call them d_1, d_2, ..., d_(N-1)). We can build this inductively:
We know how to build a dynamic 1-dimensional array
Supposing we know how to build a (N-1)-dimensional array, we show that we can build a N-Dimensional array by putting each (N-1)-dimensional array we have available in a 1-Dimensional array, thus increasing the available dimensions by 1.
Let's also assume that the data type that the arrays must hold is called T.
Let's suppose we want to create an array with R (N-1)-dimensional arrays inside it. For that we need to know the size of each (N-1)-dimensional array, so we need to calculate it.
For N = 1 the size is just sizeof(T)
For N = 2 the size is d_1 * sizeof(T)
For N = 3 the size is d_2 * d_1 * sizeof(T)
You can easily inductively prove that the number of bytes required to store R (N-1)-dimensional arrays is R*(d_1 * d_2 * ... * d_(n-1) * sizeof(T)). And that's done.
Now, we need to access a random element inside this massive N-dimensional array. Let's say we want to access the item with indicies (i_1, i_2, ..., i_N). For this we are going to repeat the inductive reasoning:
For N = 1, the index of the i_1 element is just my_array[i_1]
For N = 2, the index of the (i_1, i_2) element can be calculated by thinking that each d_1 elements, a new array begins, so the element is my_array[i_1 * d_1 + i_2].
For N = 3, we can repeat the same process and end up having the element my_array[d_2 * ((i_1 * d_1) + i_2) + i_3]
And so on.
Solution 2
The second solution wastes a bit more memory, but it's more straightforward, both to understand and to implement.
Let's just stick to the N = 2 case so that we can think better. Imagine to have a table and to split it row by row and to place each row in its own memory slot. Now, a row is a 1-dimensional array, and to make a 2-dimensional array we only need to be able to have an ordered array with references to each row. Something like the following drawing shows (the last row is the R-th row):
+------+
| R1 -------> [1,2,3,4]
|------|
| R2 -------> [2,4,6,8]
|------|
| R3 -------> [3,6,9,12]
|------|
| .... |
|------|
| RR -------> [R, 2*R, 3*R, 4*R]
+------+
In order to do that, you need to first allocate the references array (R elements long) and then, iterate through this array and assign to each entry the pointer to a newly allocated memory area of size d_1.
We can easily extend this for N dimensions. Simply build a R dimensional array and, for each entry in this array, allocate a new 1-Dimensional array of size d_(N-1) and do the same for the newly created array until you get to the array with size d_1.
Notice how you can easily access each element by simply using the expression my_array[i_1][i_2][i_3]...[i_N].
For example, let's suppose N = 3 and T is uint8_t and that d_1, d_2 and d_3 are known (and not uninitialized) in the following code:
size_t d1 = 5, d2 = 7, d3 = 3;
int ***my_array;
my_array = malloc(d1 * sizeof(int**));
for(size_t x = 0; x<d1; x++){
my_array[x] = malloc(d2 * sizeof(int*));
for (size_t y = 0; y < d2; y++){
my_array[x][y] = malloc(d3 * sizeof(int));
}
}
//Accessing a random element
size_t x1 = 2, y1 = 6, z1 = 1;
my_array[x1][y1][z1] = 32;
I hope this helps. Please feel free to comment if you have questions.

How to receive output of function in variable number of arrays in MATLAB?

I want to receive the output of [I_1,I_2,...,I_n] = ind2sub(siz,IND) for an n-dimensional array with size of dimensions defined in siz. The linear index is a single value in IND. I know the number of elements in each dimension of the array. But the number of dimensions is variable (it is known but variable). I want to know how to receive the output of ind2sub(siz,IND) in variable number of arrays as it needs n number of arrays to receive the output.
In fact, the number of dimensions is number of attributes of data points in a data set. If data set is called x the number of dimensions is size(x,2).
To collect an arbitrary number of subscripted indices from ind2sub, you'll want to use a cell array instead of individual variables like I_1, I_2, etc., to store the output. You can capture the comma-separated list output as follows, assuming your N-dimensional data is in a variable x:
[indices{1:ndims(x)}] = ind2sub(size(x), IND);
And indices will now be a 1-by-ndims(x) cell array containing the subscripts for each dimension that correspond to the linear indices in IND.

Correct Subscript Ordering for Two Dimensional Arrays

I am talking about a zero-indexed matrix of integers denoted by a pointer to pointer, i.e.
int **mat;
Then what is the correct way to represent the mat[m][n] element? Is it
*(*(mat+m)+n)
or is it
*(*(mat+n)+m)
Also, visually speaking, between m and n, which one is the row index or which one is the column index? Or do terms like row and column make any sense here? I am sure I have some conceptual gap here, and some help will be great.
The expression
mat[m][n]
is parsed as
(mat[m])[n]
which is equivalent to
(*(mat + m))[n]
which is in turn equivalent to
*(*(mat + m) + n)
so your initial guess is correct.
As for which of these mean rows and which of these mean columns - in some sense, this is up to you to decide. You're the one creating the array and you can assign it any semantics that you'd like.
On the other hand, if you create a 2D array like this:
int mat[A][B];
then in memory this will be laid out as
[0, 0][0, 1][0, 2]...[0, B-1][1, 0][1, 1][1, 2]... ... [A-1][B-1]
Because of locality of reference, if you read across this in the order shown above (do all of mat[0], then all of mat[1], etc.) than it is to iterate in the reverse order (do mat[0][0], then mat[1][0], then mat[2][0], etc.). In that sense, it's common to treat 2D arrays as having the first component select a row and the second select a column, since that more naturally aligns with how the memory is laid out.

1 dimensional array for indexing a 3D matrix

I'm trying to write a linear index for 3D matrix. Is there a formula to determine what is the linear index of (i,j,k) th element in a matrix with (nx,ny,nz) dimensions?
Is there any difference whether I'm using C of FORTRAN or something else?
I searched for similar questions but nothing was founded .
Thanks for any guide.
Actually, Fortran is column major order. That means that when linearly indexing a multidimensional array the first index grows faster, i.e.
ind(i,j,k) = i + (j-1)*nx + (k-1)*ny*nx
where I assume indexing from 1. The function ind gives an index the element (i,j,k) would have when looking at the same as a one-dimensional array (e.g., in sequence association).
Most other languages, including C derivatives, use the row-major order, so that the last index grows the fastest. They also index from 0.
ind(i,j,k) = k + j*nz + i*ny*nz
There are also other differences — the multidimensional arrays are actually arrays of arrays (similar to pointers to pointers) in C.

For an X dimensional array where X is greater than 1,is only the first dimension optional,rest are mandatory?

I am trying to get my head around this but since I am new to C I just can't imagine how it will be at or beyond 3D arrays.I know we have to mention the size of static 1D arrays,but in 2D arrays,the first dimension is optional in a declaration as in:
int arr[][2]={{2,3},{4,8},{5,3}};
But what about X dimensional arrays?Are the following OK or is only the first dimension optional to be mentioned and rest necessary?
int arr[][][2]= blah blah;
int arr[][][][8]= blah blah;
I know that in the above case of the 2D array,the second dimension is necessary to perform pointer arithmetic using arr,but I can't imagine how things would or wouldn't work out for array whose dimension is greater than 2.
Only one can be left blank. In order to know how to get to a particular value, it needs to be able to compute the offset of everything but the first value.
For example:
char array[][3]
The system knows to go three bytes (assuming 1-byte chars) * the first part of the array index + 1 for each part in the second.
char array[][2][3]
The system knows to go 6 bytes (2*3) bytes * the first part of the array index + 3 * the second part + 1 * the last part.
If the 2 weren't there, you couldn't figure out how far to offset based on the value of the first index.

Resources