Anda di halaman 1dari 18

Topic 1:

Multidimensional Array

CSC138 Structured Programming


Definitions
ARRAY A collection of a fixed number of elements
(called components) of the same type.

1-DIMENSIONAL
ARRAY An array in which the elements are arranged
in a list form.

2-DIMENSIONAL
ARRAY
A collection of a fixed number of elements
(called components) arranged in rows
and columns.
n-DIMENSIONAL
ARRAY A collection of a fixed number of elements
arranged in n dimensions (n>=1).
CSC138 Structured Programming
2D Array declaration
SYNTAX dataType arrayName [intExp1] [intExp2];

Specify the Specify the


number of rows number of columns

Example: double sales [10][5];


sales [0] [1] [2] [3] [4]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9] CSC138 Structured Programming
Accessing Array Components
SYNTAX arrayName [indexExp1][indexExp2];

Specify the Specify the


row position Column position

Example: sales [5] [3] = 25.75;


sales [0] [1] [2] [3] [4]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9] CSC138 Structured Programming
2D Array Initialization During Declaration
int board [4] [3] = { {2, 3, 1}, board [0] [1] [2]
{15, 25, 13}, [0] 2 3 1
{20, 4, 7}, [1]
{11,18,14} }; [2]
[3]

To initialize a 2D 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.
CSC138 Structured Programming
2D Arrays of different data types

int a[30][10]; // declares an int array of 30 rows and 10 columns.

float  matrix[20][25]; //declares a float array of 20 rows and 25 columns.

double wages[12][12]; //declares a double array of 12 rows and 12


columns.

char ticTacToeBoard[3][3] = {{'x', 'x', 'o'}, {'o', 'o', 'x'}, {'x', 'o', ' '} };
//declare and initialize a character array of 3 rows and 3 columns

CSC138 Structured Programming


2D Arrays of different data types
char Name[6][10] = {"Mr. Bean", "Mr. Bush", "Nicole", "Kidman", "Arnold", "Jodie"};
//declare and initialize a character string array

CSC138 Structured Programming


2D Array Operations (1)
A 2D array can be processed in three ways:
1) Process the entire array
2) Process a particular row of the array row processing
3) Process a particular column of the array  column
processing

CSC138 Structured Programming


2D Array Operations (2)
Example:

const int NO_ROWS =4;


const int NO_COLUMNS =3;

int matrix [NO_ROWS][NO_COLUMNS];

int row, col, sum, largest, temp;


matrix [0] [1] [2]
[0]
[1]
[2]
[3]

CSC138 Structured Programming


2D Array Operations (3)

INITIALIZATION

Initializing
Initializing aa single
single row:
row:
for
for (col
(col == 0;
0; col
col << NO_COLUMNS;
NO_COLUMNS; col++)
col++)
matrix
matrix [3]
[3] [col]
[col] == 0;
0;

Initializing
Initializing the
the entire
entire array:
array:
for
for (row
(row == 0;0; row
row << NO_ROWS;
NO_ROWS; row++)
row++)
for
for (col
(col == 0;
0; col
col << NO_COLUMNS;
NO_COLUMNS; col++)
col++)
matrix
matrix [row]
[row] [col]
[col] == 0;
0;
CSC138 Structured Programming
2D Array Operations (4)

PRINT

Output
Output the
the components
components of
of the
the array:
array:
for
for (row
(row == 0;0; row
row << NO_ROWS;
NO_ROWS; row++) row++)
{{ for
for (col
(col == 0;
0; col
col << NO_COLUMNS;
NO_COLUMNS; col++) col++)
cout
cout <<<< matrix
matrix [row]
[row] [col]
[col] <<
<< ““ “;“;
cout
cout <<<< endl;
endl;
}}

CSC138 Structured Programming


2D Array Operations (5)

INPUT

Input
Input data
data into
into aa single
single row:
row:
for
for (col
(col == 0;
0; col
col << NO_COLUMNS;
NO_COLUMNS; col++)
col++)
cin
cin >>
>> matrix
matrix [4]
[4] [col]
[col] ;;
Input
Input data
data into
into each
each component
component of
of the
the array:
array:
for
for (row
(row == 0;0; row
row << NO_ROWS;
NO_ROWS; row++)
row++)
for
for (col
(col == 0;
0; col
col << NO_COLUMNS;
NO_COLUMNS; col++)
col++)
cin
cin >>
>> matrix
matrix [row]
[row] [col];
[col];
CSC138 Structured Programming
2D Array Operations (6)
SUM BY ROW
Find
Find the
the sum
sum of of aa single
single row:
row:
sum
sum == 0;0;
for
for (col
(col == 0;
0; col
col << NO_COLUMNS;
NO_COLUMNS; col++) col++)
sum
sum == sum sum ++ matrix[row][col];
matrix[row][col];
Find
Find the
the sum
sum of of each
each row
row separately:
separately:
for
for (row
(row == 0;0; row
row << NO_ROWS;
NO_ROWS; row++) row++)
{{ sum
sum == 0; 0;
for
for (col
(col == 0;
0; col
col << NO_COLUMNS;
NO_COLUMNS; col++)col++)
sum
sum == sum sum ++ matrix
matrix [row]
[row] [col];
[col];

cout<<“Sum
cout<<“Sum of
of row
row “<<
“<< row
row ++ 11 << << ““ == ““ <<
<< sum
sum <<
<< endl;
endl;
}} CSC138 Structured Programming
2D Array Operations (7)
SUM BY COLUMN

