Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.

Similar presentations


Presentation on theme: "Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single."— Presentation transcript:

1 Topic 9 – Introduction To Arrays

2 CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single memory cell which holds one variable in memory. In order to solve some programming problems, it is often useful to store a set of data (more than one variable) in a specific form, or structure, in memory.

3 CISC105 – Topic 9 We have seen that when a variable is declared, the operating system finds some location in memory to store it. The location one variable is stored in has no relation to the location other variables are stored in. int x; int y; int z; Variables in Memory 1004 1006 1008 1010 1012 1014 1016 1018 (y) (x) (z)

4 CISC105 – Topic 9 The Array The simplest data structure is that of an array. An array refers to a collection of two or more contiguous (adjacent) memory cells that hold variables of the same data type. Each variable in the array is referred to as an array element.

5 CISC105 – Topic 9 Array Structure Each element in the array is referenced using two components, the array name and an index. The first is simple the name of the array. The second is the array index. This is simply an integer that indicates which element you are referencing. For example, the first element has index 0, the second has index 1, the third has index 2, and so forth. Notice that the array elements are numbered starting with zero (0) and not one (1).

6 CISC105 – Topic 9 Array Structure in Memory When an array is declared, the operating system finds some location in memory to store it. Here, the operating system does not simply need to find one memory cell, but one memory cell for each element in the array. Therefore, if an array of 40 double s were to be declared, the OS would allocate a section of memory that is big enough to store 40 double variables, one after the other.

7 CISC105 – Topic 9 Array Structure in Memory For example, if x was declared to be an array of four integers, a section of memory consisting of four memory cells, one to hold each of the array elements, is allocated. Note that the starting memory address is determined by the operating system (just like each simple variable). 1004 1006 1008 1010 1012 1014 1016 1018 (x[0]) (x[1]) (x[2]) (x[3])

8 CISC105 – Topic 9 Declaring Arrays An array is declared much like a simple variable. After the array name, a bracket “[” is found, then the size of the array (an integer value) and then a closing bracket “]”. Thus, an array of four integers, named x, would be declared as: int x[4];

9 CISC105 – Topic 9 Declaring Arrays An array can be made of any simple data type. An array of six double s, named y: An array of twelve characters, named p: An array of nine float s, named floaters: double y[6]; char p[12]; float floaters[9];

10 CISC105 – Topic 9 Referencing Array Elements Once an array has been declared, there must be a method of accessing each element in the array. This is done by using the array name, the bracket “[”, the index of the element being referenced, and then a closing bracket “]”. Thus, in order to access the first element in the array grades: grades[0]

11 CISC105 – Topic 9 Referencing Array Elements Note that the first element in an array has index 0. The second element has index 1, the third index has index 2, etc… Any statement that can be used to manipulate a simple variable can also be used to manipulate an array element, when used in this manner.

12 CISC105 – Topic 9 Manipulating Array Elements These are all valid statements (assuming x is an array of float s): printf(“%f”,x[0]); x[3] = 25.0; sum = x[0] + x[1]; sum += x[2]; x[3] += 1.0; x[2] = x[0] + x[1];

13 CISC105 – Topic 9 Initializing Arrays With simple variables, we can initialize them (set an initial value) when we declare them: int x = 27; float y = 29.4; char grade = ‘C’; double num = 2.1;

14 CISC105 – Topic 9 Initializing Arrays We can also initialize arrays when we declare them: int array[6] = { 23, 45, 220, -23, 22, 0 }; Array Size Open the initialization list with a “{“ brace. Provide one initialization value for each array element. Separate the initialization list with commas. Close the initialization list with a “}“ brace.

15 CISC105 – Topic 9 Initializing Arrays This sets array[0] to 23, array[1] to 45, array[2] to 220, array[3] to –23, array[4] to 22, and array[5] to 0. int array[6] = { 23, 45, 220, -23, 22, 0 };

16 CISC105 – Topic 9 Array Indices: A Closer Look As we have seen, array elements are accessed with two components, the name of the array and the index, which indicates the index of the element of the array we are accessing. This index can range from zero (0) through the number of elements minus one (1). For example, an array with 16 elements has indices ranging from 0 to 15.

