Wednesday, October 29, 2014

4.1.1 ARRAYS - INTRODUCTION

Why arrays?
 
The fundamental data types namely char, int, float are constrained by the fact that a variable of these types can store only one value at any given time. In order to handle a large volume of data in terms of reading, processing and printing we need a derived data type known as array.
 
Def:
An array is a fixed-size sequenced collection of elements of the same data type.
 
Examples where the concept of an array can be used:
·         List of temperatures recorded every hour in a day, or a month, or a year.
·         List of employees in an organization.
·         List of products and their cost sold by a store.
·         Test scores of a class of students.
 
For instance, we can use an array name salary to represent a set of salaries of a group of employees in an organization. We can refer to the individual salaries by writing a number called index or subscript in brackets after the array name.
e.g. salary[10]
      represents the salary of 10th employee
 
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 to develop concise and efficient programs.
 
We can use arrays to represent not only simple lists of values but also tables of data in two, three or more dimensions. We have the following types of arrays:
·         One-dimensional array
·         Two- dimensional array
·         Multi- dimensional array
 
Advantages and Disadvantages of Array
Advantages
1)      It is used to represent multiple data items of same type by using only single name.
2)      It can be used to implement other data structures like linked list, stacks, queues, trees, graphs, etc.
3)      2D arrays are used to represent matrices.
4)      The elements of arrays are stored in consecutive memory locations so searching an element is easy.
 
Disadvantages
1)      We must know in advance that how many elements are to be stored in array.
2)      Array is static structure. It means that array is of fixed size. The memory which is allocated to array can not be increased or decreased.
3)      Since array is of fixed size, if we allocate more memory than requirement then the memory space will be wasted. And if we allocate less memory than requirement, then it can create problem.
4)      As the elements are stored in consecutive memory locations so insertions and deletions are very difficult and time consuming.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.