A Quick Guide for C++ #4

Arrays 


Array Fundamentals 


An array  is collection  of objects of same data-type under the same name. The elements bare numbered  as 0,1,2....n-1.
These number are called as indices or subscripts.These numbers are used to locate the positions of the elements within the array.

The method of numbering the ith' element with index i-1 is called as zero based indexing.
That is, the element is always same as the number of steps from the initial element a[0] to that element.
For example the element a[3] is three steps from the element  a[0].

Types of arrays

There are three types of arrays.
i. One-dimensional array
ii. Two-dimensional array
iii. Multi-dimensional array


One-dimensional  Array 



It is an array in which each element is accessed by using only one subscript.The only one subscript represents the position of the element in the array.

Declaration of one-dimensional array

Syntax 
datatype array-name[size];

Example
int marks[50];

Initialization of one-dimensional arrays

You can give values to each array element when the array is first defined.

Example:
int a[5] = {9, -5, 6, 2, 8};
In the above example, value 9 is stored in a[0], value -5 is stored in a[1] value 6 is store in a[2], value 2 is stored in a[3] and value 8 is store in a[4].

Memory representation of one-dimensional arrays:

The elements of one-dimensional arrays are stored in contiguous  memory locations.

Example:
consider the declaration,
char a [5];

The element a[0] is allocated at a particular memory location, the element a[1] is allocated in the next memory location and so forth. Since the array is of the type
char each element  requires  1 byte.


Two-dimentional Array 



It is an array in which each element is accessed using 2-subscript. The subscripts represent the position of element in the array.

The elements of two-dimensional array are represented as rows and columns. To identify any element, we should know its row-number and column-number.


Declaration of two-dimensional array:

Syntax:
datatype array-name[row-size][column-size];

Example: 
int a[2][3];

Initialization of two-dimensional arrays

Example:
int a[2][3] = {1, 2, 3, 4, 5, 6};
a is a two dimensional array which contains 2 rows and 3 columns and these assignments would be
a[0][0] = 1      a[0][1] = 2      a[0][2] = 3
a[1][0] = 4      a[1][1] = 5      a[1][2] = 6

if the values are missing in an initialization they are automatically set to 0.

Example:
int b[2][3] = {
{1, 2},
{3}
};
will initialize the first two elements to the first row, the next element to the second row. The remaining elements are automatically set 0.
b[0][0] = 1 b[0][1] = 2 b[0][2] = 0
b[1][0] = 3 b[1][1] = 0 b[1][2] = 0


Multi-dimensional Array 

A multidimensional array is an array of n-dimensions. In other words array of arrays is called a multidimensional array. A one-dimensional array of one-dimensional 
arrays is called a two-dimensional array; a one-dimensional array of two-dimensional arrays is called a three-dimensional array and so on.

It's a combination of 1D arrays and 2D arrays so overall makes it a multidimensional arrays ......!!

Let's see an example :

int multiArray[2][3][4] = {
{1,2,-5,4}
{4,8,-5,-6}
{8,-5,-9,3}
{4,3,2,9}..............so on }

So in this example , the array can hold up-to 24 elements !!!
The mos common example is again the matrix !!!

TIP:

JUST REMEMBER :
x >> is your array variable name [it could be any name y,k,l..]
[1st size] >> is your row declaration !! [forms 1D array]
[2nd size] >> is your column declaration !![forms 2D array]
What ever you declare this after the 2nd size it's gonna repeat like this row,column,row,column till the nth row and column is declared and thus forms a "Multidimensional Array" !!
 









Comments

Popular Posts

Translate