17 CISC105 – Topic 9 Array Indices: A Closer Look When an array element is accessed, the index does not have to be a simple integer. Any expression that evaluates to an integer can be used as the array index. Thus, any expression that evaluates to an integer can be placed inside the brackets “[” “]” in a reference to an array element. Whatever is inside the brackets is first evaluated and then that integer is used as the index. Note that this MUST evaluate to an integer.

18 CISC105 – Topic 9 Array Indices: Examples What is the output of the following? float x[10] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; int i = 2; printf(“%f”,x[4]); printf(“%f”,x[i]); printf(“%f”,x[i] + 1); printf(“%f”,x[i+1]); printf(“%f”,x[(int)x[4]]); printf(“%f”,x[i++]); printf(“%f”,x[(4+2)/3+i+2]);

19 CISC105 – Topic 9 Array Index Ranges: Errors This index can range from zero (0) through the number of elements minus one (1). For example, an array with 16 elements has indices ranging from 0 to 15. The index into an array should not go above this range. For example, in an array with 16 elements, the reference array[16] is invalid, as this would attempt to reference the 17 th element.

20 CISC105 – Topic 9 Array Index Ranges: Errors If this is done, the resulting value is indeterminate (garbage). This is similar to what happens when a variable is used before it is initialized (set to something). It is equal to whatever happens to be in memory at the time, as the program has not yet put anything there.

21 CISC105 – Topic 9 Array Index Ranges: Errors In this example, of an integer array of four elements, attempting to access x[4] would access whatever is currently in memory at location 1016, which is indeterminate. Thus, don’t do this! 1004 1006 1008 1010 1012 1014 1016 1018 (x[0]) (x[1]) (x[2]) (x[3])

22 CISC105 – Topic 9 Array Index Ranges: Errors Overstepping the bounds of an array is actually much worse than using a variable before it is initialized. When you use a variable before it is initialized, this is like attempting to look into a box (the variable) before you put anything in there (the value). However, the variable has been allocated by the operating system; that memory cell has been reserved by the OS for that variable. No other data will be placed there.

23 CISC105 – Topic 9 Array Index Ranges: Errors When you overstep the bounds of an array, that memory cell you are referencing has NOT been allocated by the operating system for the array. It may been be allocated for some other purpose (such as storing another variable). Thus, attempting to read (or change) a value is this manner is VERY dangerous.

24 CISC105 – Topic 9 Array Index Ranges: Errors An Example In this example, z is first allocated and assigned a memory cell at 1016. The array x is then allocated at memory cell 1008 – 1014. If the bounds on the array x are overstepped, the variable z can be accessed. If you attempt to store a value in x[4], that will change z! 1004 1006 1008 1010 1012 1014 1016 1018 (x[0]) (x[1]) (x[2]) (x[3]) (z) int z; int x[4];

25 CISC105 – Topic 9 Array Index Ranges: Errors An Example Note that this example (where x[4] would modify z) is dependant on the operating system allocating z immediately following the array x. This will not always happen, as where the OS will allocate variable is not known when writing the program. Many times, the memory cell accessed when the array is overstepped will not be allocated, and will thus be filled with garbage. 1004 1006 1008 1010 1012 1014 1016 1018 (x[0]) (x[1]) (x[2]) (x[3]) (z)

26 CISC105 – Topic 9 Array Index Ranges: Errors Thus, it is important to keep in mind the C will NOT check to see if the index the program is attempting to access is actually a correct index (within the size of the array). Incorrect array indices will result in unpredictable and potentially hazardous results when the program runs. So…make sure to check your array indices!

27 CISC105 – Topic 9 Sequential Array Access Very often, a programmer desires to access each element of an array in sequence. This is common as array typically hold data that is related. This is commonly accomplished in C using a for loop statement, creating a counting loop which runs from zero to the size of the array minus one. The loop control variable (the counter) is used as the array index inside the loop body.

28 CISC105 – Topic 9 Sequential Array Access For example, suppose the array x holds four integers and we wish to put the sum of all four elements in the variable sum. 1004 1006 1008 1010 1012 1014 1016 1018 int count=0, sum = 0; int x = { 2, 4, 6, 8 }; for (count = 0; count <= 3; count++ ) { sum += x[count]; } (count) 0 (sum) 0 (x[0]) (x[1]) (x[2]) (x[3]) 8 6 4 2 1 26 2 12 3 20 4


Download ppt "Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single."

Similar presentations


Ads by Google