Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Variable: Placeholder/Structure to store a basic unit of data.

Similar presentations


Presentation on theme: "Array Variable: Placeholder/Structure to store a basic unit of data."— 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 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 5 (a) int a = 5; char n = ‘z’; z (n)

3 Array Variable: Sometimes in our program, Example: 2 solutions:
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 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 5 (a) int a = 5; char n = ‘z’; 11 (num) 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 Array (Terminologies)
Size / Length / Dimension Lower Bound (L) 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] 1 -3 1 50 1000 (A) 2 -2 2 1 25 1001 3 -1 3 2 40 1002 4 4 3 10 1003 5 1 5 4 35 1004 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 Array (Terminologies)
float A[0...5] = {50.50, 25.50, 40.50, 10.00, 35.00} 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: Type: Base / Base Address (M):
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. 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: Position (P): Word Size (W):
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 Array (Answer the Questions)
Data: Array A[0...8] M = 1000 W = 4B Position Index Array A Address Questions: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1000 1004 1008 1012 1016 1020 1024 1028 1032 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 Array (Answer the Questions)
Data: Array A[-5...1] M = 2000 W = 2B Position Index Array A Address Questions: 1 2 3 4 5 6 7 -5 -4 -3 -2 -1 1 2000 2002 2004 2006 2008 2010 2012 What is the size of A? 7 What is the position of A[-4]? 2 What is the index of A6? 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 Array (Answer the Questions)
Data: Array A[ ] M = 1517 W = 4B 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 Array (Formula to calculate Size)
Size = U – L + 1 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 1 2 3 4 L 1 2 3 4 5 -3 -2 -1 1 2 U Size(A) = 5 Size(A) = 5 Size = U – L + 1 Size(A) = U + 1 Size(A) = U Size = 2 – (-3) + 1 Size(A) = U + 1 - Size(A) = U + 1 -1 Size = = 6 Size(A) = U L Size(A) = U L

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

16 Array (Formula to calculate Address from Index / Position)
Address = M + ( i – L ) * W 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 1 2 3 4 1000 1001 1002 1003 1004 1 2 3 4 5 1000 1001 1002 1003 1004 Will Word Size (W) matter? Yes Address(A[0]) = 1000 Address(A[1]) = 1000 Address(A[1]) = 1001 = Address(A[2]) = 1001 = Address(A[2]) = 1002 = Address(A[3]) = 1002 = Address(A[i]) = i = M + i Address(A[i]) = (i – 1) Address(A[i]) = M + (i - 0) Address(A[i]) = M + (i – 1) Address(A[i]) = M + ( i – L ) * W Address(A[i]) = M + ( i – L ) * W

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

18 Array Formulas / Equations: Size = U – L + 1 Index or i = L + P – 1
U: Upper Bound of the array. L: Lower Bound of the array. Index or i = L + P – 1 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 Array (Indexing Formula)
Address Index L L+1 L+2 . i U-2 U-1 U Array in a program Array in memory M . Address = M + (i – L) * W Logical View Physical View

20 Array Question-1: Suppose, an array A[ ] 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 Array Answer-1: Data: L = -15 U = 64 M = 459 W = 2
Size = U – L + 1 i = L + P – 1 Address = M + (i – L) * W Array Answer-1: Data: L = -15 U = 64 M = 459 W = 2 (a) How many number of elements are there in the array A? Size = U – L + 1 Size = 64 – (-15) + 1 Size = Size = 80 80 elements are there in the array A.

