Sunday, August 14, 2011

C Programming: Arrays

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

An array is a data structure of multiple elements with the same data type. Array elements are accessed using subscript. The valid range of subscript is 0 to size -1.

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 a one-dimensional array. In C, single-subscripted variable xi can be expressed as,  x[1], x[2], x[3], x[4],…….,x[n]. The subscript can begin with number 0. For example if you want to represent a set of five numbers, say(35,40,20,57,19,45) by an array variable number, then you may declare the variable number as follows

int number[6];

and the computer reserves five storage locations as shown below:


The values to the array elements can be assigned as follows:

number[0] = 35
number[1] = 40
number[2] = 20
number[3] = 57
number[4] = 19
number[5] = 45

Declaration of One-Dimensional Array

type variable-name[size];

The type specifies the type of element that will be contained in the array, such as int, char etc. and size indicates the maximum number of elements that can be stored inside the array for example.     float matrix[50];

The example of One Dimensional Array is given in Next Post

No comments:

Post a Comment