Sei sulla pagina 1di 16

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

ARRAYS DATA STRUCTURE: A data structure is a class of data that can be characterized by its organization and the operations that are defined on it. Data structures are sometimes called data types. In a program, every variable is of some explicitly or implicitly defined data structures, which determines the set of operations that can be performed on that variable. CLASSIFICATION OF DATA STRUCTURES: Primitive data structures: They are not composed of other data structures. Directly supported by the programming languages n are directly operated upon by machine-level instructions. Ex. Integers, Booleans and Characters. Non primitive data structures: Data structures which are constructed from one or more primitive data structures. They are further categorized as simple data structures, linear and nonlinear data structures and file organization. Simple data structures include strings, arrays and records. Simple data structures can be combined in various ways to form more complex structures. The two fundamental complex data structures are linear and nonlinear, depending on the complexity of the logical relationship they represent. The linear data structures are stacks, queues, and linear linked list. The nonlinear data structures include trees and graphs. The data structuring techniques applied to collection of data that are managed as black boxes by operating systems are commonly called file organizations. A file carries a name, contents, a location where it is kept, and some administrative information like who owns it and how big it is. The basic file organizations are: sequential, random access, indexed sequential etc ARRAYS An array is a finite, ordered set of homogeneous elements. The ordering property means that the first, second, third.. nth element of an array can be identified. The elements of array are homogeneous i.e. they all have the same data type. One array might be comprised of elements that are all strings: another might be comprised of elements that are all integer. The elements of an array may also each be arrays. Arrays are also commonly referred to as tables.

-1 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

Arrays are basic building blocks for more complex data structures. Nearly every kind of complex structure can be represented indirectly using an array. The simplest form of array is a one-dimensional array or vector. A one-dimensional array named A, which consist of N elements can be represented as: A(1) A(I) . A(N) Subscripts The subscript or index, of an element designates its position in the arrays ordering. A particular element in an array is identified by the name of the array followed by the elements subscript enclosed in parentheses, i.e. A(I). Other possible notation to designate the element of array A with subscript I include A[I], AI and AI. Only the array itself is given name. Elements of an array are referred to by their subscripts i.e. by their relative position in the array. Definition The one-dimensional array A with elements of data structure type T having subscripts extending from L through U is A(L:U) = {A(I)} For I = L, L+1, , U-1, U Where each element A(I) is a data structure of type T. The notation A(L:U) indicates that As subscript value ranges from L through U. The number of elements in an array is called range. The range of array A(L:U) is U L + 1. The range of array A(1:N) is N. The minimum allowable value of an arrays subscript is called its lower bound; the maximum allowable value is called its upper bound. In the definition of an array L is the lower bound and U is the upper bound. Example: The temperatures recorded in a city for every hour during a 24-hour period. These elements are ordered by their corresponding time of the day. All elements are of the same kind: each is a temperature. The natural bound is 1 and the upper bound is 24. If the array name is T, T(I) is the temperature recorded at hour I, for 1 I 24. For certain applications lower bound which is not 1 can also be used. Ex. Temperature of an experiment that are recorded every ten seconds from time zero might more conveniently be stored in an array with lower bound 0.

-2 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

The lower bound may even be negative. Ex. Array which stores data to be stored graphically such as on an axis of points ranging from -100 to +100. MULTIDIMENSIONAL ARRAYS A two-dimensional array is an array in which each element is itself an array. An array named A which consists of M elements, each of which is an array of N elements can be depicted as an M-by-N table. 1 2 J N

1 2 I M Subscripts It is necessary to specify two subscript values in order to identify an individual element of a two-dimensional array. The first subscript refers to a row of the array and the second subscript refers to a column of the array. A(I,J) is the element of A that is in the Ith row and Jth column of array A. 1 2 J N

A(I,J) 1

-3 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

