An Array is a group of related data items that share a common name.Array is a collection of similar data type. for example array of int type data or float.important thing to note is array can only have data of same type only. we cannot have combination of different data types.
For example we can define an array name salary to represnt a set of salaries of group of employees.A particular value is indicated by writing a number called index number of subscript in brackets after the array name.
Example: Salary[10].
Represents the salary of the 10th employee.. while the complete set of values is referred to as an array, the individual values are called elements. Arrays can be any variable type.
important points
- Index of array starts with zero(0).
- index of last element in array is n-1, where n is the size of the array.
- array has static memory allocation.i.e memory size once allocated for an array cannot be changed.
Array declaration:
datatype array_name [array_size];Example:int a [10];float a[10];
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-sub scripted variable or a one- dimensional array. In mathematics, we often deal with variables that are single- sub scripted.
Intilization of arrays
we can intilize the elements of arrays in the same way as the ordinary variables when they are declared. The general form of intilization of arrays is:
int a [10]={2,3,4,5,7,8,9,10,1,12};.
will decalre variable a is an array of size 10 and will assign values in each element.
Character arrays must be initilized in a similar manner. Thus the statement is
char name[]={'L','A','K','S'};
Declares the name to be an array of four characters,intilized with the string "LAKS".
Intilization of arrays in c suffers two draw backs.
- Ther is no convienient way to intilize only selected elements.
- There is no shortcut method for intilizing a large number of array elements like the one available TUTORIALS.
Two-Dimensional Array
C allows us to define tables of items by using two-dimensional arrays. Two dimensional arrays can be declared as follows.
type array_name [row_size][column_size];
Note that unlike most of other languages,which use one pair of parenthesis with commas to seperate array sizes, C places each size in its own set of brackets.
Intilization of Two-Dimesnional Arrays
Like the one-dimensional arrays,two-dimensional arrays may be initialized by following their declaration with a list of intial values enclosed in braces. For example
static 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. The initialization is done row by row. The above statement can be equivalently written as
static int table[2][3]={{0,0,0},{1,1,1}};
by surrounding of elements each row by braces.
Multi-Dimensional Arrays
C allows arrays of three or more dimensions. The exact limit is determined by the compier. The general form of a multidimensional array is
type array_name[s1][s2][s3]--[sm];
where s1 is the size of the ith dimension.

help ful content for beginners
ReplyDelete