Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Chapter - 5

Multi-Dim Arrays

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page1


Multiple Dimensional Arrays
type variable[size1][size2]; // comment

Example:
// a typical matrix
int matrix[2][4];

Notice that C++ does not follow the notation used in other languages:
matrix[10,12] // Not C++
To access an element of the matrix we use the notation:
matrix[1][2] = 10;

More than two dimensions can be used:


four_dimensions[10][12][9][5];

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page2


Initializing Matrices
// a typical matrix
int matrix[2][4] =
    {
        {1, 2, 3, 4},
        {10, 20, 30, 40}
    };

This is shorthand for:

matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[0][3] = 4;

matrix[1][0] = 10;
matrix[1][1] = 20;
matrix[1][2] = 30;
matrix[1][3] = 40;
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page3
2-Dimensional Arrays
Sometimes you may want to use similar data that would
normally be stored in a table rather than a list (1-D
array).
You can envision this table as a collection of data stored
in rows and columns.
This data must be all the same data type.
For example, suppose we had a collection of daily
temperature data taken every three hours for a week.

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page4


Example-Temperatures
By hand we could create a table that might look like
0:00 3:00 6:00 9:00 12:00 15:00 18:00 21:00
Sunday 48.9 42.7 45.5 55.1 68.9 74.7 70.1 60.7
Monday 51.8 48.5 48.9 50.8 56.6 62.6 58.3 45.4
Tuesday 40.5 40.3 41.5 50.4 54.3 61.8 60.0 52.7
Wednesday 50.9 48.9 47.3 53.6 60.2 65.7 64.6 54.1
Thursday 48.7 45.2 44.8 52.8 60.4 68.4 60.2 49.2
Friday 45.4 44.9 48.9 57.9 59.3 61.5 50.7 45.3
Saturday 42.9 45.6 47.8 48.9 50.3 51.9 50.8 49.9

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page5


Example-Temperatures
To store this data in the computer we could create a
two dimensional array.
double dailytemp[7] [8] ;

Number of
Data type Array name Number columns
of rows

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page6


In General
Declaring a 2-dimensional array:
data_type array_name[numb_rows][numb_cols];

where the number of rows and columns must be an


integer, but can be a literal constant (48, 34, 1002) or
a symbolic constant that has been previously
declared.
const int no_rows = 37;
const int no_cols = 85;
long someArray[no_rows][no_cols];
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page7
Referring to Elements
Individual elements of a 2-D array may be referred as
array_name[row_number][column_number]
or
array_name[int_variable][int_variable]

Similar to 1-D arrays, the first row (or column) number


is 0, the last is max_row - 1.

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page8


Filling 2-Dimensional Arrays
At time of declaration:
int a[3][4] = { {45, 23, 46, 68},
{46, 29, 15, 32},
{57, 68, 79, 32} };

Individually, one at a time:


a[0][2] = 46;
a[1][3] = 32;

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page9


Filling 2-Dimensional Arrays
Using nested loops:
for (int j = 0; j < no_rows; j++)
{ for (int k = 0; k < no_cols; k++)
{ // some output statement
cin>> a[j][k]; //interactive
// infile >> a[j][k]; for file input
}
}
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page10
Output of a 2-D Array
One element at a time
cout<<a[2][1];
Using nested loops:
for (int j = 0; j < no_rows; j++)
{ for (int k = 0; k < no_cols; k++)
{ cout<< a[j][k]; //interactive
// outfile << a[j][k]; for file output
}
cout<<endl;
}
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page11
Other Multidimensional Arrays
The dimensions of arrays is not limited to one or two.
The array capacity is the product of the integer
constants used in the declaration.
double someArray[25][4][15][35][5];
//is a array of 5 dimensions with a capacity of //
25*4*15*35*5
In the function heading, the first capacity constant may
be left blank
void prt(double ArrayInfunction[ ][4][15][35][5]) //
function header

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page12


Other Multidimensional Arrays
To fill/print a multidimensional numeric array use loops
nested to the same level as dimensions.
To access individual elements, use array name and the
appropriate number of subscripts.

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page13


HOMEWORK – PART 1
1. Make a two dimensional array for a checkers board (8 x 8).  Declare this array manually (no for loops). The
output should look like this:

_W_W_W_W

W_W_W_W_

_W_W_W_W

________

________

B_B_B_B_

_B_B_B_B

B_B_B_B_

2.  Use nested for loops to store and display the following 2D array

12345

2 4 6 8 10

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page14


HOMEWORK – PART 2
3. Use a 2D array to store the multiplication of numbers 1-12.  Output
the array into a chart with lines between values. 
EXAMPLE:
_____________
|x| 1 | 2 | 3 |...
|1| 1 | 2 | 3 | ...
|2| 2 | 4 | 6| ..
|3| 3 | 6 | 9| ...
|...

4. Let the user enter data into a 2 by 3 array and neatly display data
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page15

You might also like