0% found this document useful (0 votes)
9 views

L5 - CD 303 Array and Its Operations With Memory Representation

The document discusses linear arrays, including their definition as a list of similar data elements referenced by consecutive numbers. It provides examples of how linear arrays are represented in memory through base addresses and offsets. The document also summarizes algorithms for traversing linear arrays to access each element once, as well as inserting a new element into the array, which involves moving existing elements downward to make space.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

L5 - CD 303 Array and Its Operations With Memory Representation

The document discusses linear arrays, including their definition as a list of similar data elements referenced by consecutive numbers. It provides examples of how linear arrays are represented in memory through base addresses and offsets. The document also summarizes algorithms for traversing linear arrays to access each element once, as well as inserting a new element into the array, which involves moving existing elements downward to make space.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Topic- Array & Its operations with Memory Representation

Subject Name/Code- Data Structure/CD 303


CSE- Data Science

Concept of Linear Array:

 Linear array is also called One-dimensional array.


 A List of a finite number of similar data elements.
 Referenced respectively by a set of consecutive numbers.

by subscript notation: A1, A2, A3, ................... An


by the parenthesis notation: A(1), A(2), A(3), . . . , A(N)
by the bracket notation: A[1], A[2], A[3], . . . , A[N]

Where N in A[N] is called a subscript and A[N] is called a subscripted


variable.

Example:
An automobile company uses an array AUTO to record the number of
automobiles sold each year from 1932 through 1984.
1. AUTO[K] =number of automobiles sold in the year K
2. LB = 1932 UB = 1984
3. Length = UB – LB + 1 = 1984 -1930 + 1= 55
4. That is, AUTO contains 55 elements.

Representation of Linear Array in Memory:


1. Linear Array store elements in contiguous memory locations.
2. Find Location of an element.
LOC(LA[K]) = Base(LA) + w(K - LB)
Example:
Consider the array AUTO in previous example, where number of automobiles
sold each year from 1932 through 1984.
Base(AUTO) = 200, LB=1932, w = 4 words per memory cell
LOC(AUT0[1932]) =200, LOC(AUTO[1933])=204, …..
So, for the year K =1965,
Find the address:
LOC(AUTO[1965]) =Base(AUTO) + w(1965 - LB)
= 200+ 4(1965-1932) = 332

Traversing of Linear Array:


Traversing means accessing and processing each element of LA exactly once.
Algorithm1: (Traversing a Linear Array)
1. [Initialize counter.] Set K := LB
2. Repeat Steps 3 and 4 while K ≤ UB:
3. [Visit element.] Apply PROCESS to LA[K]
4. [Increase counter.] Set K := K + 1
[End of Step 2 loop.]
5. Exit

Example:
Find the number NUM of years during which more than 300 automobiles were
sold.
1. [Initialization] Set NUM:= 0
2. Repeat for K = 1932 to 1984:
If AUTO[K] > 300, then:
Set NUM := NUM+ 1
[End of loop.]
3. Return.

Example:
Print each year and the number of automobiles sold in that year.
1. Repeat for K = 1932 to 1984:
Write: K, AUTO[K]
[End of loop.]
2. Return

Inserting an Element into Linear Array:


1. It refers to the operation of adding another element to the collection LA.
2. Inserting an element at the "end" of a linear array.
3. Insert an element in the middle of the linear array.
Algorithm: INSERT(LA, N, K, ITEM)
1. [Initialize counter.] Set J:= N
2. Repeat Steps 3 and 4 while J ≥ K:
3. [Move Jth element downward.] Set LA[J + 1] := LA[J]
4. [Decrease counter.] Set J :=J - 1
[End of Step 2 loop.]
5. [Insert element.] Set LA[K] := ITEM
6. [Reset N.] Set N:= N + 1
7. Exit

You might also like