You are on page 1of 10

Nicholas, MIS 07-015

What is an array? How to declare an array? Describe the characteristics of an


array.
A series of objects all of which are the same size and type is called an array. It is simply a
grouping of like-type data. Each object in an array is called an array element. For
example, we could have an array of integers or an array of characters or an array of
anything that has a defined data type. Some practical examples where the concept of the
array can be used are:
list of employees in an organization
test scores of a class of students
list of temperatures recorded every hour in a day etc.
How to declare:
A typical array can be declared as following:
<data_type> <array_name> [elements]
where <data_type> is a valid type (such as int, float..), <array_name> is a valid
identifier and [elements] field, which is always enclosed in square brackets [ ] specifies
the length of the array in terms of the number of elements.
For example, an array containing 5 integer values of type int called mango could be
represented as:
0 1 2 3 4
mango
Therefore, the mango array, with five elements of type int, can be declared as:
int mango [5];

Characteristics of an array:
Random access: The elements of an array can be accessed randomly without having to
check every element one by one (sequential access). For example:
abc

here abc is an array having 5(n= 5) integer type values. so if we want to access the 3
rd

element: abc [3]= 40 ; for 4
th
element : abc [4]= 50.
Memory allocation contagious: Array elements are stored in
subsequent memory locations. That is, there are no gaps between elements. For
int 1 int 2 int 3 int 4 int 5
memory location 0 1 2 3 4
elements 10 20 30 40 50

Nicholas, MIS 07-015
example: int abc [5]; This declaration will cause the compiler to allocate space for 5
consecutive int variables in memory. So, the number of elements in an array must be
fixed at compile time.
Homogeneous data: An array holds elements that have the same data type. An array
cannot hold any data with multiple data types. For example, data like customers name
(char) and their phone numbers (int) cannot be stored in a single array. We have to
declare two different arrays to store those two types of data.
Array size is constant: Array size should be mentioned in the declaration. Array size
must be a constant expression and not a variable.

What is data structure?
We use computer for data processing. To do such processing, we require data and
program (software). Data can be organized in computer memory in many ways. The
mathematical and logical model of any such organization of data is called data structure.
Qualities of a good data structure:
The data structure should mirror the actual data.
There are many data structures to store a data (stack, queue, array etc.). One
should use the appropriate data structure by which data can be read, write and
retrieve easily.
The data structure should have a set of necessary operations that can be
performed on data items, such as: insertion, deletion etc.
Data structure should describe the rules of how the data are related to each
other.
Describe the classification of data structure.
Array: An array is a collection of homogeneous data elements described by a single
name. Each element of an array is referenced by a subscripted variable or value, called
subscript or index enclosed in parenthesis. For example, One-dimensional array is a set
of n finite numbers of homogenous data elements.
The elements of the array are stored respectively in successive memory locations. The
elements of an array A may be denoted in C as A [0], A [1], A [2],......,A [n1], where n
refers to the number of items in an array. If n is 10, then the array elements will be A [0],
A[1]......A[9].
Linked list: A linked list is a set of dynamically allocated sequential nodes, arranged in
such a way that the nodes are linked with each other. Each node in a Linked List has two
parts. First part of the node contains the data and second part contains the pointer

Nicholas, MIS 07-015
(address) of the next node. The pointer always points to the next. If the pointer is NULL,
then it is the last node in the list.

Figure 1: How does a stack work

Figure 2: memory representation of stack

Queue: Queue is a type of data structure where elements of the data are inserted into
one end of the list and extracted from the other. A physical analogy for a queue is a line
at a bank. When we go to the bank, customers go to the rear (end) of the line and
customers come off of the line (are serviced) from the front of the line. So, whoever
comes first will be served first. That is, queue works in FIFO context (first-in first-out).
The main property of a queue is that objects go on the rear and come off of the front of
the queue. Like a stack, a queue usually holds things of the same type. We usually draw
queues horizontally.


Nicholas, MIS 07-015
Stack: A Stack is a linear data structure in which a data item is inserted and deleted at
one record. It is a simple data structure that allows adding and removing elements in a
particular order. Every time an element is added, it goes on the top of the stack; the only
element that can be removed is the element that was at the top of the stack. A stack is
called a Last In First Out (LIFO) structure. Because the data item inserted last is the
data item deleted first from the stack.

Tree: A binary tree is composed of a parent nod (root of the tree), which stores data
and also links to up to two other child nodes (sub trees) where the first nod is placed to
the left and the other nod is placed to the right. Each child node can itself be a small
binary tree. For example:

Binary tree works on the rule that child nodes which are lesser than root node keep on
the left side and child nodes which are greater than root node keep on the right side.

Nicholas, MIS 07-015
Same rule is followed in child nodes as well that are itself sub-trees. Like in above figure,
nodes (2, 4, and 6) are on left side of root node (9) and nodes (12, 15, and 17) are on
right side of root node (9).
Graph: A graph is a collection of nodes called vertices, and the connections between
them, called edges. When the edges in a graph have a direction, the graph is called
a directed graph or digraph, and the edges are called directed edges or arcs. The
following diagram shows a graph with 5 vertices and 7 edges. The edges between A and
D and B and C are pairs that make a bidirectional connection, represented here by a
double-headed arrow.

What are the common operations on data structure?
The common operations are:
Insert: adding a new item to the list. For example, adding a new item 30 in a queue-
12 15 10 20
(Position 0) (position 1) (position 2) (position 3)
12 15 10 20 30
(0) (1) (2) (3) (4)
Delete: Removing any item from the list. For example, removing 12 from the stack-
12 15 10 20
(0) (1) (2) (3)
15 10 20
(0) (1) (2)
Traverse: Traversing an array simply means doing a loop that visits every element of
the array. For instance, for a one-dimensional array with 50 places, we will use for i <=
50.
Searching: Locating an item in a list. For example: a [5] is an integer type array-

