1 / 55

Array

Array. Variable: Placeholder/Structure to store a basic unit of data. Example: 1 integer variable stores single integer data. 1 float variable stores single float data. Question is: In a computer, where is that value assigned to a variable stored?

danc
Download Presentation

Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array Variable: Placeholder/Structure to store a basic unit of data. Example: 1 integer variable stores single integer data. 1 float variable stores single float data. Question is: In a computer, where is that value assigned to a variable stored? In memory, possibly Random Access Memory (RAM) But where exactly in memory? On some physical address in memory because, Memory is divided in physical locations and, Each location has a particular address associated with it.

  2. Variable 5 (a) int a = 5; char n = ‘z’; z (n)

  3. Array Variable: Sometimes in our program, There might be a need to store multiple data of similar type. Example: Roll numbers of 10 students. Marks of DFS of 10 students. 2 solutions: Create different variables each having a different name. int num1, num2, num3 etc. Create a collection of variables referred by a common name. int num[3] This looks much better solution.

  4. Array 5 (a) int a = 5; char n = ‘z’; (num) 11 int num[3] = {11, 10, 20}; 10 Ordered 20 Finite Same data type / Homogeneous z (n)

  5. Array Array: Finite, Ordered collection of Homogeneous data elements where, Ordering is maintained by storing all elements in, Continuous / Contiguous locations of memory. Properties: Finite: Contains only a limited (finite) number of elements. Ordered: All elements are stored one by one in continuous / contiguous locations of computer memory. Homogeneous: All the elements of an array are of the same data type. Example: An array of integers to store the roll numbers of all students in a class. An array of strings to store the names of all villagers in a village.

  6. Size / Length / Dimension Lower Bound (L) Array (Terminologies) int A[5] = {50, 25, 40, 10, 35} Base Address (M) Upper Bound (U) Pascal Fortran C Position (P) Values Address Index (i) Index (i) Index (i) Ap A[i] -3 1 0 50 1000 (A) 1 -2 2 1 25 1001 2 -1 3 2 40 1002 3 0 4 3 10 1003 4 1 5 4 35 1004 5 C Language: int A[5] C Language: int A[0...4] Fortran Language: int A[5] Fortran Language: int A[1...5] Pascal Language: int A[-3...1] Pascal Language: int A[-3...1]

  7. float A[0...5] = {50.50, 25.50, 40.50, 10.00, 35.00} Array (Terminologies) Values Address Values Address 50.50 1000 2 address locations 1001 2 bytes 50.50 1000 25.50 1002 25.50 1002 1003 40.50 1004 40.50 1004 Word Size (W) 1005 10.00 1006 10.00 1006 35.00 1008 1007 35.00 1008 1009

  8. Array Terminology: Size: Number of elements in an array. Also called: Length, Dimension. Example: int A[5] Size is 5. Type: Kind of data type it is meant for. Example: int A[5] Type is int. Base / Base Address (M): Address of the memory location where the first element of the array is located. Denoted by symbol M.

  9. Array Terminology: Index (i): Subscript by which the elements in an array can be referenced / accessed. Always an integer value. Lower Bound (L): Starting index of an array. Denoted by symbol L. Upper Bound (U): Ending index of an array. Denoted by symbol U. Example: A[1]: Element located on index 1. A[-3]: Element located at index -3.

  10. Array Terminology: Range of Indices: Depends on the programming language in which the array is created. In C, Index always starts from 0 upto Size-1. In Fortran, Index always starts from 1 to Size. In Pascal, Index can have user-defined integer values. Position (P): Relative rank of an element in the array. Does not depend on the programming language. Example: A1:First element in the array. A5:Fifth element in the array. Word Size (W): Size of one element in the array. Denoted by symbol W.

  11. Data: Array A[0...8] M = 1000 W = 4B Array (Answer the Questions) Position Index Array A Address Questions: What is the size of A? 9 What is the position of A[4]? 5 What is the index of A8? 7 What is the address of A[4]? 1016 What is the address of A2? 1004 Total memory occupied by A? 9*4 = 36B Which element is on address 1028? A8 or A[7]

  12. Data: Array A[-5...1] M = 2000 W = 2B Array (Answer the Questions) Position Index Array A Address Questions: What is the size of A? 7 What is the position of A[-4]? 2 What is the index of A6? 0 What is the address of A[-1]? 2008 What is the address of A8? Incorrect What is the address of A[2]? Incorrect Which element is on address 2004? Element with position 3 / Element with index -3.

  13. Data: Array A[-53...101] M = 1517 W = 4B Array (Answer the Questions) Questions: What is the size of A? Wait a minute. I’m calculating on fingers. What is the position of A[-4]? One second. What is the index of A61? You’re confusing me. What is the address of A[-1]? Stop it please. What is the address of A[-54]? Incorrect. Atleast, got 1 right answer. Very difficult to answer because the array seems to be very big. It is difficult to represent the array on paper. Equations are needed to answer these questions.

  14. Size = U – L + 1 Array (Formula to calculate Size) Array A[0...4] L = 0 U = 4 Array A[1...5] L = 1 U = 5 Array A[-3...2] L = -3 U = 2 Index Index Index L U Size(A) = 5 Size(A) = 5 Size = U – L + 1 Size(A) = U Size(A) = U + 1 Size = 2 – (-3) + 1 Size(A) = U + 1 - 0 Size(A) = U + 1 -1 Size = 2 + 3 + 1 = 6 Size(A) = U + 1 - L Size(A) = U + 1 - L

  15. Index = i = L + P - 1 Array (Formula to calculate Index from Position & vice-versa) Array A[0...4] L = 0, U = 4 Array A[1...5] L = 1, U = 5 Array A[-31...20] L = -31, U = 20 Position (P) Index (i) Position (P) Index (i) Position (P) Index (i) Find index of 4th element. Find index of element located at position 4. Find index of A4. Index(A1) = 0 Index(A1) = 1 = 2 - 1 Index(A2) = 1 Index(A2) = 2 = 3 - 1 Index(A3) = 2 Index(A3) = 3 Index(Ap) = P - 1 Index(Ap) = P Index = i = L + P - 1 Index(Ap) = P – 1 + 0 Index(Ap) = P -1 + 1 Index = i = -31 + 4 - 1 Index(Ap) = P – 1 + L Index(Ap) = P – 1 + L Index = i = -28

  16. Address = M + ( i – L ) * W Array (Formula to calculate Address from Index / Position) Array A[0...4] L = 0, U = 4, M=1000 Array A[1...5] L = 1, U = 5, M=1000 Index (i) Index (i) Address Address Will Word Size (W) matter? Yes Address(A[0]) = 1000 Address(A[1]) = 1000 Address(A[1]) = 1001 = 1000 + 1 Address(A[2]) = 1001 = 1000 + 1 Address(A[2]) = 1002 Address(A[3]) = 1002 = 1000 + 2 = 1000 + 2 Address(A[i]) = 1000 + i Address(A[i]) = 1000 + (i – 1) = M + i Address(A[i]) = M + (i - 0) Address(A[i]) = M + (i – 1) Address(A[i]) = M + ( i – L ) * W * W Address(A[i]) = M + ( i – L )

  17. Address = M + ( i – L ) * W Array (Formula to calculate Address from Index) Array A[-5...1], L = -5, U = 1, M = 1000, W = 4 Index (i) Address What is the address of element located at index -1? What is the address of A[-1]? Address = M + (i - L) * W Address = 1000 + (-1 – (-5)) * 4 Address = 1000 + (-1 + 5) * 4 Address = 1000 + (4) * 4 Address = 1000 + 16 Address = 1016

  18. Array Formulas / Equations: Size = U – L + 1 U: Upper Bound of the array. L: Lower Bound of the array. Index or i = L + P – 1 L: Lower Bound of the array. P: Position of element in the array. Address = M + (i – L) * W M: Base address of the array. i: Index of the element. L: Lower bound of the array. W: Word size of the array. Also called: Indexing Formula Could also be expressed in the form of Position ‘P’ as: Address = M + (P – 1) * W Because (i – L) = (P – 1)

  19. Address Index Array (Indexing Formula) Array in a program Array in memory Address = M + (i – L) * W Logical View Physical View

  20. Array Question-1: Suppose, an array A[-15...64] is stored in a memory whose starting address is 459. Assume that the word size for each element is 2. Then obtain the following: (a) How many number of elements are there in the array A? (b) How much memory is required to store the entire array? (c) What is the address location for A[50]? (d) What is the address location of 10th element? (e) Which element is located at address 599?

  21. Size = U – L + 1 i = L + P – 1 Address = M + (i – L) * W Array Data: L = -15 U = 64 M = 459 W = 2 Answer-1: (a) How many number of elements are there in the array A? Size = U – L + 1 Size = 64 – (-15) + 1 Size = 64 + 15 + 1 Size = 80 80 elements are there in the array A.

  22. Size = U – L + 1 i = L + P – 1 Address = M + (i – L) * W Data: L = -15, U = 64, M = 459, W = 2 Array (b) How much memory is required to store the entire array? Total memory required = Size * W Total memory required = 80 * 2 Total memory required = 160 Answer-1: (c) What is the address location for A[50]? i = 50 Address = M + (i – L) * W Address = 459 + (50 – (-15)) * 2 Address = 459 + (50 + 15) * 2 Address = 459 + (65) * 2 Address = 459 + 130 Address = 589

  23. Size = U – L + 1 i = L + P – 1 Address = M + (i – L) * W Data: L = -15, U = 64, M = 459, W = 2 Array (d) What is the address location of 10th element? P = 10 Index or i = L + P - 1 i = -15 + 10 - 1 Answer-1: i = -16 + 10 i = -6 Address = M + (i – L) * W Address = 459 + (-6 – (-15)) * 2 Address = 459 + (-6 + 15) * 2 Address = 459 + (9) * 2 Address = 459 + 18 Address = 477

  24. Size = U – L + 1 i = L + P – 1 Address = M + (i – L) * W Data: L = -15, U = 64, M = 459, W = 2 Array (e) Which element is located at address 599? Address = 599 Address = M + (i – L) * W Index or i = L + P - 1 599 = 459 + (i – (-15)) * 2 55 = -15 + P - 1 Answer-1: 599 = 459 + (i + 15) * 2 55 + 15 + 1 = P 599 = 459 + 2i + 30 P = 71 599 – 459 – 30 = 2i 2i = 110 i = 110 / 2 i = 55 71st element with index 55 is located on address 599. / A71 is on address 599. / A[55] is on address 599.

  25. Size = U – L + 1 i = L + P – 1 Address = M + (i – L) * W Array Question-2: Suppose, an array A[-51...48] is stored in a memory whose starting address is 1000. Assume that the word size for each element is 4. Then obtain the following: (a) How many number of elements are there in the array A? (b) How much memory is required to store the entire array? (c) What is the address location for A[1]? (d) What is the address location of 53rd element? (e) Which element is located at address 1076?

  26. Array Array A Question: Increment all the values by 1. Index Some Process/Procedure/Function needs to be done on the array. What is a ‘Process’ then? ‘Process’ is a sequence of steps that is executed to get the work done. Example: Process of making Tea. In Computer terminology, such process is called: ‘ALGORITHM’ / ‘PSEUDOCODE’. When an Algorithm is implemented in a programming language, it is called a: Values (Marks of DFS of 5 students) ‘PROGRAM’

  27. Array Algorithm v/s Program: Algorithm: It is not dependent on the programming language. It does not have to follow the syntax of any programming language. Should be easily understandable. Program: It is dependent on the programming language. It has to follow the syntax of the programming language in which it is implemented. Might not be easily understandable.

  28. Algorithm: TraverseArray Array Travel through the array. Visit each and every element of the array. Question: Increment all the values by 1. Array A Index Steps: 0 11 i = 0 A[0] = A[0] + 1 For i = 0 to 4 While i <= 4, do 1 20 A[1] = A[1] + 1 A[ i ] = A[ i ] + 1 A[ i ] = A[ i ] + 1 2 9 A[2] = A[2] + 1 i = i + 1 A[3] = A[3] + 1 3 18 EndWhile EndFor A[4] = A[4] + 1 4 15 Same thing is being done repetitively just using different values. Better to do it using a Loop.

  29. TRACING i = 0 Array Algorithm: TraverseArray Iteration-1: Condition 0 <= 4 True Question: Increment all the values by 1. A[0] = 11+1 = 12 i = 0 + 1 = 1 Array A Index Iteration-2: Condition 1 <= 4 True Steps: A[1] = 20+1 = 21 0 11 i = 0 i = 1 + 1 = 2 While i <= 4, do Iteration-3: Condition 2 <= 4 True 1 20 A[2] = 9+1 = 10 A[ i ] = A[ i ] + 1 2 9 i = 2 + 1 = 3 3 18 i = i + 1 Iteration-4: Condition 3 <= 4 True A[3] = 18+1 = 19 4 15 EndWhile i = 3 + 1 = 4 Iteration-5: Condition 4 <= 4 True Check whether this algorithm is working for this array or not. A[4] = 15+1 = 16 i = 4 + 1 = 5 How to check whether an algorithm is working or not? Iteration-6: Condition 5 <= 4 False

  30. Array Algorithm: TraverseArray Steps: Index Array A Index i = 0 i = L -2 0 11 While i <= 4, do While i <= U, do -1 1 20 A[ i ] = A[ i ] + 1 Process( A[ i ] ) 0 2 9 i = i + 1 i = i + 1 1 3 18 EndWhile EndWhile 2 4 15 Will this algorithm work for any kind of array? Whether C, Fortran, Pascal? Whether Small array, Large array etc? Question: Will this algorithm be able to do any kind of processing? Question:

  31. i = -2 Iteration-1: True Condition: -2<=1 Process(A[-2]) / Process(11) Array Algorithm: TraverseArray i = -2 + 1 = -1 Iteration-2: Trace the algorithm TraverseArray on the following array. True Condition: -1<=1 Process(A[-1]) i = -1 + 1 = 0 Iteration-3: Array A Steps: Index True Condition: 0<=1 i = L Process(A[0]) -2 11 L i While i <= U, do i = 0 + 1 = 1 -1 20 i Iteration-4: Process( A[ i ] ) True Condition: 1<=1 0 9 i i = i + 1 Process(A[1]) 1 18 U i i = 1 + 1 = 2 EndWhile Iteration-5: i = 2 False Condition: 2<=1 Stop

  32. Array Algorithm: TraverseArray. Input: Array A with elements. Output: According to Process(). Data Structure: Array A[L...U]

  33. Array Algorithm: TraverseArray Steps: i = L While i <= U, do Process( A[ i ] ) i = i + 1 EndWhile Stop

  34. Algorithm: SearchArray Array Question: Search for an element and tell whether it is present in the array or not. Array A Index 0 11 9 Search is successful. Return 2 (Index of 9). 1 20 22 Search is unsuccessful. Return NULL. 2 9 Question: Will TraverseArray algorithm be used here? 3 18 4 15

  35. i = L (i <= U) Steps: Algorithm: SearchArray KEY = 9 i = 0 , found = 0 , location = NULL While (i <= 4) && (found == 0), do Array A If (A[ i ] == KEY) Index Process (A[ i ]) If( A[ i ] == 9 ), then Question: 0 11 found = 1 Is it the most optimized / efficient? 1 20 location = i Question: EndIf 2 9 Will it work for any array? i = i + 1 3 18 Question: EndWhile 4 15 Will it work for any search value? If (found == 0), then print “Search is unsuccessful.” Else print “Search is successful.” EndIf Return(location)

  36. KEY = 20 Steps: Trace Algorithm: SearchArray i = -1 , found = 0 , location = NULL i = L, found = 0, location = NULL While (i <= U) && (found == 0), do Iteration-1: If( A[ i ] == KEY ), then Condition: -1<=2 && 0==0 True Array A Index If (11 == 20) False found = 1 -1 11 i = -1 + 1 = 0 location = i 0 20 Iteration-2: EndIf Condition: 0<=2 && 0==0 True 1 9 i = i + 1 If (20 == 20) True 2 18 EndWhile found = 1 If (found == 0), then location = 0 print “Search is unsuccessful.” i = 0 + 1 = 1 Else Iteration-3: print “Search is successful.” Condition: 1<=2 && 1==0 False EndIf Search is successful Return(location) Return (0)

  37. Array Algorithm: SearchArray. Input: Array A with elements. KEY: Element to be searched. Output: On Success, appropriate message and return Index/Location of KEY in array A. On Failure, appropriate message and return NULL. Data Structure: Array A[L...U]

  38. Steps: i = L, found = 0, location = NULL While (i <= U) && (found == 0), do If( A[ i ] == KEY ), then found = 1 location = i EndIf i = i + 1 EndWhile If (found == 0), then print “Search is unsuccessful.” Else print “Search is successful.” EndIf Return(location) Stop Algorithm: SearchArray

  39. Algorithm: InsertArray Array Question: Insert an element ’16’ in this array. Question: At which location / index, the element is to be inserted? Array A Array A Array A Index Index Index 0 11 0 11 0 11 1 20 1 20 1 20 2 9 2 2 9 3 18 3 18 3 18 4 15 4 15 4 Possible. Not possible. Possible. Last index / location is NULL. Array is already full. However, array seems to be polluted / corrupted.

  40. Algorithm: InsertArray Array Question: Insert an element ’16’ in this array. 1 Question: At which location / index, the element is to be inserted? Array A Array A Index Index Solution-1: Solution-2: 0 0 11 11 Steps: Steps: A[1] = 16 1 1 A[4] = A[1] 20 16 20 16 A[1] = 16 Stop 2 2 9 9 Stop 3 3 18 18 4 4 20 Does not work. Does not work. It should be insertion, not updation. Original sequence is changed, hence polluted.

  41. Algorithm: InsertArray Array Question: Insert an element ’16’ in this array. 1 Question: At which location / index, the element is to be inserted? Array A Index It seems that the only solution is: 0 11 Shifting down of elements to make place for the new element. 1 20 Question: Will downward shifting start from Top or Bottom? i.e. Will 20 be shifted down first or 18 will be shifted down first? 2 20 9 3 9 18 4 18

  42. Algorithm: InsertArray Array Question: Insert an element ’16’ in this array. 1 Question: At which location / index, the element is to be inserted? Array A Array A Index Index Downward Shifting starting from Top. Downward Shifting starting from Bottom. 0 0 11 11 Elements will be lost. All elements are preserved. 1 1 20 20 So it cannot work. So it can work. 2 2 9 9 20 20 3 3 9 18 18 4 4 18

  43. Algorithm: InsertArray LOCATION Array KEY Question: Insert an element ’16’ in this array. 1 Question: At which location / index, the element is to be inserted? While (i > 1) Array A Index Steps: i = 4 i = U A[4] = A[3] 0 11 While (i >= 2) While (i > LOCATION) A[ i ] = A[ i – 1] A[ i ] = A[ i – 1] A[3] = A[2] 1 16 20 i = i – 1 i = i – 1 2 20 9 A[2] = A[1] EndWhile EndWhile 3 18 9 4 18 A[1] = 16 A[1] = 16 A[LOCATION] = KEY

  44. Array A Index Algorithm: InsertArray Array 0 11 Question: Insert KEY = ’16’ in this array at LOCATION = ‘1’. 1 20 Steps: If (A[U] != NULL), then print “Array is full. Insertion not possible.” Else i = U While (i > LOCATION) A[ i ] = A [ i – 1 ] i = i – 1 EndWhile A[LOCATION] = KEY EndIf Stop 2 9 3 18 4 15 Array A Index Controls the downward shifting of elements. 0 11 1 20 2 9 3 18 4

  45. -2 10 KEY = 20, LOCATION = -1 Array A i = 1 Index Trace Algorithm: InsertArray -1 30 Steps: If (A[U] != NULL), then print “Array is full. Insertion not possible.” Else i = U While (i > LOCATION) A[ i ] = A [ i – 1 ] i = i – 1 EndWhile A[LOCATION] = KEY EndIf Stop Iteration-1: -2 10 0 40 True Condition: 1 > -1 A[ 1 ] = A[ 0 ] //A[1] = 40 -1 30 1 40 i = 1 – 1 = 0 0 40 -2 10 1 50 Iteration-2: True -1 Condition: 0 > -1 30 A[ 0 ] = A[ -1 ] //A[0] = 30 Array A Index 0 30 i = 0 – 1 = -1 -2 1 10 40 Iteration-3: -1 False Condition: -1 > -1 30 10 -2 0 40 20 -1 1 A[ -1 ] = 20 30 0 40 1

  46. Array Algorithm: InsertArray. Input: Array A with elements. KEY: Element to be inserted. LOCATION: Index where KEY is to be inserted. Output: On Success, Element with value KEY inserted at index LOCATION. On Failure, appropriate message to be displayed. Data Structure: Array A[L...U]

  47. Steps: If (A[U] != NULL), then print “Array is full. Insertion not possible.” Else i = U While(i > LOCATION) A[ i ] = A[ i – 1 ] i = i – 1 EndWhile A[LOCATION] = KEY EndIf Stop Algorithm: InsertArray

  48. Array How to convert any algorithm into a ‘C’ program?

  49. Algorithm: DeleteArray Array Question: Delete an element from the array. Question: What is the value of the element to be deleted? Array A Index 60 Element is not there in the array. It cannot be deleted. 10 20 Search is successful. 20 is available at index/location 1. A[1] = NULL 30 20 Empty location is in between the other elements of the array. It should be at the tail end of the array. 40 30 40 So it leaves the array polluted / corrupted. 50 Can the last element ’50’ come to take place of deleted element ’20’ directly? 50 No. Original sequence of elements will get changed. It will pollute / corrupt the array. Question: Will the upward shifting start from Bottom / Top? Hence the remaining values needs to shifted upwards so that the empty space automatically moves downwards.

  50. Algorithm: DeleteArray Array Question: Delete an element from the array. 20 Question: What is the value of the element to be deleted? Array A Array A Index Index Solution-2: Solution-1: (Upward shifting starting from Top) (Upward shifting starting from Bottom) 10 10 i = SearchArray(A, 20) // i = 1 i = SearchArray(A, 20) // i = 1 20 30 20 40 30 30 An element is lost. All elements are preserved. So this cannot work. 40 40 50 50 So this can work. 50 50

More Related