0% found this document useful (0 votes)
24 views2 pages

Array Data Structure

Arrays allow storing multiple values of the same type contiguously in memory. They can be one-dimensional or multi-dimensional. A one-dimensional array stores elements sequentially, accessed via an index. A two-dimensional array can be thought of as an array of arrays. Arrays provide fast access to elements if the index is known but have fixed size, require same data type, and are slow for searching, inserting or deleting elements.

Uploaded by

anjana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
24 views2 pages

Array Data Structure

Arrays allow storing multiple values of the same type contiguously in memory. They can be one-dimensional or multi-dimensional. A one-dimensional array stores elements sequentially, accessed via an index. A two-dimensional array can be thought of as an array of arrays. Arrays provide fast access to elements if the index is known but have fixed size, require same data type, and are slow for searching, inserting or deleting elements.

Uploaded by

anjana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

HNDIT Data Structure and Algorithm SLIATE

2. 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

A program to store and print elements in an array:


#include<iostream.h>
#include<conio.h>
void main()
{
int i;
char keys[6]={'Q','W','E','R','T','Y'};
cout<<"The first 6 keys of a keyboard are ";
for (i=0; i<=5; i++)
cout<<keys[i]<<" ";
}

A program to search and replace an element in an array:


#include<iostream.h>
#include<conio.h>
void main()
{
int i;
char s,r;
int found=0; //found=false
char keys[6]={'Q','W','E','R','T','Y'};
cout<<"Enter a character to be searched: "; cin>>s;
cout<<"Enter a character to replace: "; cin>>r;
i= (-1);
while (i<5 && found==0)
if (keys[++i]==s) found=1;
if (found==1)
keys[i]=r;
else
cout<<"No such element.";
}

A program to delete an element from an array:


Actually we can’t delete an element from an array.
If we want to delete an element, we have to move all the subsequent elements forward.
Or otherwise we can replace the element with invalid values (e.g.: 0 or NULL).

#include<iostream.h>
#include<conio.h>
void main()
{
int i;
char d;

1
HNDIT Data Structure and Algorithm SLIATE

int found=0; //found=false


char keys[6]={'Q','W','E','R','T','Y'};
cout<<"Enter a character to be deleted: "; cin>>d;
i= (-1);
while (i<5 && found==0)
if (keys[++i]==d) found=1;
if (found==1)
keys[i]=NULL;
else
cout<<"No such element."
}

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

Or simply we can imagine a two dimensional array as follows:

table 0 1 2 3 4 5 6
0
1 This location can be referred to as table[1][5]
2

A program to store and print elements in a two dimensional array:


#include<iostream.h>
#include<conio.h>
void main()
{
Int i,j;
clrscr();
int matrix[3][4]={{5,3,6,4},{2,5,3,7},{3,6,5,8}};
cout<<"The elements of the array are";
for (i=0; i<3; i++)
{
for (j=0; j<4; j++)
cout<<matrix[i][j]<<"\t";
cout<<"\n";
}
}

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

You might also like