Professional Documents
Culture Documents
1
ARRAY :
INTRODUCTION
An array is a group of related data items that share a common name.
This data structure is useful in maintaining table and queue in several
applications. Some common examples are,
An array for storing the rainfall of 365 days of a year,
An array of scores of a set of team,
A bit matrix table for computer screen,
A symbol table, etc…
We can define an array name salary to present a set of salaries of a group of
employees. Writing a number called index number or subscript in brackets
after the array name indicates a particular value. For example,
Salary [10];
Represent the salary of the 10th employee. While the complete set of values is
referred to as an ARRAY, the individual values are called elements. Arrays
can be of any variable type.
The ability to use a single name to represent a collection of items and
to refer to an item by specifying the item number enables us develops concise
in efficient program. For example: A loop with the subscript as the control
variable can be used to read the entire array, perform the calculation and,
printout the results.
TYPES OF ARRAYS:
Array
Static Dynamic
Variabl
Fixed Limited Unlimited
e
ONE-DIMENSIONAL ARRAYS:
A list of items can be given one variable name using only one subscript and such a
variable is called single subscripted value or one-dimensional array.
Storage structure for one-dimensional array
The memory of computer is linear and one dimensional array is started in continuous
memory cells storage of an element a [I+1] will be adjacent to the storage for element a[I],for
I=1 to n.
2
The declaration new line (int a [100]) reserves 100 successive memory locations each
large enough to contain a single integer. The address of the first of this location is called the
base address of the array ‘a’ and is denoted by base (a).
If ‘s’ is the size of each individual element of the array then the reference to the
element [0] is to the element at location base (a) then reference to a [1] is to the element at
base (a) + s. a reference to a [2] is the element at base (a)+ 2* s. In general the reference to a
[I] is to the element at location base (a)+I*s.
Declaration of one dimensional arrays
Like any other variable, array must be declared before they are used. The general
form of array declaration is,
Type variable name [size];
The type specifies the type of element that will be contained in the array, such as int,
float or character and the size indicates the maximum number of element that can be stored
inside the array.
For example,
Float height [50];
Declares the height to be an array containing 50 real elements. And subscripts 0 to 49 are
valid.
Similarly,
Int group [10];
Declares the group as an array to contain a maximum of 10 int constant. Remember, any
reference to the array out side the declared limits would not necessarily cause an error.
Rather, in might result in unpredictable program results.
TWO-DIMENSIONAL ARRAYS:
Array is used to stored the list of value sometime there will situation where a value in
the form of table to be stored.
Storage structure for two-dimensional array
in a two dimensional array the data is arranged in rows and
column , however the machine memory is arranged as a row of memory all. In a two
dimensional array the data is started in cells row by row that is used first started the first row
of the array then the second row of the array and then the next the so on. such a storage
scheme is called row major order.
The alternative is to store in an array column by column, it is called column major
order.
Let supposed that a two dimensional array integer array is store in row major
sequence and let base (a) be the address of first element at the array a [r1][r2] are the range of
3
first and second dimensional array respectively. Base [a] is the address of a [0][0]. If ‘s’ is the
size of each element then the address of any element a[I1][i2] can be calculated as ,
Base (a)+(i1*r1+i2)*s
Where,
-a is array name,
-if row major then i1 is the row of an array and i2 is column of an array,
-if column major then i1 is column of an array and i2 is row of an array;
-s is the size of element.
the different compiler allows different level or dimensional array . C allows arrays of
three or more dimensions. The compiler determines the exact limit. The general form of a
multi-dimensional array is
1) Matrix
The array having the application in matrix representation. By various mechanisms
matrix representation is carried out.
a) Sparse matrix
b) Graph representation of matrix through array
2) Object as array:
Through this application in the c programming, class structure or any user define
function can be used in the form of array it is called object of array
5
3) Stack
This is the most useful application of array. It permits insertion and deletion of
element from only one end defined as stack. The end is called the top of the stack. We
can access element in opposite order from that in which we were added . this
phenomenon is referred as LIFO. It is very useful in the prefix, postfix and infix
expression.
4) Queue
This is the another important application of array. it permits deletion to be performed
at one end called front . of list and insertion at other end called rear. The information
in list processed in some order in which it was received that is first in first out (FIFO).
Or first come first serve (FCFS) basis. the type of list referred as queue.
• Storage Structure For Array :
The simple data structure that makes use of computed address to locates elements
is the one dimensional array called vector.
Normally a number of memory location sequentially allocate to the vector.
Assuming that each element require one word of memory an N element vector
will occupy N consecutive word in memory.
A vector size is fixed and there for require fixed number of memory locations.
In general , a vector A with a subscript lower bound of ‘One’ can be represented
pictorially as in figure.
• STACK :
• In the most general form of linear list, we are allow to delete an element from and
insert an element to any position in the list.
• An Important subclass of lists permits the insertion or deletion of an element to occur
only at one end. A linear list belonging to this subclass is called a stack.
• The insertion operation is referred to as “push” and the deletion operation as “pop”
• The most and least accessible element in a stack are known as TOP and BOTTOM of
stack respectively
• Since insertion and deletion operation are performed at one end of stack, the elements
can only be removed in the opposite order from that in which they are added to the
stack such a linear list frequently to as LIFO ( Last In First Out).
• The familiar example of Stack is a pile of trays in cafeteria.
• These are supported by some kind of spring action in such a manner that person
desiring a tray finds that only one is available to him or her at the surface of the tray
counter. The removal of the top tray causes the load on the spring to be lighter and
the next tray to appear at the surface of the counter. A tray which is placed on the pile
causes the entire pile to be pushed down and that tray to appear above the tray counter
such an arrangement of tray is shown in figure.
OPERATION ON STACK :
• The representation of stack by a vector is as given bellow :
• A Pointer TOP keep track of the top element in the stack
• Initially when the stack is empty TOP has a value of ‘zero’, and when stack contain
single element TOP has value ‘one’ and so on.
6
• Each time a new element is inserted in the stack the pointer is incremented by ‘one’
before the element is placed on the stack.
• The pointer is decremented by ‘One’ each time a deletion is made from stack.
QUEUE :
• The important subclass of list permits deletion to be performed at one end of a list
and insertion at the other.
• The information in such a list is processed in same order as it was received that is on
first in , first out (FIFO) or First come , First served (FCFS) basis. This type of list is
frequently referred to as queue.
• Figure shows the representation of queue illustrating how an insertion is made to the
right of the rightmost element in queue and how a deletion consists of deleting the left
most element in the queue.
• The familiar and traditional example of a queue is a checkout line at a supermarket
cash register. The first person in line is the first to be checked out.
• More relevant example of a queue can be found in a time sharing computer system
where many user share the system simultaneously. Since such a system typically has a
single central processing unit and one main memory these resources must be share by
allowing one user program to execute for a short time followed , followed by the
execution of another user’s program, etc., and processed the information in a queue
pattern.
• This queue may not operate on a strictly first in , first out basis but on some complex
priority scheme based on such factors as what compiler is being used, the resulting
queue is sometimes called a priority queue.
• An algorithm for inserting an element in a queue is as given bellow.
• In Case of Queue, we can not reuse the space on which deletion is performed until
Queue becomes empty as shown in figure.
• A more suitable method of representing a QUEUE which prevents and excessive use
of memory is to arrange the element Q[1], Q[2] …Q[n] in a circular fashion with Q[1]
following Q[n]
• DEQUE : A Deque ( Double ended queue) is a liner list in which insertion and
deletion are made to or from either end of the structure such a structure can be
represented by figure as given bellow.
• It is clear that a deque is more general than a stack or queue. There are two variation
of deque, namely the input-restricted deque and the output restricted deque. The input
restricted deque allows insertion at only one end, while an output restricted deque
permits deletion from only one end.
◌ It is one of the most useful application of queue, priority queue & link list.
◌ A simulation program attempts to model a real world situation. In order to learn
something about it. As it would be too expensive or dangerous to experiment with
the real system. Each object & action in real situation as it’s counter part in the
program.
◌ If program successfully mirrors real world situation, the results of the program
should mirror.
◌ The result of actions been simulated that it is possible to understand both occurs in
the rear world situation without actually observing it’s occurrence.
7
◌ Ex:
They are physical simulators such as flights simulators used to train and air pilot.