Anda di halaman 1dari 5

Arrays

--------An array is nothing but a collection of variables of


similar data type stored under one name. Whenever
user want to work with more variables of same data
type it is better to Work with the array.
If we are working with individual variables the
following are the problems.
1) The editor will be filled with variables if they are
more in number.
2) It is tough to refer a variable immediately as and
when we required.
3) Taking input and printing output is tough. As we
have to used more control or format strings.
4) For all the variables the memory will be allotted
randomly.
If we use array the following are the advantages.
1) We can declare any number of variables with in
very less space.
2) It is easy to refer a variable immediately as and
when we require.

3) Taking input and printing output is easy by using


one control or format string and with the help of loop
we can refer all the variables.
4) For all the variables the memory will be allocated
sequentially (or) continuously.
We have three types of arrays in c language.
They are
1) Single dimensional array.
2) Double dimensional array
3) Multi dimensional array
Single dimensional array: An array with one subscript
Syntax for declaring the array is :
Data type array_ name[size];
Ex: int a[5];
For the above declaration with the array name 'a'
automatically 10 bytes of memory will be allocated.
Immediately it will be divided into 5 equal parts
of each two bytes.
To refer each part an identification number will
be provided which can be called as index. The index
will start with 0 and ends with 4.

a
Indexes: 0 1 2 3 4
Total size of the array "a" = 10 bytes
Note:- The number of bytes of memory that has to be
allotted for the array = the product of number of
variables and number of bytes for each variable for
that data type.
If an array a contains n elements i.e., a[n], then
the index will starts with 0 and ends with n-1. To
refer the 1st variable the index is 0 and to refer the
nth variable the index is n-1.
To refer a required variable in the array:
Syntax: array name[index];
To refer the first variable the index is 0 and to refer
the 5th the index is 4 etc
Example: a[0],a[1],a[2] and so on.
Two Dimensional Arrays
-------------------------------

An array with two dimensions or two subscripts


can be called as two-dimensional array.
Syntax to declare the two dimensional array:
Data type array name[row size][column size];
Here the first dimension or first subscript stands
for row size which specifies the number of rows in
the two dimensional array. The second dimension (or)
second subscript stands for number of columns in
each row in the two dimensional array.
The number elements in the two dimensional
array is nothing but the product of row size and
column size.
The total number of bytes of memory that has to
be allotted for the two dimensional array is the
product of number of elements and the number of
bytes that has to be allotted for each variable of that
data type.
To refer each row, an identification number will
be provided which can be called as row index. The
row index will start with 0 and ends with (rowsize-1).
To identify each column, an index will be provided
which can be called as column index. The column
index starts with 0 and ends with (columnsize-1).

The Syntax To refer an element in the two


dimensional array:
array name[row index] [column index];
Example: int a[2][3];
For the above declaration automatically 12 bytes
of memory will be allocated for 6 elements.
Immediately it will be divided into 2 rows and 3
columns.
To identify rows, row index will be provided which
starts with 0 and ends with 1. To identify columns,
column index will be provided which starts with 0
ends with 2.

Anda mungkin juga menyukai