290 likes | 418 Views
Administrative details. Lab # 7 and #8 should be turned in by now Project # 2 is due this Thursday We are starting lab #9 this week, due next Thursday Homework will be due next Thursday, problems 6.11 and 6.15 in Deitel. Topic 9 – Introduction To Arrays. Introduction to Data Structures.
E N D
Administrative details • Lab # 7 and #8 should be turned in by now • Project # 2 is due this Thursday • We are starting lab #9 this week, due next Thursday • Homework will be due next Thursday, problems 6.11 and 6.15 in Deitel CISC105 – Topic 9
Introduction to Data Structures • Thus far, we have seen “simple” data types. These refer 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. CISC105 – Topic 9
Variables in Memory 1004 • 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; 1006 1008 (y) 1010 1012 1014 (x) 1016 (z) 1018 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. CISC105 – Topic 9
Array Structure • Each element in the array is referenced using two components, the array name and an index. • The first is simply 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). 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 doubles 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. CISC105 – Topic 9
(x[0]) (x[1]) (x[2]) (x[3]) Array Structure in Memory 1004 • 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). 1006 1008 1010 1012 1014 1016 1018 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]; CISC105 – Topic 9
Declaring Arrays • An array can be made of any simple data type. • An array of six doubles, named y: • An array of twelve characters, named p: • An array of nine floats, named floaters: double y[6]; char p[12]; float floaters[9]; 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] 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. CISC105 – Topic 9
Manipulating Array Elements • These are all valid statements (assuming x is an array of floats): • 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]; 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; CISC105 – Topic 9
Separate the initialization list with commas. Open the initialization list with a “{“ brace. Close the initialization list with a “}“ brace. Array Size Provide one initialization value for each array element. Initializing Arrays • We can also initialize arrays when we declare them: int array[6] = { 23, 45, 220, -23, 22, 0 }; 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 }; 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. 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. 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]); 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 17th element. 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. CISC105 – Topic 9
(x[0]) (x[1]) (x[2]) (x[3]) Array Index Ranges: Errors 1004 • 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! 1006 1008 1010 1012 1014 1016 1018 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. 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 have been allocated for some other purpose (such as storing another variable). Thus, attempting to read (or change) a value is this manner is VERY dangerous. CISC105 – Topic 9
(x[0]) (x[1]) (x[2]) (x[3]) Array Index Ranges: ErrorsAn Example int z; int x[4]; 1004 • 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! 1006 1008 1010 1012 1014 1016 (z) 1018 CISC105 – Topic 9
(x[0]) (x[1]) (x[2]) (x[3]) Array Index Ranges: ErrorsAn Example 1004 • 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. 1006 1008 1010 1012 1014 1016 (z) 1018 CISC105 – Topic 9
Array Index Ranges: Errors • Thus, it is important to keep in mind that 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! CISC105 – Topic 9
Sequential Array Access • Very often, a programmer desires to access each element of an array in sequence. • This is common as arrays 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. CISC105 – Topic 9
0 (count) 2 (x[0]) 4 (x[1]) 6 (x[2]) 8 (x[3]) 0 (sum) Sequential Array Access 1004 4 1 3 2 • For example, suppose the array x holds four integers and we wish to put the sum of all four elements in the variable sum. 1006 1008 1010 int count=0, sum = 0; int x[4] = { 2, 4, 6, 8 }; for ( count = 0; count <= 3; count++ ) { sum += x[count]; } 1012 1014 1016 12 20 2 6 1018 CISC105 – Topic 9