Programming
Fundamentals
Lecture 3b
2 Dimensional Arrays
• If the data is provided in a list form, it can be stored using one-dimensional
arrays.
• However, sometimes data is provided in a table form.
• For example, suppose that you want to track the number of cars in a particular
color that are in stock at a local dealership.
• The dealership sells six types of cars in five different colors.
2 Dimensional Arrays
• A single variable specifies each array element but arrays can have higher
dimensions.
• A two-dimensional array, sometimes referred to as a table, consists of both
rows and columns of elements.
• The following array of numbers is called a two-dimensional array of integers:
8 16 9 52
3 15 27 6
14 25 2 10
• This array consists of three rows and four columns.
• To reserve storage for this array, both the number of rows and the number of
columns must be included in the array’s declaration.
2 D Arrays Declaration
• A collection of a fixed number of components arranged in rows and columns
(that is, in two dimensions), wherein all components are of the same type.
• The syntax for declaring a two-dimensional array is:
• Wherein intExp1 and intExp2 are constant expressions yielding positive integer
values.
• The two expressions, intExp1 and intExp2, specify the number of rows and the
number of columns, respectively, in the array.
Two-Dimensional array
Sales
The
statement:
double
sales[10][5];
2D Arrays Declaration
Elements
• Calling the array val, the following is the correct specification for this two-
dimensional array:
int val[3][4];
Similarly, the declarations
double volts[10][5];
char code[6][26];
• specify that the array volts consists of 10 rows and 5 columns of floating-point
numbers.
• The array code consists of 6 rows and 26 columns, with each element capable
of holding one character.
2D Arrays Accessing
Elements
• To locate each element in a two-dimensional array, its position in the array is
used.
• The term val[1][3] uniquely identifies the element in row 1, column 3.
2D Arrays Accessing Elements
• To access the components of a
two-dimensional array, you need
a pair of indices:
• One for the row position and one
for the column position.
The statement: sales[5][3] = 25.75;
•Array elements in two-
dimensional arrays require
two indexes:
•sales[d][m]
2D Arrays Initialization
During Declaration
• Like one-dimensional arrays, two-dimensional arrays can be initialized when
they are declared.
• The following example helps illustrate this concept. Consider the following
statement:
2D Arrays Initialization
During Declaration
• To initialize a two-dimensional array when it is declared:
1. The elements of each row are enclosed within curly braces and separated
by commas.
2. All rows are enclosed within curly braces.
3. For number arrays, if all components of a row are not specified, the
unspecified components are initialized to 0.
4. In this case, at least one of the values must be given to initialize all the
components of a row.
2D Arrays Initialization
During Declaration
• Although the commas in the initialization braces are always required, the inner
braces can be omitted.
2D Arrays Initialization During Declaration
2D Arrays Example 1
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int NUMROWS = 3;
const int NUMCOLS = 4;
int i, j;
int val[NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27,6,14,25,2,10};
cout << "\nDisplay of val array by explicit element"
<< endl << setw(4) << val[0][0] << setw(4) << val[0][1]
<< setw(4) << val[0][2] << setw(4) << val[0][3]
<< endl << setw(4) << val[1][0] << setw(4) << val[1][1] for (i = 0; i < NUMROWS; i++)
<< setw(4) << val[1][2] << setw(4) << val[1][3] {
<< endl << setw(4) << val[2][0] << setw(4) << val[2][1] cout << endl; // print a new line for each row
<< setw(4) << val[2][2] << setw(4) << val[2][3]; for (j = 0; j < NUMCOLS; j++)
cout << "\n\nDisplay of val array using a nested for loop";
cout << setw(4) << val[i][j];
Initialization
starts with this }
element cout << endl;
val[0][0]=8 val[0][1]=16 val[0][2]=9 val[0][3]=52 return 0;
val[1][0]=3 val[1][1]=15 val[1][2]=27 val[1][3]=6 }
val[2][0]=14 val[2][1]=25 val[2][2]=2 val[2][3]=10
2D Arrays Example 1
// displays sales chart using 2-d array
#include <iostream>
#include <iomanip> //for setprecision, etc.
using namespace std;
const int DISTRICTS = 4; //array dimensions
cout << “\n\n”;
const int MONTHS = 3;
cout << “ Month\n”;
int main()
cout << “ 1 2 3”;
{
for(d=0; d<DISTRICTS; d++)
int d, m;
{
double sales[DISTRICTS][MONTHS]; //two-dimensional
cout <<”\nDistrict “ << d+1;
array
for(m=0; m<MONTHS; m++) //display array values
//definition
cout << setiosflags(ios::fixed) //not exponential
cout << endl;
<< setiosflags(ios::showpoint) //always use point
for(d=0; d<DISTRICTS; d++) //get array values
<< setprecision(2) //digits to right
for(m=0; m<MONTHS; m++)
<< setw(10) //field width
{
<< sales[d][m]; //get number from array
cout << “Enter sales for district “ << d+1;
} //end for(d)
cout << “, month “ << m+1 << “: “;
cout << endl;
cin >> sales[d][m]; //put number in array
return 0;
}
} //end main
2 D Arrays Element
Operations
• As with one-dimensional array variables, two-dimensional array variables can
be used anywhere that scalar variables are valid.
• The last statement causes the values of the four elements in row 0 to be added
and the sum to be stored in the scalar variable sumRow0.
• Each array element is identified by its row and column position
Larger Dimensional Arrays
• Although arrays with more than two dimensions aren’t commonly used, C++
does allow declaring any number of dimensions by listing the maximum size of
all dimensions for the array.
• For example, the declaration int response[4][10][6]; declares a three-
dimensional array.
• The first element in the array is designated as response[0][0][0] and the last
element as response[3][9][5]
• Similarly, arrays of any dimension can be declared.
Practice
Run the code and determine the output
#include <iostream>
using namespace std;
int main()
{
int i, j, val[3][4] = {8,16,9,52,3,15,27,6,14,25,2,10};
for (i = 0; i < 3; ++i)
for (j = 0; j < 4; ++j)
cout << " " << val[i][j];
return 0;
}
Practice
a. Write a C++ program that adds the values of all elements in the val
array used in Previous Exercise and displays the total.
b. Modify the program written for part a to display the total of each
row separately.
Practice
Write a C++ program that adds equivalent elements of the two-
dimensional arrays named first and second. Both arrays should have
two rows and three columns. For example,
element [1][2] of the resulting array should be the sum of first[1][2]
and second[1][2].
The first and second arrays should be initialized as follows: