You are on page 1of 9

The Islamic University of Gaza

Engineering Faculty

Department of Computer Engineering

Fall 2021

ECOM 2003

Eng. Baraa Alastal

Computer Programming: C++


Lab#9: Multidimensional Arrays

Contents
❖ Introduction
❖ Declaring Multi-Dimensional Arrays
❖ Passing Two-Dimensional Arrays to Functions
❖ Multidimensional Arrays

Introduction
Data in a table or a matrix can be represented using a two-dimensional array.
You can use a two-dimensional array to store a matrix or a table. For example, the
following table that describes the distances between the cities can be stored using a
two-dimensional array.

Declaring Multi-Dimensional Arrays


The general form of a multidimensional array declaration

2
Lab#9: Multidimensional Arrays

For example, the following declaration creates a three dimensional 5 * 10 * 4


integer array

❖ Two-Dimensional Arrays
The simplest form of the multidimensional array is the two-dimensional array.
To declare a two-dimensional integer array of size x*y you would write something as
follows:

Where type can be any valid C++ data type and array Name will be a valid C++
identifier.
A two-dimensional array can be thought as a table, which will have x number of
rows and y number of columns. A 2-dimensional array a, which contains three rows
and four columns can be shown as below:

Thus, every element in array a is identified by an element name of the form a[ i ][ j ],


where a is the name of the array, and i and j are the subscripts that uniquely identify
each element in a.

3
Lab#9: Multidimensional Arrays

As an example, here is how to declare a two-dimensional array matrix of int values:


int matrix[5][5];

❖ Initializing Two-Dimensional Arrays


Multidimensional arrays may be initialized by specifying bracketed values for each
row. Following is an array with 3 rows and each row have 4 columns.

The nested braces, which indicate the intended row, are optional. The following
initialization is equivalent to previous example

Also, you can initialize a two-dimensional array as follows:


int a[3][4];
a[0][0] = 0; a[0][1] = 1; a[0][2] = 2; a[0][3] = 3;
a[1][0] = 4; a[1][1] = 5; a[1][2] = 6; a[1][3] = 7;
a[2][0] = 8; a[2][1] = 9; a[2][2] = 10; a[2][3] = 11;

❖ Accessing Two-Dimensional Array Elements


An element in 2-dimensional array is accessed by using the subscripts, i.e., row index
and column index of the array. For example:

4
Lab#9: Multidimensional Arrays

The above statement will take the element 11 from the 3rd row and fourth column
of the array.

❖ Ex1: Write a program that finds the sum of the numbers of the largest
row in 2- dimensional array and determines the index of this row.

5
Lab#9: Multidimensional Arrays

Passing Two-Dimensional Arrays to Functions


When passing a two-dimensional array to a function, C++ requires that the column
size be specified in the function parameter type declaration.

❖ Ex2: gives an example with a function that returns the sum of all the
elements in a matrix.

6
Lab#9: Multidimensional Arrays

Multidimensional Arrays
You used a two-dimensional array to represent a matrix or a table.
Occasionally, you will need to represent n-dimensional data structures. In C++, you
can create n-dimensional arrays for any integer n.
To declaring an n-dimensional array for n >= 3. For example, you may use a three-
dimensional array to store exam scores for a class of six students with five exams
and each exam has two parts (multiple-choice and essay).
The following syntax declares a three-dimensional array variable scores.

double scores[4][3][2] = {
{{7.5, 20.5}, {9.0, 22.5}, {15, 33.5}},
{{4.5, 21.5}, {9.0, 22.5}, {15, 34.5}},
{{6.5, 30.5}, {9.4, 10.5}, {11, 33.5}},
{{6.5, 23.5}, {9.4, 32.5}, {13, 34.5}}};

scores[0][1][0] refers to the multiple-choice score for the first student’s second
exam, which is 9.0. scores[0][1][1] refers to the essay score for the first student’s
second exam, which is 22.5.

7
Lab#9: Multidimensional Arrays

❖ Lab work:

1. Write a program to store temperature of two different cities for a week and
display it(the user will enter the temperatures in matrix).

2. Write a function that returns the sum of all the elements in a specified
column in a matrix using the following header:

const int SIZE = 4;


double sumColumn(const double m[][SIZE], int rowSize, int columnIndex);

Write a test program that reads a 3-by-4 matrix and displays the sum of
each column. Here is a sample run:

❖ Home work:

1. Write program adds two matrices, saves it in another matrix (two-


dimensional array) and displays it on the screen.

Note: In this program, user is asked to enter the number of rows r and
columns c. The value of r and c should be less than 100 in this
program. The user is asked to enter elements of two matrices.

8
Lab#9: Multidimensional Arrays

2. Write a function to display a matrix as shown below. The diagonal of the


matrix fills with 0. The lower side fills will -1s and the upper side fills with 1s.

You might also like