Nicholas, MIS 07-015
12 15 10 20 30
(0) (1) (2) (3) (4)
Here n=5, to find the 2
nd
item in the array, a[2]= 20
Sorting: organizing items of a list in some order: ascending or descending. For example:
a [5] is an integer type array-
12 15 10 20 30
(0) (1) (2) (3) (4)
Re-organizing the array in ascending order we get,
10 12 15 20 30
(0) (1) (2) (3) (4)
Merging: Combining two sorted list into a single sorted list. For example:
Array- 1:
10 12 15
(0) (1) (2)
Array 2:
20 30
(0) (1)
New combined array:
10 12 15 20 30
(0) (1) (2) (3) (4)


Memory representation of single dimensional array:
The array which is used to represent and store data in a linear form is called as 'single
or one dimensional array.
Syntax: <data-type> <array_name> [size];
Example: int a[3] = {2, 3, 5};
Total Size (in Bytes):
total size = length of array * size of data type

Nicholas, MIS 07-015
In above example, a is an array of type integer which has storage size of 3 elements. The
total size would be 3 * 2 = 6 bytes.
Memory Representation:
Let us declare an array: int a[12]= {30,40,50,60,70,80,90,100,110,120,130,140}
So the compiler has now allocated a total of 12 subsequent memory position with
unique memory address for each elements of a[12] array. In memory the array would
look like:



To find the index/ location/memory address of the array, we use this formula:
Loc[k] = base + (k LB)* w
where, k= index/memory address of a specific element
base= starting index/memory address
w= element size
LB= Lower bound (lowest memory position of an array)
to find index of 7th element:
Loc(a[7])=124+ (7-0)* 2 = 124+ 14 = 138


Nicholas, MIS 07-015
Comparison between array and Linked list

Random access vs. sequential access: In arrays, the elements can be randomly
accessed using subscript variable. For example: a[0] , a[1] etc can be randomly
accessed. While in linked list we have to traverse through the list for accessing element,
which is time consuming. So in linked list, the access is sequential.
Contiguous memory vs. dispersed memory structure: In arrays, the elements are
stored in contiguous memory location. For example, if 1
st
element is stored at 2000 then
the second element will be stored at 2002. But it is not necessary to store next element
at the consecutive memory location in a linked list. Elements are stored at any available
location, but the pointer to that memory location is stored in previous node.
Easy vs. complicated Insertion/Deletion operation: As the elements in an array are
stored in consecutive memory locations. So while inserting new elements, we have to
create space for insertion. More time required for creating space and inserting element.
Similarly, we have to delete an element from given location and then shift all successive
elements up by 1 position. In linked list, we have to just change the pointer address
field. So insertion and deletion operations are quite easy to implement.
Homogeneous vs. Heterogeneous data type: Each element of an array must be of
same data type because an array cannot hold data of various types. For example, if we
declare an array as integer type, we cannot store float, character or any other type of
data in that array. But for linked list, heterogeneous data can be stored in its nodes. But
it is not possible to use ordinary pointers to connect them. So we have to use void
pointer because it is capable of storing pointer to any type as it a generic pointer type.
Static vs. dynamic memory allocation: Array uses static memory allocation while
linked list uses dynamic memory allocation. So for array, the memory size must be
declared when the programmer is writing the program. But for linked list, there is no
need to define an initial size as memory can be allocated at run-time.
What is array traverse? Write a program in C to traverse an array.
Traversing an array means iterate through each element of the array once and perform
some operations on each element. For example, if we declare an array of 10 integers, we
can travel through each element and multiply it with 10 and then show the result.







#include<stdio.h>
void main()
{
int a[100],i,n;
printf("Enter the Array size less than 100:");
scanf("%d",&n);
printf("Enter the elements in array: \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The Traverse of array is:\n");

for(i=0;i<n;i++)
printf("%5d",a[i]);
}


Nicholas, MIS 07-015
How to insert an item in an array
Insertion of an item in an array means, adding a new item to the existing list of the
array. For example consider an array a[10] having three elements in it initially and a[0]
= 1, a[1] = 2 and a[2] = 3 and we want to insert a number 45 at location 1 i.e. a[0] = 45,
so we have to move elements one step below so after insertion a[1] = 1 which was a[0]
initially, and a[2] = 2 and a[3] = 3. Array insertion does not mean increasing its size i.e
array will not be containing 11 elements.






















#include <stdio.h>

int main()
{

int array[100], position, c, n, value;
printf("Enter number of elements in array\n");

scanf("%d", &n);
printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);

printf("Enter the value to insert\n");
scanf("%d", &value);

for (c = n - 1; c >= position - 1; c--)
{
array[c+1] = array[c];
array[position-1] = value;
}

printf("Resultant array is\n");

for (c = 0; c <= n; c++)
printf("%d\n", array[c]);

return 0;
}

Nicholas, MIS 07-015
How to delete an item from an array
Deleting an item means removing the item from the array list and keeping the rest of
the items untouched. For example consider an array a[10] having three elements in it :
a[1] = 5, a[2] = 7 and a[3] = 10 and we want to delete the item located in a[2] which is 7.
So after the delete operation our new list will be a[1] = 5 and a[2] = 10 which was
initially a[3]. So the delete operation shifted the elements one step up.



#include <stdio.h>
#include <conio.h>
int main()
{
int array[100], position, c, n;

printf("Enter number of elements in array\n");
scanf("%d", &n);

printf("Enter %d elements\n", n);

for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);

printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);

if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];

printf("Resultant array is\n");

for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}

getch();
}