hello everyone!
i have a quiz about 2D Arrays.
and i don't know how the questions might be,
can you help me :)

thank you.

PS:please correct my grammar mistakes if you see any, i'm here to learn.

thank you

5 Years
Discussion Span
Last Post by Khalessi

well i think i do understand that 2D array is to create two spaces in memory one for row and the other for col..


No.. A 2D array will still allocate 1D block of memory.. It's just how you access this.. For example:

    rows = 13;
    cols = 10;
    int* values;
    values = new int[rows*cols];

    for(unsigned i=0; (i < rows); i++)
        for(unsigned j=0; (j < cols); j++)
            values[i*cols+j] = 0.0;

We are still using a 1D block of memory, however, accessing this 1D of memory as a 2D (array).. Does this make sense/


A 2d array is declared like this:

int values[10][20];

It is very similar concept to an Excel spreadsheet, which has columns and rows. In the above declaration the array has 10 rows and 20 columns. What phorce illustrated above is a 1d array, not a 2d array.


thank you so much guys!
it helps.

still waiting an example of 2d array question:(


Here is an example:

A 2D array is initialised and represented as the following:

(type) -> name - > [][]

So as follows:

int 2darray[100][100]

This initialises a value of 100 rows and 100 cols note that in memory it is still a 1D block of data, the compiler, therefore, does not allocate data for rows and cols.

The following can be seen as a 2D array:

int main(int argc, char *argv[]) {

    int vals[100][100]; // 100 rows, 100 cols

    for(unsigned i=0; (i < 100); i++)
        for(unsigned j=0; (j < 100); j++)
            vals[i][j] = 0;


This can also be repesented as a 1D block (example, given above) where the memory is allocated for a 1D block, however, how we access it and therefore read from it changes.

Does this make more sense? Or, is there a specific question on 2D arrays?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.