2 I M Definitions The two dimensional array A with individual elements of type T having row subscript ranging from 1 to M and column subscript from ranging from 1 to N is A(1:M, 1:N) = {A(I,J)} For I = 1.M and J = 1.N Where each A(I,J) is a data structure of type T. Array A is said to be of dimensions M by N. There are M elements in each row and N elements in each column of array A, thus total number of elements in array A is M * N. In general, a two-dimensional array A with its first subscript having lower bound L1 and upper bound U1 and its second subscript having lower bound L2 and upper bound U2 is defined as: A(L1:U1,L2:U2) = {A(I,J)} for L1 I U1 and L2 J U2 Where each A(I,J) is a data structure of type T. The number of elements in a row of A is U2 L2 + 1 and the number of elements in a column of A is U1 L1 + 1. Thus the total number of elements in array A is (U2 L2 + 1) * (U1 L1 + 1). Example: Data about the grades of students in a class on the four semester-exams. Array GRADE wld have dimensions M by 4, where M is the number of students. GRADE(I,J) is the grade achieved by the Ith student on the Jth exam. The value of I must be in between 1 to M and J must be in between 1 to 4. Each element of the array is a grade, they cld all be integers, or all be fixed-point numbers with two decimal places, or all be characters, depending on the convention adopted. Cross-section A cross-section of a two-dimensional array is obtained by holding one of the subscripts constant while the other through its entire range of values. The notation that is commonly used to denote a cross-section is an asterisk (*) for the subscript value that is to be allowed to take on its entire range of values. A(*,5) refers to the fifth column of the array A. That is A(*,5) = { A(1,5),A(2,5)..A(M,5)}

-4 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

A(I,*) is the Ith row of the array A. Ex. GRADE(*,3) is the column of GRADE values for recording exam grades for all students on the third exam. W(47,*) is the row of W values recording the weather conditions on the 47 th day of the year. A cross-section of a two-dimensional array is essentially a slice taken from the array. Transpose The transpose of a two-dimensional array is obtained by reversing the subscript positions. The transpose of an M-by-N array is an N-by-M array. The transpose of the array A is denoted as AT. The transpose of the array A(M,N) is shown in fig. 1 2 I M

1 2 J N By definition, A(I,J) = AT(J,I) Example, A(2,9) = AT(9,2) The element in row 2, column 9 of array A is the same as the element in row 9, column 2 of the transpose of array A. More Dimensions An array can be defined to be three-dimensional, four-dimensional, N-dimensional. An N-dimensional array requires that the value of N subscripts be specified in order to identify an individual element of the array. An N-dimensional array A cld be defined as: A(L1:U1, L2:U2, LN:UN)

-5 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

An individual element of array A can be specified as A(I1,I2,,IN) where each subscript Ik is within the appropriate bounds LK IK UK, for each k = 1,2,.,N. The total number of elements in array A is
N

i.e.

(UK LK +1) k=1 (U1 L1 + 1) * (U2 L2 + 1) *.*(UN LN + 1)

Example: Data about the number of students in each of a universitys three classifications of students, by sex, for all of a universitys five colleges can be stored as three-dimensional array. Such an array can be called COLLEGE and can have dimensions 3 by 2 by 5. And it can be drawn as:

The value of element COLLEGE(I,J,K) is an integer representing the number of students in class I of sex J for college K. I must be 1,2 or 3; J must be 1 or 2; K must be 1,2,3,4 or 5. Cross-section of a multi-dimensional array is formed by holding one or more subscript values constant and varying each of the other subscripts through its entire range of values. Example: COLLEGE(1,*,3) is the row of COLLEGE containing the number of students in the 1 st class of college 3 for all the sexes. COLLEGE(*,*,3) is the plane of COLLEGE containing the number of students in college 3 for each class and both sexes. MAPPINGS TO STORAGE: ONE-DIMENSIONAL ARRAYS Lower Bound: One

-6 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

EMP-NO is a one-dimensional array with subscript lower bound 1 and subscript upper bound N. One way to store this array is so that the physical order of elements is the same as the logical order of elements. Storage for element EMP-NO( I+1) will be adjacent to storage for element EMP-NO(I), for I = 1,.,N-1. In order to calculate the starting address of element EMP-NO(I), it is necessary to know: 1. The starting address of the storage space allocated to the array. 2. The size of each element in the array. Take B as arrays starting address, also known as base location. Assume that each element of the array occupies S bytes. Then the location of the Ith element of the array is B + (I -1) * S because I -1 elements, each of size S, physically precede the Ith element. Generalizing the Lower Bound Consider an array declared as Z(4:10), which is having lower bound of not equal to one. The starting address of Z(6) is B + (6 4) * S because 6 4 i.e. 2 elements precede Z(6). For an array declared Z(-2:2), the location of Z(1) is B + (1 (-2)) * S because 1 (-2) i.e. 3 elements precede Z(1). In general the element A(I) of the array defined as A(L:U) is at location B + (I L) * S The formula is correct for all the lower bound i.e. positive, negative or zero. MAPPINGS TO STORAGE: MULTI-DIMENSIONAL ARRAYS Row-major Order As computers memory is linear, a multi-dimensional array must be linearized as it mapped to storage. One alternative for linearization is to store first the first row of the array, then the second row, then the third row and so forth. Example: The array defined by A(1:4,1:6), which logically appears as shown below 1 2 3 4 5 6