Find
Find the
the sum
sum of
of each
each individual
individual column:
column:

for
for (col
(col == 0;
0; col
col << NO_COLUMNS;
NO_COLUMNS; col++) col++)
{{
sum
sum == 0;0;
for
for (row
(row == 0;
0; row
row << NO_ROWS;
NO_ROWS; row++) row++)
sum
sum == sumsum ++ matrix
matrix [row]
[row] [col];
[col];

cout<<“Sum
cout<<“Sum ofof column
column “<<
“<< col
col ++ 11 <<
<< ““ == ““
<<
<< sum
sum <<
<< endl;
endl;
}}
CSC138 Structured Programming
2D Array Operations (8)
LARGEST ELEMENT IN A SINGLE ROW

Find
Find the
the largest
largest element
element in in row
row number
number 4: 4:
row
row == 4;
4;
largest
largest == matrix
matrix [row][0];
[row][0]; //// Assume
Assume thatthat thethe first
first element
element
//of
//of the
the row
row is
is the
the largest.
largest.

for
for (col
(col == 1; 1; col
col << NO_COLUMNS;
NO_COLUMNS; col++) col++)
ifif (largest
(largest << matrix
matrix [[ row]
row] [[ col])
col])
largest
largest == matrix
matrix [row]
[row] [col];
[col];

cout
cout <<“The
<<“The largest
largest element
element in
in row
row 4”
4” <<
<< largest;
largest;
CSC138 Structured Programming
2D Array Operations (9)
LARGEST ELEMENT IN EACH ROW AND EACH COLUMN
Find
Find the
the largest
largest element
element inin each
each row:
row:
for
for (row
(row == 0; 0; row
row << NO_ROWS;
NO_ROWS; row++) row++)
{{
largest
largest == matrix
matrix [row][0];
[row][0]; //// Assume
Assume that that the
the first
first element
element
//of
//of the
the row
row isis the
the largest.
largest.
for
for (col
(col == 1; 1; col
col << NO_COLUMNS;
NO_COLUMNS; col++) col++)
ifif (largest
(largest << matrix
matrix [[ row]
row] [[ col])
col])
largest
largest == matrix
matrix [row]
[row] [col];
[col];

cout
cout <<“The
<<“The largest
largest element
element in in row
row ”” <<<< row
row ++ 11
<<
<< ““ == ““ <<
<< largest
largest <<<< endl;
endl;
}} CSC138 Structured Programming
2D Array Operations (10)
LARGEST ELEMENT IN EACH ROW AND EACH COLUMN
Find
Find the
the largest
largest element
element in in each
each column:
column:
for
for (col
(col == 1; 1; col
col << NO_COLUMNS;
NO_COLUMNS; col++) col++)
{{
largest
largest == matrix
matrix [0][col];
[0][col]; //// Assume
Assume that that the
the first
first element
element
//of
//of the
the row
row is
is the
the largest.
largest.
for
for (row
(row == 1; 1; row
row << NO_ROWS;
NO_ROWS; row++) row++)
ifif (largest
(largest << matrix
matrix [[ row]
row] [[ col])
col])
largest
largest == matrix
matrix [row][row] [col];
[col];

cout
cout <<“The
<<“The largest
largest element
element in in column
column ”” <<
<< col
col ++ 11
<<
<< ““ == ““ <<
<< largest
largest <<<< endl;
endl;
}} CSC138 Structured Programming
Lab Assignment 1: 2D Array Basic
Operations
1. Explain and differentiate types of
Scores [0] [1] [2] [3]
programming paradigms: structured,
OOP, logic, functional, etc.
2. Write a program that will declare and [0] 50.5 75.0 45.5 60.0
initialize the Scores array.
• The program will perform the following [1] 77.0 88.0 90.5 55.5
processes on the Scores array:
a) find the total sum and the average of [2] 45.0 67.5 70.5 80.0
all elements in the array.
b) find the largest element in the array.
c) find the smallest element in the array.
d) Display all the elements in the array,
the total sum and the average of all
elements, the largest element and the
smallest element.

CSC138 Structured Programming

Anda mungkin juga menyukai