An array is a collection of similar data elements stored at contiguous memory locations. This data type is useful when a group of elements are to be represented by a common name. An array is used to store a group of data items that belong to the same data type.
An array is a group
of elements that share a common name, and is differentiated from one another by
their positions within the array. An array is also defined as a collection of homogeneous data.
Arrays are the
derived data type in C programming language which can store the primitive type
of data such as int, char, double, float, etc. The array is the simplest data
structure where each data element can be randomly accessed by using its index
number. Array index always start from
zero.
Array is beneficial if we have to store similar elements. For example, 70,78,80,95, 90 and 98 are marks in 6 subjects of a student. To store these 6 marks 6 variables would be required- mark1, mark2, mark3, mark4, mark5 and mark6. For such a representation, arrays are useful. This can be represented as mark[6]. We don’t need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.
Declaration and Accessing of Array:
Like other variable,
an array must be declared before they are used in the program. The general form of array declaration is:
Syntax:
data_type array_name[size];
The data_type
specifies the type of element that will be contained in the array, such as int,
float, or char and the size indicates the maximum number of elements that can
be stored inside the array.
Example: int mark[5];
Here,
int is the data_type,
mark is the array_name,
and 5 is the size.
This example
describes an array of 5 integer elements and the elements are numbered starting
with 0. Each element is accessed by the array name followed by its position in
square brackets. This position is known as subscript,
thus each array element is a subscripted variable. Therefore we would have:
mark[0], mark[1], mark[2], mark[3], mark[4].
If array is accessed
beyond its limit, C does not report it as an error. Having declared mark[5],
accessing mark[5] is not a syntax error. The program does not terminate abnormally,
though it is logical error as the last legal element is mark[5].
Initialization of Arrays:
An array can be initialized
along with its declaration.
Syntax:
data_type array_name [size] = {value1,
value2, value3…};
Example:
in mark[3]={50,60,90};
Declares the variable mark as an array
of size 3 and assigns 50 to the zeroth element, 60 to the first
element and 90 to the second element of mark.
Size parameter may be omitted. In such
cases, the compiler allocates enough space for all initialized elements.
int mark[]={50,60,90};
One Dimensional Array:
A list of data items can be given one
variable name using one subscript
and such a variable is called a single-subscripted variable or one dimensional
array.
Declaration of one dimensional array:
Syntax:
data_type
array_name[size];
Here, data_type defines the type of each
element of the array. The array_name specifies the array name by which the
array will be referenced and size defines the number of elements the array will
store.
For example, if we want to represent
three subjects marks of a student, say (50,60,90), by a array variable mark,
then we declare mark as follows:
int mark[3];
The first element has the index 0. Thus,
since there are three elements, the last one index is 2. Therefore we would
have, mark[0],mark[1],mark[2]
The values can be assigned as follows:
mark[0]=50; assign the value 50 to the 0th
element
mark[1]=60; assign the value 60 to the 1st
element
mark[2]=90; assign
the value 90 to the 2nd element
printf(“ Value
=%d”,mark[1]); Output the contents of the 1st element of mark.
We can also initialize
array( i.e. give value to each array element) when the array is declared. For
example,
int mark[3]={50,60,90};
Two Dimensional Arrays:
There are certain situations where a
table of values will have to be stored. C allows us to define such table using two dimensional arrays.
A two-dimensional array is a grid having
rows and columns in which each element is specified by two subscripts. Two dimensional arrays is known as matrix. Elements in two dimensional
arrays commonly referred to by x[i][j] where ‘i’ is the row number and ‘j’ is
the column number.
An array a[m][n] is an m by n table
having m rows and n columns containing m x n elements. The size (total number
of elements) of the array is obtained by calculating m x n.
The following table is a pictorial
representation of a two dimensional array.
50 |
1 |
45 |
2 |
65 |
3 |
It is a 3x2 table (array) i.e. with 3
rows and two columns. It has 6(3*2) elements. Each element can be referenced by
using a row number and the column number. Since two numbers are used to refer
to each element, this is called a two dimensional array. C uses row major
arrangement to access the elements of the array-first number represents the row
and the second number represents the column.
Declaration of two dimensional arrays:
Two dimensional arrays are declared as
follows:
data_type array_name [row_size][column_size];
Example:
int score[5][2];
Declares an array named score of 10
elements.
Initialization of two dimensional arrays:
Similar to the one-dimensional arrays,
two dimensional arrays can also be initialized when they are declared.
int table[2][3]={0,0,0,1,1,1};
Initializes the elements of the first
row to zero and the second row to one. Initialization is done row by row. For
better degree of readability, we canalso write the same initialization in the
matrix form as:
int table[2][3]={ {0,0,0}, {1,1,1} };
Multidimensional Arrays:
C permits arrays of more than two dimensions. In multidimensional
arrays data is stored in tabular format.
In C we can define
multidimensional arrays in simple words as an
array of arrays. The total number of elements that can be stored in a
multidimensional array can be calculated by multiplying the size of all dimensions.
For example,
The array int x[5][10][20] can store
total 5*10*20=1000 elements
Syntax:
data_type array_name[size1][size2][size3]…..[sizen];
For example, a three
dimensional array, x can be declared as:
int x[3][2][4];
The element of the array
shown above is represented by three
subscripts. The first subscript 3 specifies a plane number, the second
subscript 2 represents row number and third subscript 4 is column number.
We can initialize a three
dimensional array in a similar way like a two dimensional array.
Example:
int x[3][2][4]={ {
{0,1,2,3}, {4,5,6,7}}, {{8,9,10,11}, {12,13,14,15}},
{{16,17,18,19},{20,21,22,23}};
In similar ways, we can
create arrays with any number of dimensions. However, the complexity also
increases as the number of dimensions increases.
0 Comments
if you have any doubts plz let me know...