0% found this document useful (0 votes)
53 views37 pages

CSC138 - CH2 (Part 1)

The document discusses two-dimensional arrays including how to declare, initialize, access elements, and perform common operations on 2D arrays such as input, print, sum rows and columns. It provides examples of declaring 2D arrays of different data types as well as initializing and accessing elements of a 2D array.

Uploaded by

Mohamad Faiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views37 pages

CSC138 - CH2 (Part 1)

The document discusses two-dimensional arrays including how to declare, initialize, access elements, and perform common operations on 2D arrays such as input, print, sum rows and columns. It provides examples of declaring 2D arrays of different data types as well as initializing and accessing elements of a 2D array.

Uploaded by

Mohamad Faiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

CSC 138 – Structured Programming

CHAPTER 2:
TWO DIMENSIONAL ARRAY
(2D-ARRAY)
LEARNING OBJECTIVES
Upon completion, you should be able to:
o be able to declare a two-dimensional array.
o be able to perform fundamental operations
on a two-dimensional array.
REVIEW DEFINITION OF EACH TYPE
OF ARRAY

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.
TWO DIMENSIONAL ARRAY
o The arrays we have used so far are referred to as single or
one-dimensional arrays because they use a single group of
items that can classified in a single column.
o In some cases, you may want to divided the list in two or
more sections. For example, here is a list of numbers divided
in various columns:

o Instead of having two lists that represent the same types of


values, you can declare a single array variable and divide its
list of items in two. A two-dimensional array is an array
made of two lists of items of the same data type.
TWO DIMENSIONAL ARRAY
o A collection of a fixed number of components
arranged in rows and columns, wherein all
components are of the same type.
o Is a block of memory locations associated with a
single memory variable name and designated by
row and column numbers
o Each element is written as
array (row #, column #)
o Used for tables of values
TWO DIMENSIONAL ARRAY
o Example 2D arrays:

Array (0,0) Array (0,1) Array (0,2) Array (0,3) Array (0,4)

Array (1,0) Array (1,1) Array (1,2) Array (1,3) Array (1,4)

Array (2,0) Array (2,1) Array (2,2) Array (2,3) Array (2,4)

Array (3,0) Array (3,1) Array (3,2) Array (3,3) Array (3,4)

[0] [1] [2] [3] [4]

[0] 3 5 10 4 5
[1] 2 7 9 8 6
[2] 1 0 3 4 7
[3] 2 4 5 7 9
DECLARATION 2D ARRAY
o Declaration syntax:
data_type arrayName[int Exp1][int Exp2];

Specify the Specify the


number of number of
rows columns

intExp1 and intExp2 specify the number of rows and the number of
columns, respectively, in the array.

o Example: int mark[4][5];


[0] [1] [2] [3] [4]
[0]
[1]
[2]
[3]
mark
DECLARATION 2D ARRAY
o 2D Arrays of different data:
• 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.

o Example how to declare 2D-array


dataType arrayName[rowSize][columnSize];
int matrix[5][5];
TWO-DIMENSIONAL ARRAY
ILLUSTRATION
[0] [1] [2] [3] [4] [0] [1] [2] [3] [4] [0] [1] [2] [3]

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

[1] [1] 4 5 6
[1]
[2] [2] 7 [2] 7 8 9

[3] [3] [3] 10 11 12

[4] [4] int array[][] = {


{1, 2, 3},
int matrix[5][5]; {4, 5, 6},
matrix[2][1] = 7; {7, 8, 9},
{10, 11, 12}
};
PAST SEM’S QUESTIONS
2D ARRAY INITIALIZATION
o 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.

int board [4] [3] = { {2, 3, 1}, board [0] [1] [2]
[0] 2 3 1
{15, 25, 13},
[1]
{20, 4, 7},
[2]
{11,18,14} };
[3]
2D ARRAY INITIALIZATION
o Example 2D array initialization:
int square[3][3] = { {1, 2, 3},
{4, 5 , 6}, {7, 8, 9} };

[0] [1] [2]


[0] 1 2 3
[1]
4 5 6
[2]
7 8 9
square
2D ARRAY INITIALIZATION
int square[3][3] = { {1, 2, 3},
{4, 5, 6}, {7, 8, 9} };
[0] [1] [2]
[0] 1 2 3
[1] 4 5 6
[2] 7 8 9
square

int square[3][3] = { {1, 2, 3},


{4, 5, 6}, {7, 8, 9} };
[0] 1 2 3
[1] 4 5 6
[2] 7 8 9
square
2D ARRAY INITIALIZATION
ACCESSING A 2D ARRAY
o Need a pair of indices: one for the row position and
one for the column position.
o The syntax:
arrayName[indexExp1][indexExp2]
o Example:
sales[5][3]=25.75;
• //Stores 25.75 into row number 5 and column number 3(6th row, 4th
column) of the array sales.

