This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.

Find out moreTazeen Muzammil MSSE

Physical Memory

We have to store our data structures in the memory of our computer. Memory is organized into banks, rows, columns etc. We supply a bank number, row number etc (= an address), memory returns us the contents Address

contents

Physical Memory

Arrays

The array is the most commonly used data storage structure; its built into most programming languages. Since arrays are usually easy to traverse, search and sort, they are frequently used to store relatively permanent collection of data. But if the size of the structure and the data in the structure is constantly changing the array may not be useful. The elements of the array may be denoted by the bracket notation

A[1], A[2], A[3] .A[n]

Linear Array

A linear array is a list of finite number n of homogeneous data elements such that:

± The elements of the array are referenced respectively by an index set consisting of n consecutive numbers. ± The elements of the array are sorted respectively in successive memory location. The number n of elements is called the length or size of the array.

**Length of the Array
**

The length or the number of data elements of the array can be obtained by this formula:

Length= UB-LB+1 where: UB is the largest index, called the upper bound LB is the smallest index, called the lower bound

**Representation of Linear Arrays in Memory
**

Let LA be a linear array The elements of LA are stored in successive memory cells. So the computer does not need to keep track of the address of every element of LA, but needs to keep track only of the address of the first element of LA denoted by:

Base(LA)

Using this address the computer can calculate the address of any element of LA by the following formula:

LOC(LA[k])=Base(LA)+w(K-LB)

where:

K is the array index W is the width of the record

Representation of Linear Arrays in Memory

Exercise

Consider the linear arrays AAA(5:50), BBB(5:10), and CCC(18)

a) Find the number o elements in each array b) Suppose Base(AAA)=300 and w=4 for AAA. Find the address of AAA[15], AAA[35] and AAA[55]

**Traversing Linear Array
**

Let A be collection of data elements stored in the memory of the computer. If we want to count the number of elements of A or we want to print the contents of each element of A, this can be do by traversing A. Traversing means accessing and processing each element exactly once. Traversing a linear data structure is easier as compared to traversing a non linear structure.

**Algorithm (using repeat-while loop)
**

Here LA is a linear array with lower bound LB and upper bound UB. This algorithm traverses LA applying an operation PROCESS to each element of LA. 1. [Initialize counter] Set K=LB 2. Repeat steps 3 and 4while K< UB 3. [Visit element]Apply PROCESS to LA[K] 4. [Increase counter] Set K=K+1 5. [End of step 2 loop] 6. Exit.

**Algorithm (using repeat-for loop)
**

Here LA is a linear array with lower bound LB and upper bound UB. This algorithm traverses LA applying an operation PROCESS to each element of LA. 1. Repeat for K=LB to UB 2. [Visit element]Apply PROCESS to LA[K] 3. [End of loop] 4. Exit.

Exercise

Consider the array AUTO which records the number of automobiles sold each year from 1932 through 1984 a) Find the number NUM of years during which more than 300 automobiles were sold. b) Print each year and the number of automobiles sold in that year.

**Inserting and Deleting
**

Let A be collection of data elements stored in 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 element from A. Inserting an element at the end of a linear array can be easily done provided the memory space allocated for the array is large enough.

Inserting Elements

**Algorithm for Insertion
**

LA is the linear array with N elements and K is the positive integer such that K< N. This algorithm inserts an element ITEM into the Kth position in LA. 1. 2. 3. 4. 5. 6. 7. 8. [Initialize counter] Set J=N Repeat steps 3 and 4while J > K [Move Jth element down]Set LA[J+1] = LA[J] [Decrease counter]Set J=J-1 [End of step 2 loop] [Insert element]Set LA[K]=ITEM [Reset N]Set N=N+1 Exit.

**Algorithm for Deletion
**

LA is the linear array with N elements and K is the positive integer such that K< N. This algorithm deletes an element ITEM into the Kth position in LA. 1. 2. 3. 4. 5. 6. Set ITEM =LA[K] Repeat for J = K to N-1 [Move J+1 element upward]Set LA[J] = LA[J+1] [End of loop] [Reset N]Set N=N-1 Exit.

**Benefits of Using Array
**

We can retrieve an array element from its index in constant time, meaning it costs us asymptotically nothing to look up a record. Consist solely of data, no space wasted on links. Physical continuity/memory locality: if we look up element i, there is a high probability we will look up element i+1 next

**Drawbacks of Using Array
**

Inflexible: we have to decide in advance how much space we want when the array is allocated. Once the block of memory for the array has been allocated, that s it we re stuck with the size we ve got. If we try to write past the end of the array (overflow), we ll be intruding on memory allocated for something else causing a segmentation fault. We can compensate by always allocating arrays larger than we think we ll need, but this wastes a lot of space.

**Multi dimensional Arrays
**

A two-dimensional m*n array A is a collection of m.n data elements such that each element is specified by a pair if integers(such as J,K), called subscript. Two-dimensional arrays are sometime called matrix array. Two-dimensional 3*4 array A

1. A[1,1] 2. A[2,1] 3. A[3,1] A[1,2] A[2,2] A[3,2] A[1,3] A[2,3] A[3,3]

**Representation of Two-Dimensional Arrays in Memory
**

A is a two dimensional 3*4 array

**Column-major order
**

[1,1] [2,1] [3,1] [1,2] [2,2] [3,2] [1,3] [2,3] [3,3] [1,4] [2,4] [3,4] C4 C3 C2 C1

**Row-major order
**

[1,1] [1,2] [1,3] [1,4] [2,1] [2,2] [2,3] [2,4] [3,1] [3,2] [3,3] [3,4] r3 r2 r1

Matrix Multiplication

Let A be an M* P matrix, and let B be a P*N matrix array. This algorithm stores the product of A and B in a M*N matrix array C

1. Repeat Steps 2 to 4 for I=1to M 2. Repeat Steps 3 to 4 for J=1to N 3. Set C[I,J]=0 Initialize C[I,J] 4. Repeat for K=1 to P 5. C[I,J]=C[I,J]+A[I,K]*B[K,J]

[END of inner loop] [END of Step 2 loop] [END of Step 1 loop]

- TASPW1 Installation Log
- Assignment#4
- HHS_en01_Being_a_Hacker
- 1 - Intro to SW Engg.
- Readme
- class exercise session 1,2
- Software Testing
- Software Quality Assurance
- Project Planning
- Project Management
- Lec 6-String Processing
- Lec 5-Stacks and Queues
- Lec 4-Sorting Techniques
- Lec 3-Searching Techniques
- Lec1-Introduction to Data Structure and Algorithms
- Muslim 3
- Srs Template
- Softcell Example Srs
- Req. Engg. Spec. Analysis. Model.
- Introduction to UML
- Happy Holiday Homes
- 2 - Software Process and Life Cycles
- 1 - Intro to SW Engg.
- Uml Sequence Diagrams

- Algorithms, notes
- Lec1-Introduction to Data Structure and Algorithms
- Label Cas
- Lesson Plan for c&Datastructure
- Custom Config Doc
- Popular sorting algorithms
- Array
- Report Writing
- monoalphateic cipher.ppt
- ROOT
- 120Wireeles
- LAtestFBODschemetwocol Backup
- mt2
- Cryptography and Network Security QB
- Algorithms CS
- C Paper
- Users Guide 5 26
- Assembler
- data stucture and algorithm questions
- Fd 25951958
- Database Model
- Solid Area Scan Conversion
- HowToGuideRecordsAPI_EN.pdf
- Collections 2
- huuli
- Linked Listnotes
- Hfm Overview
- Tarjei Mandt
- Hash Set
- Multi Array

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd