You are on page 1of 16

ICT S112: FUNDAMENTALS OF COMPUTING

ARRAYS
PREPARED BY MR. JOHN PATRICK S. PAULINO
OBJECTIVES

01 02 03
Understand the use Identify the types Develop simple
of arrays in C++ of arrays. programs using
programming. arrays.
WHAT IS AN ARRAY?

➢ A series of elements of the same type placed in contiguous


memory locations that can be individually referenced by
adding an index to a unique identifier.
➢ A collection of similar data type value in a single variable.
➢ Characterized by:
✓ Data Type
✓ Array Name
✓ Array Size
➢ The elements in the array can then be referenced using a
valid index.
APPLICATION OF ARRAYS

1. Used to implement mathematical vectors and matrices, as


well as other kinds of rectangular tables.

2. Used to implement other data structures, such as lists,


heaps, hash, tables, deques, queues, and stacks.

3. One or more large arrays are sometimes used to emulate in


program dynamic memory allocation, particularly memory
pool allocation.

4. Used to determine partial or complete control flow in


programs, as a compact alternative to multiple “if”
statements.
• Code optimization
ADVANTAGES • Easy to Traverse Data
Easy to Sort Data
OF ARRAYS •
• Random Access

Fixed Size
DISADVANTAGE
OF ARRAYS
TYPES OF ARRAYS

One-Dimensional Two-Dimensional Multi-Dimensional


Array Array Array
ONE-DIMENSIONAL ARRAY

➢ A one-dimensional array in C++ is also referred to as a list.


➢ A group of elements having the same data type and same
name
➢ A one-dimensional array declaration has the format :

data_type array_name [array_size];

➢ To reference an element, the syntax is

array_name [index];
ONE-DIMENSIONAL ARRAY
Example: int score[5];

Graphical Representation:

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

5 10 15 20 25

The individual value of array variable score[5];


score[0] = 5;
score[1] = 10;
score[2] = 15;
score[3] = 20;
score[4] = 25;
ONE-DIMENSIONAL ARRAY
➢ Below are some more examples of one-dimensional array
declarations.

int n1[5], n2[100], n3[200];


float real[5];
double doub[1000];

➢ Arrays of the same type can be declared on the same line


separated by commas.
➢ Declarations can also be a mixed of ordinary variables and
arrays of the same type.
Valid Array Elements Invalid Array Elements
n1[0] n2[-1]
real[4] real[2.5]
doub[500] doub[1000]
ONE-DIMENSIONAL ARRAY

Initializing One-Dimensional Array in C++

data_type array_name[array_size] = {comma_separated_element_list};

Example:

int values[10] = {1,2,3,4,5,6,7,8,9,10};

➢ The number of values between braces {} shall not be greater than


the number of elements in the array.
TWO-DIMENSIONAL ARRAY

➢ Also referred to as a table.


➢ Can be called as “arrays of arrays”
➢ A two-dimensional array has two indexes; The first index
refers to the row, while the second refers to column.
➢ The syntax in declaring two-dimensional arrays in C++ is:

data_type array_ame[row_size][column_size];

➢ To reference an element in a multidimensional array the


syntax is:

array_name[row_index][column_index];
TWO-DIMENSIONAL ARRAY

Example: int score[3][3];

Graphical Representation:
Column

0 1 2

0 10 15 20

Row 1 25 30 35

2 40 45 50
TWO-DIMENSIONAL ARRAY

The individual value of array variable score[3][3];


score[0][0] = 10;
score[0][1] = 15;
score[0][2] = 20;
score[1][0] = 25;
score[1][1] = 30;
score[1][2] = 35;
score[2][0] = 40;
score[2][1] = 45;
score[2][2] = 50;
TWO-DIMENSIONAL ARRAY

Initializing Two-Dimensional Array in C++

int score[3][3] = {
{10.15.20},
{25,30,35},
{40,45,50}
};

or

int score[3][3] = {10,15,20,25,30,35,40,45,50};


TWO-DIMENSIONAL ARRAY

➢ Examples of valid array access are:


cout << TwoDArray[0][2] << “\n”; // assumed initialized
TwoDArray[1][2] = 25;
TwoDArray[0][0] = TwoDArray[1][2] + 3;

➢ Examples of invalid array access are:


cout << TwoDArray[1,2] << “\n”;
TwoDArray[-1][1] = 1.25;
TwoDArray[5][0] = 3.1416;
QUESTION?

You might also like