Array Data Structure
Array Data Structure
What is an array?
Arrays are a group of continuous similar type of variables referred by a common name.
In C++, array can be declared and used as follows:
0 1 2 3 4
int num[5]; num
0 1 2 3 4
num[2]=3; num 3
0 1 2 3 4
num[4]=2*num[2]; num 6
cout<<num[4]+num[2]; 9
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
char d;
1
HNDIT Data Structure and Algorithm SLIATE
Multi-Dimensional Array:
Multi-dimensional array can be considered as an array of arrays.
For example a two dimensional array may look like as follows:
table
0 1 2 3 4 5 6
0
0 1 2 3 4 5 6
1 This location can be referred to as table[1][5]
0 1 2 3 4 5 6
2
table 0 1 2 3 4 5 6
0
1 This location can be referred to as table[1][5]
2
Advantages of arrays:
Fast access if index is known
Easy to add an element in a particular location.
Disadvantages of arrays:
Fixed size
Same data type
Slow in searching an element
Slow in inserting/deleting an element