Skip to main content

Types of Array and Operations on it . . . .

Article No: 4 

TYPES OF ARRAY

One-Dimensional Array

The simplest form of an array is one-dimensional array that may be defined as the finite as the set of homogeneous elements. If only one subscript/index is required to reference all elements in an array then the array will be termed as one-dimensional array or simply array.

Syntax: Data-type array-name [size];

Example:

int arr[10];
char name [20];

Array is a set of pair that is index and value. For each index there is value associated to it. Smallest index value is called lower bound and largest index value is called upper bound of an array.

Mapping

We can find out size of an array by subtracting lower bound from upper bound. That is,
Size = U – L + 1

Here, U is upperbound and L is lower bound.

Array elements can be accessed by a[0], a[1]. . . . .[n]. In C programming language array is starts with 0 subscript. In one-dimensional array if l = 1 then size of array will be upper bound. In one-dimensional array position of specific subscript element can be found by formula,
A= M + (i – 1) * W

M = starting address
i = the subscript of array
W = is the size of an element

Let us, take an example for it, in one-dimensional array a[20], find out the memory location of a[18], where array starts from memory location 1900 and size is 4.

         M = 1900, W = 4, i = 20
        
         A[20] = M + (i – 1) * W
         A[20] = 1900 + (18 – 1) * 4
         A[20] = 1968

Another example, an array A[-3…….8] is stored in memory whose starting address is 1024 word size of element is 2, find out the location for A[0] and A[5].

A[0]

A[0] = M + (i – L) * W
A[0] = 1024 + (0 – (-3)) * 2
A[0] = 1030
A[5]

A[5] = M + (i – L) * W
A[5] = 1024 + (5 – (-3)) * 2
A[5] = 1040


Two-Dimensional Array

Two-dimensional arrays (alternatively termed as matrices) are the collection of homogeneous elements where the elements are ordered in a number of rows and columns.

Syntax: data-type array-name [row][column];

Example:

 int a[3][3];
char c[4][4];



OPERATIONS ON ARRAYS

1.      Traversing

Input: An array A with elements
Output: According to PROCESS()
Data structures: Array A[L….U]

Steps:

i=L
while i<=U do
PROCESS A[i]
i=i+1
end while
stop


2.      Searching

Input: KEY is the element to be searched.
Output: Index of KEY in A or a message on failure.
Data structures: An array A[L….U]

Steps:

i=L, found = 0, location = 0
while (i<=U) and (found = 0) do
if COMPARE(A[i], KEY) = TRUE then
found = 1
location = i;
else
i = i + 1
endif
endwhile
if found = 0 then
Print “ Search is unsuccessful : KEY is not found”
else
Print “Search is successful : KEY is in the array at location”, location
endif
Stop

3. Merging

Input: Two arrays A1[L1 . . . U1], A2[L2 . . . U2].
Output: Resultant array A[L . . . U], where L = L1, and U = U1 + (U2 – L2 + 1) when A1 is appended after A2.

Steps:

i1 = L1, i2 = L2
L=L1, U = U1 + (U2 – L2 + 1), i=L1
while (i1 < U1) do
                     A[i] = A1[I1]
                     i = i + 1, i1 = i1 + 1
endwhile
while (i2 < U2) do
                     A[i] = A2[i2]
                     i = i + 1, i2 = i2 + 1
endwhile
stop





Resourced Used: (Data Structure, 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...