EnggPedia - The Engineering Encyclopedia

Wed03292017

Last update06:04:15 AM

Font Size

Profile

Menu Style

Cpanel
Back Computer Programming Languages Array & Types of Array - One Dimensional, Two Dimensional Array

Array & Types of Array - One Dimensional, Two Dimensional Array

Array:

An array is the combination of homogenous elements with consecutive index numbers and successive memory locations. The values of an array are called elements of that array. Microprocessor takes time to search values stored on dispersed location but due to array values become in sequence, so searching process of microprocessor become fast.

There are two types of arrays.

  1. One Dimensional Array
  2. Two Dimensional Array

One Dimensional Array:

This type of array represents and store data in linear form. It is also called single dimensional array. The general syntax to declare one dimensional array is

[ Size ] ;

In C/C++ language, array name is named constant which can not change during execution of a program. [ ] is subscript operator to mention the size of an array and size should be always a positive value. Some example about declaration of 1- D array and its memory representation is shown as follow.

int a[ 5] ;

Index No

0

1

2

3

4

Values

Memory Locations

100

102

104

106

108

char ch[ 5] ;

Index No

0

1

2

3

4

Values

Memory Locations

200

201

202

204

204

In C/ C++ language, index number of first element of array is zero and its successive memory location depends on coordination of operating system and compiler. The difference between memory locations of two array elements depends on the data type. For example, for int data type this difference will be 2. The size of array can be found by using the following formula.

Size of an array = Total Element * Size of Data type

int a[ 5] = {4, 7, 8, 9, 6 } ;

In this statement array of 5 integer values has declared and initialized. The memory representation will be looking as

Index No

0

1

2

3

4

Values

4

7

8

9

6

Memory Locations

100

102

104

106

108

char ch[ 5] = { a , i , o , u , e } ;

In this statement array of 5 character values has declared and initialized. The memory representation will be looking as

Index No

0

1

2

3

4

Values

a

i

o

u

e

Memory Locations

200

201

202

203

204

Two Dimensional Array (2 D Array) :

This type of array represents and store data in matrix form. The general syntax to declare two dimensional array is :

< Array name < [Rows ] [Columns ] ;

Where array name is a named constant which can not change during execution of program. [ ] is subscript operator to mention the rows and columns of two dimensional array. Each 2- D array is converted into 1- D array before representing in memory. Let a 2- D array of 4 integer values is declared such as

int a[ 2] [2 ] = {4, 5, 6, 8} ;

there re two methods to convert this 2- D array into 1- D array.

Row By Row ( or Row major order )

It is default method which is followed by the compiler.

According to this method 2- D array is traversed row by row and store in 1- D array. According to Row by row method, its memory representation is shown as

Index No

0,0

0,1

1,0

1,1

Values

4

5

6

8

Memory Locations

104

106

108

110

Column by Column (or Column major order )

According to this method 2- D array is traversed column by column and store in 1- D array. According to column by column method, its memory representation is shown as

Index No

0,0

1,0

0,1

1,1

Values

4

6

5

8

Memory Locations

104

106

108

110

Related Articles to this Article
While loop is a conditional loop which execute set of instructions repeatedly until specific condit
The most common advantages of C language are
In C/ C++ language, loop is an iterative constructs which execute set of instructions according to
Input functions or streams are used are to read different type of data (Such as numeric, string or
Read more similar Articles