Presentation Transcript
Slide 1:Array
An array is a group of related item that share a common name.
Eg: Salary[10]
The array name salary to represent set of salaries of a group of employee. A particular value is indicated by writing a number called index number or subscript in bracket after the array name
Slide 2:One – dimensional Array:
A list of items can be given one variable name using only one subscript and such a variable is called a single-subscripted variable or one dimensional array.
Subscript should begin with number
x[10];
To represent set of 5 number 10,20,30,40,50 by an array variable number, then we may declare the variable number as follows
int number[5];
Eg:
number[0] = 10;
number[1] = 20;
number[2] = 30;
number[3] = 40;
number[4] = 50;
Slide 3:Declaration of Array:
General form,
type variable-name [size];
type – data type
size – max no of elements that can be store inside the array
Eg,
float height[5];
int age[5];
char name[5];
Eg,
“GOOD LUCK”
When the complier sees a character string, it terminates it with an additional null character, when declaring character arrays we must allow one extra element space for the null terminator.
Slide 4:Initialization of Array:
General form,
(i) static type arrayname [size] = {list of values};
Eg,
* static int number[3] = {1,2,3};
* static int number[5] = {1,2,3};
* static int a[ ] = {1,2,3…………n};
* static char name[ ] = {‘a’ ,’b’, ’c’………n};
Slide 5:Two – Dimensional Array:
It is possible for the array to have two or more dimension. The two dimension arrays is also called a matrix
General form,
type array-name [row-size] [column-size];
Eg,
a[2][3];
In this first index select the row & the second index select the column within that row,
column 0 column 1 column 2
[0][0] [0][1] [0][2]
Row 0 1 2 3
[1][0] [1][1] [1][2]
Row 1 4 5 6
Slide 6:Initialization of Two – dimensional array:
(i) Two dimensional arrays may be initialized by following their declaration with a list of values enclosed in braces.
Eg: static int table[2][3] = {1,2,3,4,5,6};
(ii) The initialization is done by row by row
Eg: static int table[2][3] = {(1,2,3),(4,5,6)};
(iii) We can initialize two dimensional array in the form of matrix
Eg: static int table[2][3] = {
{1,2,3},
{ 4,5,6}
};
Slide 7:Multi – dimensional Array:
C allows array of three or more dimensions. The exact limit is determined by the compiler.
General form,
type array –name [s1] [s2] [s3] ………..[sn];
Eg,
int exam [6] [4] [5];