# Arrays

Topics: Array, String theory, Dimension Pages: 5 (1177 words) Published: October 19, 2013
ARRAYS IN C, C++, JAVA

ARRAYS IN C PROGRAMMING
An array in C programming language can be defined as number of memory locations, each of which can store the same data type and which can be referenced through the same variable name.
An array is a collective name given to a group of similar quantities. Eg: 1. Percentage marks of 100 students
2. Age of 25 people

Thus, an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. Usually, the array of characters is called a 'string', whereas an array of integers or floats is simply called an array. All elements of any given array must be of the same type.

DECLARATION OF AN ARRAY:
Arrays must be declared before they can be used in the program. Standard array declaration is as
type variable_name[length of array];
Here type specifies the variable type of the element which is going to be stored in the array. In C Language, arrays starts at position 0. The elements of the array occupy adjacent location in memory.

INITIALIZING ARRAYS:
Initializing of array is very simple in C Programming. The initializing values are enclosed within curly braces in the declaration and placed following an equal sign after the array name. Eg: int array={1,2,3,4,5};//declare and initialize the array

SINGLE DIMENSIONAL ARRAY:
Single dimensional arrays are essentially list of information of one type. Eg: If we wish to declare an array s for a 10 character string, we would write char s;

TWO DIMENSIONAL ARRAY:
C allows multidimensional array. Two dimensional array is the simplest form of multidimensional array. To declare a two dimensional integer array 'd' of size 10,20 we would write int d; Two dimensional arrays are stored in a row-column matrix where the first index indicates the row and second index indicates the column. This means the rightmost index changes faster than the leftmost sequencing through the array.

MULTIDIMENSIONAL ARRAYS:
Arrays of three or more dimensions are often used because of the amount of memory required to hold them. For eg: A four dimensional character array with dimensions 10,6,9,4 would require 10x6x9x4 or 2160 bytes. If the array were integer, 4320 bytes would be allocated. The required storage increases exponentially with the number of dimensions.

ARRAYS IN C++
C++ provides a data structure, the array, which stores a fixed size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as n1, n2,....n99, we can declare one array variable such as numbers and use n, n,...n to represent individual variables.
All arrays consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

DECLARING ARRAYS:
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:
type array_name[array size];
This is called a single dimensional array. The array size must be an integer constant greater than zero and type can be any valid C++ data type. For eg: To declare a 10 element array called balance of type double, we will use

double balance;

INTIALIZING ARRAYS:
We can initialize C++ array elements either one by one or by using a single statement as follows:
double balance={100.0,2.0,3.4,17.0,50.5};
The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].

MULTIDIMENSIONAL ARRAYS:
C++ allows multidimensional arrays. General form of multidimensional array declaration:
type name[size][size]....[size N];
The simplest form of multidimensional array is the two dimensional array. A...