Professional Documents
Culture Documents
Engineering Faculty
Fall 2021
ECOM 2003
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.
2
Lab#9: Multidimensional Arrays
❖ 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:
3
Lab#9: Multidimensional Arrays
The nested braces, which indicate the intended row, are optional. The following
initialization is equivalent to previous 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
❖ 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:
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:
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