You are on page 1of 4

Chapter 1 Introduction to Data Structure Arrays: Arrays are most frequently used in programming.

. An array is a finite ordered set of homogenous elements. By finite we mean that there is a specific number of elements in the array. This number may be large or small, but it must exist. By ordered we mean that the elements of the array are arranged so that there is a zero, first, second, third, and so forth. By Homogenous we mean that all the elements in the array must be of the same type. For example an array may contain either all integer elements or all character elements but it cannot be combination of both. Each element of an array is referenced by a subscripted variable or value, called subscript or index enclosed in parenthesis. If an element of an array is referenced by single subscript, then the array is known as one dimensional array, if it is referred by two subscripts then it is known as two dimensional arrays and so on. Analogously the arrays whose elements are referenced by two or more subscripts are called multi dimensional arrays. The two basic operations that access an array are extraction and storing. The extraction operation is a function that accepts an array a, and an index, I, and returns an element of the array. In C, the result of this operation is denoted by the expression a[i]. The storing operation accepts an array a, an index, i, and an element, x. In C this operation is denoted by assignment statement a[I] =x. The smallest element of an arrays index is called its lower bound and in C is always 0, and the highest element is called upper bound. If lower is the lower bound of an array and upper the upper bound, the number of elements in the array, called its range, and is given upper-lower + 1. An important feature of a C array is that neither the upper bound nor the lower bound may be changed during a programs execution. The lower bound is always a 0 and upper bound will be defined when program is written. One Dimensional array One Dimensional array is used when it is necessary to keep a large number of items in memory and reference all the items in a uniform manner. A one dimensional array can be implemented easily. The C declaration Int b[100]; Reserves 100 successive memory locations, each large enough to contain a single integer. Of course, 100 separate variables could

have been used to hold the integers. The advantage of an array, however, is that it allows the programmer to declare only a single identifier and yet obtain a large amount of space. The address of the first of these locations is called the base address of the array b and is denoted by base (b). Suppose that a size of each individual element of the array b is esize. Then a reference to the element b[0] is to the element at location base(b), a reference to b[1] is to the element at base(b)+esize, a reference to b[2] is to the element base(b)+2*esize. In general, a reference to b[i] is to the element at location base (b) +i*esize. Thus it is possible to reference any element in the array, given its index. In fact, in the C language an array variable is implemented as a pointer variable. The type of the variable b in the above declaration is pointer to an integer or int*. An asterisk does not appear in declaration because the brackets automatically imply that the variable is a pointer. Every parameter of a C function must be declared within the function; however the range of a one dimensional array parameter is only specified in the main program. This is because in C new storage is not allocated for an array parameter. Rather, the parameter refers to the original array that was allocated in the calling program. Since an array variable in C is a pointer, array parameters are passed by reference rather than by value. That is unlike simple variables that are passed by value; an arrays contents are not copied when it is passed as parameters in C. Instead the base address of the array is passed. A string is defined in Ca s an array of characters. Each string is terminated by the NULL character, which indicates the end of the string. A string constant is denoted by a set of characters included in double quote marks. The Null character is automatically appended at the end of characters in a string constant when they are stored. Within an program Null character is denoted by an escape sequence \0. A string constant represents an array whose lower bound is 0 and whose upper bound is the number of characters in the string. For example the string Hello There is an array of 12 characters (The blank and \0 each counts as a character). Two Dimensional arrays :

The component type of an array can be another array. If we define an array as Int a[3][5] This defines a new array containing three elements. Each of these elements is itself an array containing five integers. An element of this array is accessed by specifying two indices: a row number and a column number. The number of rows and columns are known as range of dimension. In the array a range of first dimension is 3 and of second is 5. Thus A has 3 rows and 5 columns. Multi Dimensional arrays: C also allows arrays with more than two dimensions. For example, a three dimensional array may be declared by Int b[3][2][4] An element of this array is specified by three subscripts. The first subscript specifies plane number, the second row number and the third one column number. Such an array is useful when a value is determined by three inputs. For example an array of temperature might be indexed by latitude, longitude and altitude. C allows arbitrary number of dimensions. Structures in C A structure is a group of items in which each item is identified by its own identifier, each of which is known as a member of the structure. Structure is also known as record. For example: Struct{ char first[10]; char midint; char last[20]; }sname,ename; This declaration creates two structure variables, sname and ename, each of which contains three members first,midint,last. Alternatively we can assign a tag to the structure and then declare the variables by means of the tag. For ex, consider the following declaration that accomplishes the same thing as the declaration just given: Struct nametype{ Char first[10];

Char midint; Char last[20]; }; Struct nametype sname,ename; This definition creates a structure tag nametype containing three members, first,midint and last. Once a structure tag has been defined variables sname and ename may be decalred. Once a variable has been declared as a structure, each member within that variable may be accessed by specifying the variable name and the items member identifier separated by a period. Ename.midint=M; Sname.last[i]=ename.last[i]; An alternative to using a structure tag is to use the typedef definition in C. For example Typedef struct{ Char first[10]; Char midint; Char last[20]; }nametype; Says that the identifier Nametype is synonymous with the preceding structure specifier whenever NAMETYPE occurs. We can then declare Nametype sname,ename;

You might also like