You are on page 1of 18

Array

An array allows you to store a group of items of the same


data type together in memory.

The contents of an array are stored in contiguous


memory.
It is special type of variable that can contain or hold one
or more values of the same data type with reference to
only one variable name.
The data items in an array are referred to as elements.
It can be individually referenced by adding an index to
a unique identifier.
IT 105 Computer Programming 2
Array Elements and Subscripts

An array can be distinguished through a pair of square


brackets and a specified number inside the square
brackets [ ].

The number inside the square brackets is called an


index or element.

Each element is assigned a unique number known as


subscripts.
Subscripts are used to identify specific elements in an
array.

IT 105 Computer Programming 2


Array Elements and Subscripts

A Normal Variable

An Array Variable

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

// this is an array variable of five elements

IT 105 Computer Programming 2


Array Elements and Subscripts

An Array Variable

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


x
// this is an array variable of five elements

Each element can be referred to as :

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

IT 105 Computer Programming 2


Declaration and Definition of Array

data type arrayname [index];

int num [5];

char name [10];


Note: In a fixed-length array, the size of the array is
constant and must have a value at compilation time.

IT 105 Computer Programming 2


Initialization

An array can be initialized when it is declared ,when


initializing the array, the value for their various indexed
variables are enclosed in braces and separated by commas.

int num[5] = { 10,20,30,40,50 };


0 1 2 3 4

10 20 30 40 50

char choice[5] = { ‘a’,’b’,’c’,’d’,’e’};

IT 105 Computer Programming 2


Initialization

num[0] = 10 ; choice[0] = ‘a’ ;


num[1] = 20 ; choice[1] = ‘b’ ;
num[2] = 30 ; choice[2] = ‘c’ ;
num[3] = 40 ; choice[3] = ‘d’ ;
num[4] = 50 ; choice[4] = ‘e’ ;

Initialization without size


int num[] = { 10,20,30,40,50 };
Partial initialization
int num[5] = { 10,20};
(The rest are filled with 0’s)
IT 105 Computer Programming 2
Sample C++ Statements using Array

single variable array

num = 20 ; num[0] = 20 ;
num[x] = 20 ;
num[x-1]= 20 ;
a = a + 1 ; a[1] = a[1] + 1 ;

c = a + b ; c = a[1] + b[2] ;

c[0]= a[1]+b[2] ;
IT 105 Computer Programming 2
Sample C ++ Statements using Array

single variable array

cin >> x; cin >> x[0];

cout << x; cout << x[1];

if (grade[3]
if (grade ›=75)
›=75)
{
{
cout<<“passed”;
cout<<“passed”;
}
}
IT 105 Computer Programming 2
Processing One-Dimensional Array

// reading values into an array variable


for ( i = 0 ; i<=4; i++)
{
cin<< num[i];

IT 105 Computer Programming 2


Processing One-Dimensional Array

// displaying stored values of an array variable


for ( i = 0 ; i<=4; i++)
{
cout<< num[i];

IT 105 Computer Programming 2


IT 105 Computer Programming 2
IT 105 Computer Programming 2
Two - Dimensional Array

Two-dimensional arrays are stored in a row-column matrix, where


the left index indicates the row and the right indicates the
column.

Col 0 Col 1 Col 2 Col 3


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

IT 105 Computer Programming 2


Declaring Two-Dimensional Array

A typical declaration of two-dimensional array is: 

data type arrayname[rows][columns];


 
 
Example:
int num [2][3] ;

num[0][0] num[0][1] num[0][2]


num[1][0] num[1][1] num[1][2]

IT 105 Computer Programming 2


Initializing Two-Dimensional Array
Two–dimensional array can be initialized as:

int num[2][3] = {{ 10, 20,30},{40,50,60}};

10 20 30

40 50 60

IT 105 Computer Programming 2


Processing Two- Dimensional Array

A two-dimensional array can be processed in three


ways:

1. Process a particular row of the array, called row


processing.

2. Process a particular column of the array, called


column processing.

3. Process the entire array.

IT 105 Computer Programming 2


Processing Two- Dimensional Array

IT 105 Computer Programming 2

You might also like