Sie sind auf Seite 1von 44

1

2
3
4
Todays session will focus on 1D and 2D Arrays, and also stack data
structure implementation using Arrays
5
6
7
Definition:
An array (also called a subscripted variable) is a collection of similar elements stored
in adjacent memory locations referred by the same name.
Arrays are "a set of items which are randomly accessible by numeric index. " What
this means is that an array consists of a set of physically contiguous memory
locations. These memory locations can all be addressed with one name the name of
the array. Each location in the array is referred to by the subscript of the array. In C,
the syntax to declare an array is as follows.
data-type array-name [size];
Advantages of using the array:
Arrays are typically laid out in memory as a contiguous chunk of blocks, where each
block is of the same data type. Because the memory is laid out as a contiguous set of
memory chunks, arrays are very fast for accessing items by index. Arrays are a great
choice of data structure when the number of elements it contains is known ahead of
time.
8
Array declaration and subscripts:
Arrays must be explicitly declared as the compiler allocates space for them. Here, data-type
declares the type of an array indicates the type of elements it holds and size defines how many
elements the array will hold.
e.g. int aiArrayOfIntegers[10];
This statement declares an integer array named aiArrayOfIntegers, consisting of 10 elements. Each
of these 10 elements can only contain an integer value. The index of the first element of an array in C
is 0. That is, the ten elements of the array can be referenced as
aiArrayOfIntegers[0], aiArrayOfIntegers[1], aiArrayOfIntegers[2], ... , aiArrayOfIntegers[9].
These values may be accessed as shown below.
iVar1 = aiArrayOfIntegers[0];
Note that in the declaration statement, aiArrayOfIntegers[10]means that there are 10 elements in the
array; but in an assignment statement, aiArrayOfIntegers[10]refers to the 11th element of a 10-
element array. That is, if there are ten elements in an array, the subscript of the last element of the
array is 9, and not 10. This is a common programming mistake and results in some indeterminate
value being returned by aiArrayOfIntegers[10], which is very likely to cause program failure.
In the declaration statement, the subscript can be a defined constant; however, it cannot be a
variable.
The amount of storage required to hold an array is directly related to its type and size. For a single
dimensional array, the total size in bytes is computed as:
total-bytes = sizeof( data-type) * length of array.
9
Application areas of an array:
An array is an example of a static storage structure. It is used when a list of similar data
needs to be stored and the number of items is known. It is frequently used in various data
structure programs
Limitations of an array:
When growing structures are required ie. The storage requirements keep changing in an ad
hoc manner, then arrays cannot be used as the size of an array should be know during
declaration time. When too many insertion and deletion operations are needed, it becomes
very tedious in an array
Advantages of an array:
Random access is possible and hence easier to access the elements. It is the choice of
data structure when the number of items are fixed
10
11
12
13
Formula for array:
The index refers to the relative position of an element from the first element and hence the
subscript of the first element is 0. The address of the and the value for other subscripts are
calculated as follows:
the address for the value aiArrayOfIntegers[n]can be calculated as
Firstelement address(Base address) + n * sizeof(datatype of the array)
The value of aiArrayOfIntegers[n] can be calculated as
aiArrayOfIntegers[n]= *( Firstelement address(Base address) + n * sizeof(datatype of
the array) )
Consider the following example:
int aiNumber[4] = { 10,20,30,40};
Assume the starting address of an array as 2000
Array elements are referenced as follows :
To get the element at index 0
aiNumber[0] = * (Base address + 0 * sizeof(int))
*(2000 + 0)
*(2000)
10
aiNumber[1] = * (Base address + 1* sizeof(int))
*(2000 + 1*4)
*(2004)
20
In general to access the element at index I
aiNumber[i] = * (Base address + i * sizeof(int))
14
C has no boundary checking on arrays.
E.g.
int aiCount[10], iVar;
for (iVar=0;iVar<100;iVar++)
aiCount[iVar] = iVar;
This code will compile without error, but it is incorrect because the for loop will cause the
array count to be overrun.
15
16
17
18
Answers:
1.The C language does not support array bound checking, the onus lies entirely on the programmer
2.The subscript indicates the relative distance of an element from the first element
3.The address based on the analogy can be calculated as follows:
Address of an ArrayName[index] = ( Base address + index * sizeof(datatype))
Address of aiNum[3]=1000+3 * 4=1012
4. 32 bytes
5. scanf(%d,&aiNum[2]);
19
20
Example :
Column major order of an array of size 3X4
[0,0]
[1,0]
[2,0]
[0,1]
[1,1]
[2,1]
Whether an array is stored in row-major or column-major order depends on the
implementation chosen by the compiler or the assembly language programmer.
21
22
23
24
25
26
27
28
29
Assumption The code is written to meet the specifications given . There might be
multiple validations which can be incorporated in future . For example the item price
is assumed to be 100 in this example to simplify writing code
30
31
1) printf(" \t %d \t %d\n",aiEmployeeInfo[1][0],aiEmployeeInfo[1][1]);
2) 40 bytes
3) scanf(%d,&aiEmployeeInfo[0][1]);
32
Real Life applications of stack
1. Stack of newspapers Todays newspaper is added at the top and we remove
the top most newspaper
2. Parameter passing to functions uses a stack(dealt later in the course)
3. The evaluation of arithmetic expressions by a compiler uses a stack
33
34
35
36
tos indicates the index of the array
37
38
39
An application of array implementation of stack is the evaluation of postfix
and prefix expressions in a compiler. The arithmetic expressions given in
programs is the infix form, the evaluation takes place in the compiler using
the postfix or the prefix forms . This uses a stack structure for the
implementation.
Note: The program given is not modular as the concept of functions has not
been covered yet. In the SP module , the same program would be made
modular with the help of functions and this is one of the best programming
practice.
40
41
42
43
44

Das könnte Ihnen auch gefallen