-7 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

1 A(2,4)

2 3 4 And it appears physically in row-major order as: | Row 1 | Row 2 | Row 3 | Row 4 |

A(2,4) Assuming the that B is the arrays base address and that each element of the array is of size S, the starting address of the element A(I,J) is B + (I 1)*6*S + (J 1)*S becos there are I 1 rows, each of length 6*S, which precede the row that element A(I,J) is in, and there are J 1 elements, each of length S, which precede element A(I,J) in row I. Element A(2,4) is located at B + 9*S. The element array A(I,J) of array defined by A(L1:U1,L2:U2) is at location: B + (I L1) * (U2 L2 + 1)*S + (J L2)*S bcos there are I L2 rows, each of length (U2 L2 +1)*S, which precede the row that element A(I,J) is in, and there are J L2 elements, each of length S, which precede element A(I,J) in row I. Example: The array defined by Z(-2:2, 4:6) can be depicted logically as: 4 5 6

-2 -1

Z(0,6)

-8 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

0 1 2 This array appears physically in row-major order as: | Row -2 | Row -1 | Row 0 | Row 1 | Row 2 |

Z(0,6) There are two rows (row -2 and row -1), each of length 3 * S, preceding row 0. In row 0, there are 2 (i.e. 6 4) elements, each of length S, preceding element Z(0,6). The starting location of Z(0,6) is B + (0 (-2)) * (6 4 + 1)*S + (6 4)*S, Which is B + 8*S. With an N-dimensional array, row-major order varies the subscripts in right-to-left order. For array A(L1:U1,L2:U2,.,LN:UN) the elements are stored in the following order: A(L1,L2,.,LN), A(L1,L2,.,LN+1),.., A(L1,L2,.,UN) A(L1,L2,LN-1+1,LN), A(L1,L2,.,LN-1+1,LN+1),.., A(L1,L2,...,LN-1+1,UN),., A(L1,L2,.,UN-1,UN),., A(U1,L2,..,LN-1,LN), A(U1,L2,..,LN-1,LN+1),.., A(U1,L2,,UN-1,UN),, A(U1,U2,,UN) Column-major Order Another alternative for linearization of two-dimensional array is to store the elements in column-major order, i.e. store first the first column, then the second, then the third and so forth. The A array appears physically in column-major order as:

-9 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

| Col 1

Col 2

Col 3

Col 4

Col 5 |

Col 6

A(2,4) Assuming that B is the arrays base address and that each element of the array is of size S, the starting address of the element A(I,J) is B + (J 1)*4*S + (I 1)*S bcos there are J 1 columns, each of length 4* S, which precede the column that element A(I,J)is in, and there are I 1 elements, each of length S, which precede A(I,J) in column J. Element A(2,4) is located at B + 13*S. The element A(I,J) of the array defined by A(L1:U1,L2:U2) with column-major storage is at location: B + (J L2) * (U1 L1 + 1)*S + (I L1)*S With an N-dimensional array, column-major order varies the subscripts in left-to-right order. For array A(L1:U1,L2:U2,..,LN:UN) The elements are stored in the following order: A(L1,L2,..LN) A(L1 +1,L2,..LN),., A(U1,L2,.,LN) A(L1,L2+1,,LN) A(L1+1,L2+1,.,LN),., A(U1,L2+1,.,LN),., A(L1,L2,.,LN+1) A(L1+1,L2,.,LN+1),.., A(U1,L2,.,LN+1),., A(L1,L2,..,UN),, A(L1,U2,.,UN),., A(U1,U2,..,UN) TRIANGULAR ARRAYS Definitions A triangular array may be either upper-triangular or lower-triangular as shown in the figure, where all the elements below or above the diagonal are zero.

- 10 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

An array is also zero. x x 0 x 0 0 0 0 0 0 0 0

called strictly upper (or lower) triangular if the elements of the diagonal are x x x 0 0 0 x x x x 0 0 x x x x x x x x x x 0 x x x x x x x 0 x x x x x 0 0 x x x x 0 0 0 x x x 0 0 0 0 x x 0 0 0 0 0 x

Upper-triangular Lower-triangular In a lower-triangular array with N rows, the maximum number of nonzero elements in the Ith row is I. The total number of nonzero elements is no more than
N

