Arrays:

Array is nothing but group of elements where all the elements are of same type.

The two broad categories of the array are

1) One dimensional array

2)Multi-dimensional array.

1)      One-dimensional Array -

Consider the following example

a =   1

0

-4

3

7

i)       In the above example, we have formed group of five integers and the ‘a’ acts as group name. To access individual elements of this group, we have to specify subscript along with the array name.

ii)      The subscript is also called as Index. The elements in the above array are accessed as a [o], a , a , a and a . Thus the subscript values will range from 0 to 4.

iii)     To access any element in the array only one subscript is sufficient and hence the array ‘a’ is called as one dimensional array.

iv)     Declaration of one dimensional array :-

The general syntax of declaring one dimensional array is as follows.

datatype     arrayname [size]

for e.g.        int a  ;

Using the above declaration we declare to the compiler that a is an array of 5 elements are of type int. In response to this declaration, the compiler will allocate a space of 10 successive bytes in memory. The subscript values will range from 0 to 4 and the array elements will be accessed as a [o], a , a , a , and a . In general if one dimensional array is of size ‘n’ then the subscript values will range from 0 to n –1.

v)      Initialization while declaration of one dimensional array -

the array elements can be initialized at the time of declaration. Consider the following examples.

int a  = { 10, 20, 30, 40, 50};

int b  = { 10, 20, 30};

int c  = { 0, 20, 30};

int d  ;

static int e ;

int f [ ]

int f [ ] = { 10, 20, 30 };

2)  Two dimensional Array:

Consider the following example :

 0 1 2 3 0 12 –7 4 8 1 23 31 19 15 2 14 2 9 28

1. In the above example, we have a group of 12 integers in the tabular form of 3 rows and 4 columns and the group name is b. in the above example if we want to access the element 19 then it will accessed as b [ 1 ] [ 2 ].

Since we specify two subscripts to access the particular element of the array it is called as two dimensional array.

II. Declaration of two dimensional array.

The general syntax of declaring two dimensional array is as follow :

Data type array name (row-size) (column-size).

For e.g. int b(3) (4).

1. Due to the above declaration, we declare to the compiler that ‘b’ is a two dimensional array of figures of 3 rows and 4 columns. In response to the declaration the compiler will allocate a space of 24 successive bytes in memory.
2. The array elements will be stored in the memory row-wise. The first sub-script is always a row sub-script and the second sub-script is always column sub-script.
3. In the above example the values of row sub-script will range from 0 to 2 and the values of the column sub-script will range from 0 to 3.

In general if the two dimensional array is of m rows and n columns then the values of row sub-script will vary from 0 to m-1 and the values of column sub-script will vary from 0 to n-1.

1. WAP that accepts one dimensional array of not more than 100 integers. The program should also accept one more element say
2. x. The pgm should find out and print how many times the element x is present in the given array.

→      # include

# include

void main ( )

{

int a ;

int i,n,x,count=0 ;

clrscr ( ) ;

cout << “How many element = ”

cin >> n ;

cout << “Enter all element = ” ;

for ( i=0; i

cin >> a[i] ;

cout << “Enter element to be searched = ” ;

cin >>x ;

for ( i=0 ; i

if ( a[i] = = x )

count + + ;

cout << x << “ is not present \n” ;

else

cout “ is present “<

getch ( ) ;

}

Example 2:

1. WAP that accepts one dimensional array of not more than 100 integers. The pgm should sort them in ascending order and print them.

→      void main ( )

{

int a [ 100 ]

int i, n, j, temp ;

clrscr ( ) ;

cout << “ How many elements = ” ;

cin >> n ;

cout << “Enter all element = ” ;

for ( i = 0 ; i < n ; i + + )

cin >> a [ i ] ;

for ( i = n – 1 ; i > 0 ; i – – )

for ( j = 0 ; j < i ; j – – )

if ( a [ j ] > a [ j + 1 ] )

temp = a [ j ] ;

a [ j ] = a [ j + 1 ] ;

a [ j + 1 ] = temp ;

}

cout << “ sorted away in ” ;

for ( i = 0 ; i < n ; i + + )

cout << a [ i ] << ‘   ’ ;

getch ( ) ;

}

Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

### Comments (0)

There are no comments posted here yet