Skip to main content

Introduction to Array


Article No: 3


INTRODUCTION

In this chapter we will learn about array – a collection of similar data types. Elements of array contents are implemented by using consecutive memory locations. Arrays can be one-dimensional or multi-dimensional. In this chapter we will discuss about types of an array, operations on elements of an array.

In many situations, it may be possible that it is useful to collect the similar type of data items. C supports concept of array for this purpose. Let’s take an example of employee records:

1.      Declare 25 different variables to store the age of employees.
2.      Assign value to each variable.

It is also not easy to handle these variables in the program. The concept of array is useful in the situation. The type of data items may be char, int, or float. The elements of array share the same variable name. Specifying the array name followed by subscript in brackets indicates the elements of array.

WHAT IS AN ARRAY?

If we want to store a group of data together in one place then array is the one data structure we are looking for. This data structure enables us to arrange more than one element, that is why it is termed as composite data structure. In this data structure, all the elements are stored in contiguous (touched each other) locations of memory.



FIGURE 2.1 Array of data

An array is finite, ordered and collection of homogeneous (same) data elements. Array is finite because it contains only limited number of elements; and ordered, as all the elements are stored one by one in contiguous locations of computer memory in a linear ordered fashion. All the elements of an array are of same data type (say, integer) only and hence it is termed as collection of homogeneous elements. For example, an array of integers to store the age of all the students.

An array is known as linear data structure because, all elements of the array are stored in a linear data structure.

Declarations:

BASIC            : DIMENSION A[100]
FORTRAN     : DIM A[100]
Pascal           : A: ARRAY[1…100] of integer
C                   : int A[100]


TERMINOLOGY

SIZE

Number of elements in an array is called the size of the array. It is also alternatively termed as length or dimension.

Type

Type of an array represents the kind of data type it is meant for. For example, array of integers, array of character strings.

Base

Base of an array is the address of memory location where the first element in the array is located. For example, 453 is the base address of the array.

Index

All the elements in an array can be referenced by a subscript like Ai or a[i], this subscript is known as index. Index is always an integer value. As each array elements is identified by a subscript or index that is why an array element is also termed as subscripted or indexed variable.

Range of index

Indices of array elements may change from a lower bound (L) to an upper bound (U), which are called the boundaries of an array.

In a declaration of an array in FORTRAN (DIMENSION A[100]), range of index is 1 to 100. For the same array in C (int A[100]) the range of index is from 0 to 99. These are all default range of indices. However in Pascal, a user can define the range of index for any lower bound to upper bound, for example, for A: ARRAY[-5 …19] of integer, the points of the range is -5, -4, -3, …., 18, 19. Here, the index of i-th element is -5 + i – 1. In terms of L, the lower bound, this formula stands as:

                                                         Index (Ai) = L + i – 1

If the range of index varies from L . . . U then the size of the array can be calculated as

                                                         Size (A) = U – L + 1

Word

Word denotes the size of an element. In each memory location, computer can store an element of the word size w, say. The word size varies from machine to machine such as 1 to 8 bytes. Thus, if the size of an element is double the word size of a machine then to store such an element, it requires two consecutive locations.


Resource Used:

1. Data Structure through C (By: D Samanta)

Compiled By: Chaudhary Amit V.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Email/Password

In our  previous  article we saw how to create authentication module using Google identity provider. Now we will see how to implement Email and Password authentication using Firebase. We will use Bootstrap form to create intuitive user interface for sign-up and login using Email and Password. Step 1: Create signUp component. ng generate component signUp Step 2: Create custom form in sign-up.component.html file. The result of above code: Step 3: Add two functions for creating the user and login using Email and Password in src/app/providers/AFAuth.ts file. Step 4: Call createUserWithEmailAndPassword function in service from sign-up.component.ts. Step 5: Update routing configuration in app.module.ts  to include signUp component. Step 6: Update Login form to have Bootstrap form. The result of the above code. Step 7: Enable Email/Password component in Firebas...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...