0% found this document useful (0 votes)
20 views9 pages

Wa0019.

Array

Uploaded by

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

Wa0019.

Array

Uploaded by

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

Arrays

• Collection of elements of same data type


• All these elements are stored in consecutive
memory locations
• Values of elements can repeat.
• The collection is represented by one name
• Each individual data in the array is referenced by a
subscript or index (positive integer constant or
expression) enclosed in a pair of square brackets []
Example:
• Array contains 12
elements
• The first element in every
array is the zeroth element
– first element of array c is
referred to as c[0]
– second element of array c is
referred to as c[1]
• In general, the ith element
of array c is referred to as
c[i – 1]
Defining Arrays
• Arrays occupy space in memory
• Programmer specify the type of each element and
the number of elements required by each array
• Syntax
<data type> <identifier> [<constant size>];
• Example
int numArray[20]; /*tell the computer to reserve 20 spaces.
Elements are from numArray[0] to numArray[19]*/
Array Initialisation
• Individual elements of the array can be initialized
• Initial values must be constants, never be variables or
function calls

• Example
int numArray[4]= {10,20,30,40};
/*4 is size. numArray[0]=10,… numArray[3]=40*/
or
int numArray[4];
numArray[0]=10;
numArray[1]=20;
Array Initialisation contd.
• The array definition
int n[5] = { 32, 27, 64, 18, 95, 14 };
causes a syntax error because there are six initializers and
only five array elements

• If the array size is omitted from a definition with an


initializer list, the number of elements in the array
will be the number of elements in the initializer list
• For example,
int n[] = { 1, 2, 3, 4, 5 };
would create a five-element array
Two-Dimensional Arrays
• The elements of a two-dimensional array are
arranged in rows and columns

• In general, an array with m rows and n columns is


called an m-by-n array
Two-Dimensional Arrays
• The array contains three rows and four columns, so
it’s said to be a 3-by-4 array
Accessing Two-Dimensional Array
Elements
• Array declaration
<data type> <identifier> [<row size>] [<column size>];
int matrix[2][3]; //array name is matrix with 2 rows and 3
columns

• An element in 2-dimensional array is accessed by using


two subscripts, i.e., row index and column index of the
array
• Example: int val = a[2][3];
// take 4th element from the 3rd row of the array
Initializing Two-Dimensional Arrays
• Multidimensional arrays may be initialized by
specifying bracketed values for each row

• An array with 3 rows and each row has 4 columns


int a[3][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} , {8, 9, 10, 11}};
is equivalent to
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

You might also like