Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

array

The document provides an overview of arrays, including their definition, initialization, and operations such as insertion, deletion, and searching. It discusses both one-dimensional and two-dimensional arrays, emphasizing the importance of declaring types and dimensions. Additionally, it outlines algorithms for traversing, inserting, deleting, and searching elements within linear arrays.

Uploaded by

jecrcdon123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

array

The document provides an overview of arrays, including their definition, initialization, and operations such as insertion, deletion, and searching. It discusses both one-dimensional and two-dimensional arrays, emphasizing the importance of declaring types and dimensions. Additionally, it outlines algorithms for traversing, inserting, deleting, and searching elements within linear arrays.

Uploaded by

jecrcdon123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

(8)

Reference Notes
Some points to be revised again
4) An array is a collection of similar elements
(2) The first element in the array is numbered0, so the
last element is 1 less than the size of
the array.
(3) Before using an array its type and dimension must be declared.
4) Array element are always stored in continuous memory locations.
Size of the array remains constant.

More Than One Dimension


So far we have explored arrays with only one
dimension. t is alsopossible for arrays to
have two or more dimensions. The two dimensional arrays is also
calleda matsix.

Initializing A 2-Dimensional Array


We can initialize a two dimensional array ljke. ......

int stud[4J[2]
1234,56
#1212,33},
1342, 32},
{2145, 46}

Or we can also initialize like this......

int stud[4]2] = (1234,56, 1212, 33, 342, 32, 2145,46):


It is important to remember that while initializing an array it is necessary to mention the
Second (column) dimension, whereas the first dimension (row) is optional.
Thus the declarations,
int arr[2][3] = (1, 2, 3, ,4, 5, 6};
int arr[ ][3] = {1, 2, 3, 4, 5, 6};. are perfectly acceptable.
(10) Reference Notes
Traversing Linear Arrays
Let Abe a collection of data elements stored in the
memory of the computer. Suppose we
want to print the contentsS of each element of A or suppose we want to count the
number of
elements of A with a given property. 1his can be acComplished by traversing A, i.e., by
accessing and processing each element of A e'xactly one.

Algorithm (Traversing a Linear Array) TRAVESE (A, LB, UB)


Here A is a linear array with lower bound LB and upper bound UB. This
algorithm
traverses A applying an operation process to each element of A
STEP I: [Initialize counter] Set K:-LB.
STEP 2: Repeat Steps 3 and 4 while K<sUB.
STEP 3: [visit element] Apply process to A(K)
STEP 4 : [Increase counter] Set KK+
[End of Step 2loop]
STEP 5: " Exit.

Inserting and Deleting Element


Let Abe a collection of data elements i the memory of the computer. "Inserting" refers
to the operation of adding another element to the collection A, and deleting" refers to the
operation of removing one of the elements fronA.

Algorithm Inserting into a Linear Array) INSERT (A,N, K, ITEM)


Here a is alinearaey with Nelements and Kis apositive integer such that K<=N. This
algorithm inserts an element TEM into the Kth position in A.
STEP 1: Injtialize counter] Set J:=N.
STEP 2: Repeat Steps 3 and 4 while J>=K.
STEP 3: [Move Jth element downwards] Set A[J+1]:=AJ].
STEP 4: (Decrease counter] Set J := J-1.
[End of Step 2:loop]
STEP 5: [Insert element.] Set A[K]: ITEM.
STEP 6: (Reset N.] Set N:=N+1.
STEP 7: Exit.
Arrays (|1)

Algorithm (Deleting from a LinearArrav) DELETE (A, N, K, ITEM)


Here A is a linear array with N elements and K is a positive integer such that K<=N. This
algorithm deletes the Kth element from A.
STEP I: Set ITEM := A[KJ.
STEP 2 :
Repcat steps 3and 4 while J = K to N-1.
STEP 3: [Move Jth element upwards] Set A(J]:FAJ+1].
STEP 4: [Increase counter Set J := J+1.
[End of Step 2 loop]
STEP S: |Reset the number Nof elements in A]Set N N-1.
STEP 6: Exit.

Searching element from a Linear Array


Searching is the process of finding the locatiÑn of given element in the linear array. The
search is said to be successful if the given element is foundfe. the element
does eXISt n ne
array, otherwise search is said to be unsuccessful.
There are two ways to search any element :
(a) Linear Search (b) Binary.Search
Linear Search

The method which tràyerses array sequendally to locate item is called Linear search or
sequential search. In the best posSible case, the item may occur at frst position. In this case, the
search operation terminates in success withjuat one comparison. However the worst case occurs
when either the item is present at last position or missing from the array. Thus in worst case the
linear searçh is O(n) operaiions

Algorithm (Linear Scarch) LINEAR_SEARCH (A, N, ITEM, LOC)


Here A is the Linear array with Nelements and ITEM is a given item of the information.
This procedure finds the locations LOCof 1TEM in A or sets LOC=0 if the search is
unsuccessful.
STEP 1: [Initialize LOC] Set LOC: = 0.
STEP 2: [Initialize counter] Set I:=1
STEP 3: Repeat step 5 and 5 while |<=N
STEP 4 : IF AU]= ITEM, then [If item found]
Set LOC:=land Exit.

You might also like