You are on page 1of 25

Multi Dimensional Array

CIT103 Computer Programming


Array Structure
What is an Array?
As we have learnt before an array is a data structure, which
can store a fixed-size collection of elements of the same data
type. An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of
variables of the same type.
Types of Arrays
There are 3 types of arrays in C++ namely;

 One Dimensional Array

 Two Dimensional Array

 Multidimensional Array
One Dimensional Arrays
One dimensional arrays (1D arrays) is a list of the variable of
similar data types. It allows random access and all the
elements can be accessed with the help of their index. The
size of this array is fixed. Represented below:
Example of an 1D Array
First Element Last Element

Number 1 Number 2 Number 3 Number 4 …


Two Dimensional Arrays
The simplest form of the multidimensional array is the two-
dimensional array (2D arrays). A two-dimensional array is, in
essence, a list of one-dimensional arrays. A two-dimensional
array can be think as a table, which will have x number of rows
and y number of columns. Matrices are usually represent by 2D
arrays.
Two Dimensional Arrays
Creating Arrays
Declaring Two Dimensional Arrays
Syntax of Declaring a Two Dimensional Array
int[][] arrayname = new int[m][n];
Declaring Two Dimensional Arrays
The statement

int[][] arrayname = new int[m][n];

declares an array of type DataType corresponding to a table with of


m rows and n columns .
Declaring Two Dimensional Arrays
Example

int[][] myArray = new int[3][4];

declares a three-row, four-column procedure-level array named


myArray
Initializing Two Dimensional Arrays
Initializing of Two Dimensional Array

int[][] arrayName = { {initialValues}, … {initialValues} };


Initializing Two Dimensional Arrays
Initializing of Two Dimensional Array

int[][] myArray = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };


Initializing Two Dimensional Arrays
Initializing of Two Dimensional Array

int[][] myArray = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

This creates a two-dimensional array with 3 rows and 3 columns, where the
first row contains the values 1, 2, and 3, the second row contains the values
4, 5, and 6, and the third row contains the values 7, 8, and 9.
Activity
i. Write a statement that declares an Integer array named
intQuantities. The array should have four rows and two
columns.

ii. Write a statement that assigns the number 7 to the element


located in the third row, first column in the intQuantities array.
Traversing Arrays
How Arrays are stored
When an array is stored the variable and the location of that variable in the
array is stored; this is referred to as the index of the array. Indexes of
arrays always start from 0 and are referenced by calling the array name
and the index.

Syntax: arrayName[rowIndex][columnIndex];
Example of How Arrays are stored
For our previously declared array age is stored in this format

Number 1 Number 2 Number 3 Number 4 …


(0,0) (0,1) (0,2) (0,3) (0,n)

Number 1 Number 2 Number 3 Number 4 …


(1,0) (1,1) (1,2) (1,3) (1,n)

Number 1 Number 2 Number 3 Number 4 …


(2,0) (2,1) (2,2) (2,3) (2,n)
Traversing Arrays
Two Dimensional arrays are traversed through nested loops. For loops are
usually used for array traversal. In order to go through an array, a variable
is needed to reference the variable. For example in other to print out the
scores in the array declared should be written as follows.
Traversing Arrays: For Loop

for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray[i].length; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}

Group Activity
Activity 1
Create a 2 dimensional array called alpha. Alpha should have 7 rows
and 4 columns. Prompt the user for the nth letter of the alphabet save
the users input in the array with the 27th and 28th slot being blank.

a. Print the array printing rows then columns.

b. Print out the array printing columns then rows


Activity 1 - Output
Activity 2
Create a 2 dimensional array called multi. Multi should have 12
rows and 10 columns. Calculate and save the multiplication tables
(n*1 to n*12) for the numbers 1 through 10 in multi. Display the
multiplication table.

You might also like