Chapter 8 Array.doc
Chapter 8 Array.doc
Arrays
An array is a group of similar type of data elements, stored in contiguous memory
locations. These data elements are addressed by common name, an array is declared by
writing data types, followed by name, followed by size (dimensions) in brackets.
Arrays are also called as subscripted variables. Here the dimension means the number
of subscripts required to represent the element in array. Remember the subscript of an array
must be always a positive integer number.
Types of Arrays
1) One dimensional
2) Two dimensional
1) One dimensional
The arrays that use only one dimension to arrange its elements are called as one
dimensional array. A one dimensional array can be used to represent a list of data items and it is
also known as a vector. This type of array uses single subscript to represent the element. E.g.
list, vector etc.
All the values of array are stored in memory variable either in a row or column. Every
element of the array can be retrieved by indicating its location number or index of the memory.
Array declaration
Before using an array it must be declared in the initial stage by a variable type and the
maximum memory required for the array.
[10] is the number 10 is the size of array which tells how many values of float
type will be in array.
[] the square bracket tells the compiler the array size limit.
The statement a[10] states that float variable “a” is an array of 10 floating values. Above
declaration creates memory spaces of floating type for storing the 10 values of “a” variable.
0 1 2 3 4 5 6 7 8 9
The memory of “a” consist of 10 spaces and are indexed by a number for different
memory location form 0 to 9 i.e. begins from a[0] to a[9]. In C array indices always begin with
zero.
The single element can be accessed by specifying the subscript number in the bracket
following the array name. The number represents the element position in array. As the above
statement states that the element indices starts form 0 to 9, where first array element is a[0],
second array element is a[1] and so on.
After declaration of array the data of the array variable can be entered in the memory by use
of following methods.
A) Arithmetic statement
The value of array variable can be supplied in the memory by use of the arithmetic
statement.
a[0] = 1.22;
In this case the memory if filled by using arithmetic statement. The above statements are
the arithmetic statement where value of “a” variable is stored under different memory location
indicated by the index number in the square bracket to represent memory location.
B) Scanf statement
The values of array can be entered by using scanf statement. In this case the values in
array can be entered by including the scanf statemet in for loop. Here the loop variable acts as
an index. E.g. i in the following case.
Ex. for(i=0;i<=4;i++)
Scanf(“%f”, &a[i]);
In the above statement ‘i’ value starts from 0 and ends at 4. It reads 5 values of ‘a’
variable. These 5 data values can be supplied are for ex. 3.14, 5.11, 2.43, 5.11, 6.23. they filled
in the memory as values of arrays.
C) By declaration
The arrays can be given values directly at the time of declaration as below.
int a[5] = { 3, 5, -3, 1, 4 };
In this case, the value of array is supplied at the point of declaration and the 5 values are
supplied as; a[0]= 3, a[1]=5, a[2]=-3, a[3]=1, a[4]=4
The value of array can be printed on screen by printf statement in the for loop statement.
Here the loop variable acts as an index. E.g. i in the following case.
a) for(i=0;i<=4;i++)
printf(“a[%d]=%d”, i, a[i]);
a[0]= 3,
a[1]=5,
a[2]=-3,
a[3]=1,
a[4]=4
b) for(i=0;i<=4;i++)
printf(“%d/t”,a[i]);
In this method the values of a[0] to a[4] will be printed in one row as:
3, 5, -3, 1, 4
2) Two dimensional
A two dimensional array can be used to represent a table of data items consisting of
rows and columns and is also known as a matrix. The matrix in mathematics is a two
dimensional array which uses two dimensions viz. row and column. The element of the two
dimensional array is indicated by the row and column number in which it is located. The element
of matrix is written as aij, where i stands for row and j stands for column.
int a [3][3];
int b [4][5];
int c [6][8];
float x [10][10];
float y [20][20];
The structures like determinants, matrix can be represented using these arrays.
Remember the capacity of two dimensional array to store the number of elements is calculated
by multiplying its dimensions. i.e. an array c [6][8] can store 6 × 8 = 48 elements.
a) Arithmetic statement
The element of array can be supplied by using arithmetic statement by supplying the row
number and column number.
a[0][0]= 8;
a[1][2]= -4;
b) Scanf statement
The value can be entered to a two dimensional array using scanf statement in nested for
statement, where the outer loop generates the row numbers and inner loop generates the
column number.
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
scanf(“%d”, &a[i][j]);
}
Where ‘a’ is two dimension array. The i is varied from 0 to 2 and j is varied from 0 to 2.
Hence a matrix of 3 × 3 order is read by this statement. The 9 elements are entered in the
format
3 1 8
4 7 2
6 3 1
c) By declaration
Printing of array
The two dimensional array can be printed using prinf( ) statement and nested for loop,
where the outer loop generates the row numbers and inner loop generates the column number.
for(i=0;i<=2;i++)
for(j=0j<=2;j++)
printf(“%d”, a[i][j]);
printf(“\n”);
}
A 3 × 3 matrix is printed using nested for statement. The i is varied from 0 to 2. When 3
element of first row are printed by j a new line control ‘\n’ is executed and next row is printed on
a new line.