Two Dimensional Array
Visual Representation of a two
dimensional Array
• It is an array that can be described as table having both row and
column
A two dimensional array [row]
[column]
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
Comparing one dimensional and two
dimensional array
• A one dimensional array can be created by indicating the number of
elements it contains
• Int array[5];
• BUT a two dimensional is created by indicating number of rows and
columns it has
• int array[2][3]
Comparing one dimensional and two
dimensional array
• A one dimensional array can be referenced or accessed by indicating
just the row index
• If there is an array such as array[4]={1, 5, 7, 9};
• To access 7 we indicate the index of 7
• array[rowindex]; e.g., array[2];
• A two dimensional array is referenced or accessed by indicating the
row and column index
• If there is an array such that array[2][2]={{2,3},{5,8}};
• To access 5 we indicate the row index which is 1 and the column index which
is 0 (using zero based indexing)
Using CPP to create a two
dimensional array
• To create includes:
• Declaration
• Initialization
• These can be done
• Statically
• Dynamically
Array Creation
• Note that an array has homogenous data type such as
• Int
• Float
• Double
• Char
• Basically we use an array when we want to store elements of the
same data type in a single variable such that one will not overwrite
the other
• E.g. to store scores of all the students in a class
Array Creation
• When it comes to two dimensional array, we can store
• Score of a number of students in different courses
• E.g., score of 8 students offering 5 courses
• This will be int score[8][5];
Output
Output
Dynamic Creation