I= N(N+1) I=1 2 This expression is also true for an upper-triangular array with N rows. Linearization One approach is to linearize the array and store only the elements of the nonzero triangular portion. The upper-triangular array T can be stored as one-dimensional array named S with subscript bounds 1 through N(N+1)/2, by rows as follows: Element T(1,1) is stored as element S(1), element T(1,2) is stored as element S(2), and so forth up to element T(1,N) which is stored as element S(N). Then element T(2,2) is stored as S(N+1), becos T(2,1) is zero. Element T(N,N) is stored as S(N(N+1)/2). Sharing Space Sometimes program uses more than one triangular array and required to be stored in the manner that conserves space. Assume that array A is upper-triangular and N-by-N, and that array B is lower-triangular and N-1 by N-1. Then A and B can be stored together in an N-by-N array C. Elements C(I,J) where IJ are elements of A, and elements C(I,J) where I>J are elements of B. In fact, element A(I,J) is stored as C(I,J) for IJ, and element B(I,J) is stored as C(I+1,J) for IJ.

- 11 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

If both the arrays are of dimensions N-by-N, then array C must be N by N+1. If both the arrays are upper-triangular, having dimension N-by-N, then one of the triangular array can be transposed to convert it into lower-triangular array. Then they can be stored together in array C with dimensions N-by-N+1. SPARSE ARRAYS Definition Another special type of array which arises commonly in applications is a sparse array. An array is called sparse if it has a relatively high density of zero elements. 0 0 0 0 0 0 2 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Linearization Multi-dimensional sparse array can be linearized through the previous techniques, but there wld be much wasted space. Two alternative representations are available that will store explicitly only the nonzero elements. Vector representation Each nonzero element in a two-dimensional sparse array can be represented as triple with the format row-subscript, column-subscript, and value. These triples can be ordered by increasing row-subscript major and column-subscript minor, and can be stored as a vector. The above sparse array is represented by vector V as: row column V(1) 1, 5, V(2) 1, 8, V(3) 2, 2, V(4) 3, 1, value 1 2 1 1

- 12 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

V(5) 5, 4, 4 V(6) 6, 8, 2 V(7) 8, 1, 2 V(8) 8, 2, 1 This storage mapping uses more space to represent any particular nonzero element but avoids representing the zero elements. If sparse-array is one-dimensional each nonzero element will be represented by a pair. In general each nonzero element of an N-dimensional array is represented by an entry with N + 1 value. Operations on the vector representation of the two-decimal sparse array must use the row and column information to determine where each value resides. Inconvenience with this form of representation is handling addition of nonzero element to the array or changing previously nonzero elements to have zero value. The problem is not in changing the elements value, but in maintaining the order of nonzero elements in the representation vector. If the above array is updated so that the element with subscripts (1,8) were to have value zero, then vector elements V(3) through V(8) shd be shifted to become elements V(2) through v(7).

- 13 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

FIND MAX FROM 10x1 ARRAY #include<iostream.h> void main() { int a[10], i, max; for(i=0;i<10;i++) { cin>>a[i]; } max=a[0]; for(i=0;i<10;i++) { if(a[i]>=max) max=a[i]; } cout<<"\nMaximum Number is : }

"<<max;

ADD TWO VECTOR TO THIRD #include<iostream.h> void main() { int a[10], b[10], c[10], i; cout<<"Enter values of first vector:\n"; for(i=0;i<10;i++) cin>>a[i]; cout<<"Enter values of second vector:\n"; for(i=0;i<10;i++) cin>>b[i]; cout<<"The addition is: \n"; for(i=0;i<10;i++) { c[i]=a[i]+b[i]; cout<<c[i]<<endl; } } Transpose Of An Array #include<iostream.h>

- 14 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

void main() { int a[3][3], i, j; cout<<"Enter Values:\n"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>a[i][j]; } } cout<<"Transpose:\n"; for(j=0;j<3;j++) { for(i=0;i<3;i++) { cout<<a[i][j]<<" "; } cout<<endl; } } CREATE IDENTITY MATRIX #include<iostream.h> void main() { int a[5][5], i, j; for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(i==j) a[i][j]=1; else a[i][j]=0; } } cout<<"Identity Matrix:\n"; for(i=0;i<5;i++)

- 15 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

DATA & FILE STRUCTURE

By Aadil Keshwani aadil.keshwani@gmail.com

for(j=0;j<5;j++) { cout<<a[i][j]<<" "; } cout<<endl;

}}

- 16 By www.sharebca.com Share BCA.Com A place to learn and share all about BCA

Potrebbero piacerti anche