o int i=5; int j=3;


• //sales[5][3] is equivalent to sales[i][j]=25.75;
ACCESSING A 2D ARRAY
o To access an element of a 2D array, we
must specify the row and column index.
o Example:
square [1][2]

[0] [1] [2]


[0] 4 2 8
[1] 4 7 6
[2] 7 2 9
square
2D ARRAYS OPERATIONS
o A 2D array can be processed in 3 ways:
• Process the entire array. (Initializing and printing
the array)
• Process a particular row of the array, called row
processing. (Find the largest element and sum
of a row)
• Process a particular column of the array, called
column processing. (Find the largest element
and sum of a column)
2D ARRAYS OPERATIONS
Initialization

Input

Print

Sum

Average

Count

Max & Min

Searching

Sorting
1. INITIALIZATION
o Initializing a single row:
• To initialize row number 4 to 0.
row = 4;
for(col = 0; col < Number_of_Columns; col++)
matrix[row][col]=0;

o Initializing the entire array:


• To initialize the entire matrix to 0
for(row = 0; row < Number_of_Rows; row++)
{
for(col = 0; col < Number_of_Columns; col++)
matrix[row][col]=0;
}
2. INPUT
o Use nested loops
o Data are loaded row by row
o The outer loop represent rows and the inner loop
represent columns
Outer (rows)
for ( x = 0; x < 3; x++) Inner (cols)
for (y = 0; y<4; y++)

o After the array is loaded, it can be used in


calculations or can be displayed.
2. INPUT
o Input a single row:
• To input data into row number 4 (fifth row)
row = 4;
for(col = 0; col < Number_of_Columns; col++)
cin >> matrix[row][col];

o Input data into each component of the array:


for(row = 0; row < Number_of_Rows; row++)
{
for(col = 0; col < Number_of_Columns; col++)
cin >> matrix[row][col];
}
3. PRINT
o To print a two-dimensional array, you have to print each
element in the array using a loop like the following:

for (int row = 0; row < rowSize; row++)


{
for (int column=0; column<columnSize;
column++)
{
cout << matrix[row][column] << " ";
}
cout << endl;
}
3. PRINT
o Example
3. PRINT
o Output
4. SUM
o To sum entire values in 2D array
4. SUM
o To sum entire values in 2D array (output):
4. SUM
o To sum of the elements in the row:
//sum values of row
for ( int r=0; r<3;r++)
{
cout<<endl;
double sumRow=0.0;
for (int c=0; c<4;c++)
{
sumRow= sumRow+ array[r][c];
}
cout<<"The sum of row "<<r<<"is: "<< sumRow <<endl;

} sumRow

array
4. SUM
o To sum of the elements in the row:
4. SUM
o To sum of the elements in the row:

Reset sum = 0 for each row.


4. SUM
o To sum of the elements in the row (output):
4. SUM
o To sum of the elements in the column:
//sum values in column **to sum values in column,
for ( int c=0; c<4;c++) the outer looping must
{ starting with column size.
cout<<endl;
double sumC=0.0;
for (int r=0; r<3;r++)
{
sumC= sumC + array[r][c];
}
cout<<"The sum of column "<<c<<" is: "<<sumC <<endl;
}

array

sumCol
4. SUM
o To sum of the elements in the column:
4. SUM
o To sum of the elements in the column:

Reset sum = 0 for each column.


4. SUM
o To sum of the elements in the column (output):
5. AVERAGE
o Find average for entire values/row & column in 2D array:
5. AVERAGE
o Find average for entire values/row & column in 2D array:
CSC 138 – Structured Programming

CHAPTER 2:
TO BE CONTINUED….

You might also like