22 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: (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 (c) What is the address location for A[50]? i = 50 Address = M + (i – L) * W Address = (50 – (-15)) * 2 Address = ( ) * 2 Address = (65) * 2 Address = Address = 589

23 Array Answer-1: (d) What is the address location of 10th element?
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: (d) What is the address location of 10th element? P = 10 Index or i = L + P - 1 i = i = i = -6 Address = M + (i – L) * W Address = (-6 – (-15)) * 2 Address = ( ) * 2 Address = (9) * 2 Address = Address = 477

24 Array Answer-1: (e) Which element is located at address 599?
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: (e) Which element is located at address 599? Address = 599 Address = M + (i – L) * W Index or i = L + P - 1 599 = (i – (-15)) * 2 55 = P - 1 599 = (i + 15) * 2 = P 599 = i + 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[ ] is stored in a memory whose starting address is 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 Increment all the values by 1.
Array Index Array A Question: Increment all the values by 1. Some Process/Procedure/Function needs to be done on the array. 1 2 3 4 11 20 9 18 15 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: Program:
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 Array Algorithm: TraverseArray Travel through the array.
Visit each and every element of the array. Question: Increment all the values by 1. Index Array A Steps: 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 Increment all the values by 1.
Array TRACING i = 0 Algorithm: TraverseArray Iteration-1: Condition 0 <= 4 True Question: Increment all the values by 1. A[0] = 11+1 = 12 i = = 1 Index Array A Steps: Iteration-2: Condition 1 <= 4 True A[1] = 20+1 = 21 11 i = 0 i = = 2 While i <= 4, do 1 20 Iteration-3: Condition 2 <= 4 True A[2] = 9+1 = 10 A[ i ] = A[ i ] + 1 2 9 i = = 3 3 18 i = i + 1 Iteration-4: Condition 3 <= 4 True A[3] = 18+1 = 19 4 15 EndWhile i = = 4 Iteration-5: Condition 4 <= 4 True Check whether this algorithm is working for this array or not. A[4] = 15+1 = 16 i = = 5 How to check whether an algorithm is working or not? Iteration-6: Condition 5 <= 4 False

30 Array Algorithm: TraverseArray Steps: Index Index Array A i = 0 i = L
-2 11 While i <= 4, do While i <= U, do -1 1 20 A[ i ] = A[ i ] + 1 Process( A[ i ] ) 2 9 i = i + 1 i = i + 1 1 3 18 EndWhile EndWhile 2 4 15 Question: 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?

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

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

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

34 Array Algorithm: SearchArray Question: Search for an element and tell
whether it is present in the array or not. Index Array A 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 Algorithm: SearchArray
i = L (i <= U) Steps: KEY = 9 i = 0 , found = 0 , location = NULL While (i <= 4) && (found == 0), do Index Array A If (A[ i ] == KEY) Process (A[ i ]) If( A[ i ] == 9 ), then Question: 11 found = 1 Is it the most optimized / efficient? 1 20 location = i Question: 2 9 EndIf Will it work for any array? i = i + 1 3 18 EndWhile Question: 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 Trace Algorithm: SearchArray
KEY = 20 Steps: 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 Index Array A If (11 == 20) False found = 1 -1 11 location = i 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 = = 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: Input: Output: Data Structure: SearchArray.
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 Algorithm: SearchArray
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.” Return(location) Stop

39 to be polluted / corrupted.
Array Algorithm: InsertArray Question: Insert an element ’16’ in this array. Question: At which location / index, the element is to be inserted? Index Array A Index Array A Index Array A 11 11 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 Array Algorithm: InsertArray Question:
Insert an element ’16’ in this array. Question: At which location / index, the element is to be inserted? 1 Index Array A Index Array A Solution-1: Solution-2: 11 11 Steps: Steps: 1 20 16 A[1] = 16 1 20 A[4] = A[1] 16 Stop A[1] = 16 2 9 2 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 Array Algorithm: InsertArray Question:
Insert an element ’16’ in this array. Question: At which location / index, the element is to be inserted? 1 Index Array A 11 It seems that the only solution is: 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 18 9 4 18

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

43 Array Algorithm: InsertArray LOCATION KEY Question:
Insert an element ’16’ in this array. Question: At which location / index, the element is to be inserted? 1 While (i > 1) Index Array A Steps: i = 4 i = U A[4] = A[3] 11 While (i >= 2) While (i > LOCATION) 1 A[3] = A[2] A[ i ] = A[ i – 1] A[ i ] = A[ i – 1] 20 16 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 Algorithm: InsertArray Steps: If (A[U] != NULL), then
Index Array A Algorithm: InsertArray 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 Index Array A Controls the downward shifting of elements. 11 1 20 2 9 3 18 4

45 Trace Algorithm: InsertArray
-2 10 KEY = 20, LOCATION = -1 Index Array A i = 1 -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 Condition: 1 > -1 True 40 -1 A[ 1 ] = A[ 0 ] //A[1] = 40 30 1 40 i = 1 – 1 = 0 40 -2 10 1 50 Iteration-2: Condition: 0 > -1 True -1 30 Index Array A A[ 0 ] = A[ -1 ] //A[0] = 30 i = 0 – 1 = -1 30 -2 10 1 40 Iteration-3: -1 30 Condition: -1 > -1 False 10 -2 40 -1 20 1 A[ -1 ] = 20 30 40 1

46 Array Algorithm: Input: Output: Data Structure: InsertArray.
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 Algorithm: InsertArray
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

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

49 Array 1 2 3 4 Algorithm: DeleteArray 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. 1 2 3 4 10 20 Search is successful. 20 is available at index/location 1. 30 20 A[1] = NULL Empty location is in between the other elements of the array. It should be at the tail end of the array. 40 30 50 40 So it leaves the array polluted / corrupted. 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 Array 1 2 3 4 1 2 3 4 Algorithm: DeleteArray Question:
Delete an element from the array. Question: What is the value of the element to be deleted? 20 Array A Array A Index Index Solution-2: Solution-1: (Upward shifting starting from Top) (Upward shifting starting from Bottom) 1 2 3 4 1 2 3 4 10 10 i = SearchArray(A, 20) // i = 1 20 i = SearchArray(A, 20) // i = 1 30 20 30 30 40 An element is lost. All elements are preserved. 40 50 So this cannot work. 40 50 So this can work. 50 50

51 Array 1 2 3 4 KEY Algorithm: DeleteArray Question:
Delete an element from the array. Question: What is the value of the element to be deleted? 20 While(i < 4), do Array A Steps: Index i = SearchArray(A, 20) // i = 1 i = SearchArray(A, 20) // i = 1 i = SearchArray(A, KEY) 1 2 3 4 10 While(i <= 3), do While(i < U), do A[ 1 ] = A[ 2 ] 30 20 A[ i ] = A[ i + 1 ] A[ i ] = A[ i + 1 ] A[ 2 ] = A[ 3 ] 30 40 i = i + 1 i = i + 1 A[ 3 ] = A[ 4 ] 40 50 EndWhile EndWhile 50 A[ 4 ] = NULL A[ 4 ] = NULL A[ U ] = NULL

52 Array 1 2 3 4 Steps: i = SearchArray(A, KEY) If( i == NULL ), then
Index Array Array A 1 2 3 4 60 Algorithm: DeleteArray 70 Question: Delete an element with KEY = ’20’ from the array. 80 Steps: 90 i = SearchArray(A, KEY) 100 If( i == NULL ), then print “KEY not found. Deletion not possible.” Else Array A Index While(i < U), do Controls the Upward Shifting of elements starting from Top. 1 2 3 4 10 A[ i ] = A[ i + 1 ] i = i + 1 20 EndWhile 30 A[ U ] = NULL 40 EndIf 50 Stop

53 Trace Algorithm: DeleteArray
KEY = ’22’ i = -1 Array A Index -2 -1 1 11 Condition: -1 == NULL False 33 -2 -1 1 Steps: 44 Iteration-1: 33 Condition: -1 < 1 True 55 i = SearchArray(A, KEY) 44 A[ -1 ] = A[ 0 ] //A[-1] = 33 If( i == NULL ), then 66 i = = 0 print “KEY not found. Deletion not possible.” -2 -1 1 11 77 Iteration-2: Else 33 Condition: 0 < 1 True While(i < U), do 44 A[ 0 ] = A[ 1 ] //A[0] = 44 Array A Index A[ i ] = A[ i + 1 ] 44 i = = 1 i = i + 1 -2 -1 1 11 Iteration-3: EndWhile Condition: 1 < 1 False -2 -1 1 11 22 33 A[ U ] = NULL A[1] = NULL 33 44 EndIf 44 Stop

54 Array Algorithm: Input: Output: Data Structure: DeleteArray.
Array A with elements. KEY: Element to be deleted. Output: On Success, Element with value KEY deleted from the array. On Failure, appropriate message to be displayed. Data Structure: Array A[L...U]

55 Algorithm: DeleteArray
Steps: i = SearchArray(A, KEY) If( i == NULL), then print “KEY not found Deletion not possible.” Else While(i < U) A[ i ] = A[ i +1 ] i = i + 1 EndWhile A[U] = NULL EndIf Stop


Download ppt "Array Variable: Placeholder/Structure to store a basic unit of data."

Similar presentations


Ads by Google