Numeric Arrays Page 1 Numeric Arrays Chapter 4

  • Slides: 41
Download presentation
Numeric Arrays Page 1 Numeric Arrays Chapter 4 Data Structures in C for Non-Computer

Numeric Arrays Page 1 Numeric Arrays Chapter 4 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 2 What you MUST know before we start: (Remember: The topics

Numeric Arrays Page 2 What you MUST know before we start: (Remember: The topics in this course build on each other) • What integers and real numbers are • How integers and real numbers are stored in RAM • The basic concept of what an address is and how it is used • The use of C/C++ to manipulate integers and real numbers Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 3 Numeric Arrays • Array: (def) A Regular Order or Arrangement

Numeric Arrays Page 3 Numeric Arrays • Array: (def) A Regular Order or Arrangement • For our Purposes, we can define an array as a data structure containing a fixed number of contiguous storage elements all of the same type Assume we wished to store the squares of the numbers: Number: 0 1 2 3 4 5 6 7 8 9 Square: 0 1 4 9 16 25 36 49 64 81 We could store the numbers as scalar variables: int d 0 = 0, d 1 = 1, d 2 = 4, d 3 = 9, d 4 = 16, d 5 = 25, d 6 = 36, d 7 = 49, d 8 = 64, d 9 = 81; Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 4 The problem with this approach is: • Remembering the variables

Numeric Arrays Page 4 The problem with this approach is: • Remembering the variables names can become complex • Manipulating each of the variables is tedious For Example, suppose we were trying to find the value 64 in our list, but didn’t know where it was stored: if (d 0 == 64) printf(“at d 0’’); else if (d 1 == 64) printf(“at d 1’’); else if (d 2 == 64) printf(“at d 2’’); else if (d 3 == 64) printf(“at d 3’’); else if (d 4 == 64) printf(“at d 4’’); else if (d 5 == 64) printf(“at d 5’’); else if (d 6 == 64) printf(“at d 6’’); else if (d 7 == 64) printf(“at d 7’’); else if (d 8 == 64) printf(“at d 8’’); else if (d 9 == 64) printf(“at d 9’’); else printf(“Not found”); Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 5 A preferred alternative would be to store the squares of

Numeric Arrays Page 5 A preferred alternative would be to store the squares of the numbers as an integer array: int numvector[10]; What does this declaration do ? ? ? • Using the datatype int implies that we will require 2 bytes per element • the variable name numvector will be associated with the base address of the array • [10] indicates how many elements will be in our array • since we are creating an array of type int, we are requesting 2 * 10 = 20 CONTINGUOUS bytes of RAM Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 6 We could also initialize our array when we declare it:

Numeric Arrays Page 6 We could also initialize our array when we declare it: int numvector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; OR int numvector[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; Even though we do not explicitly indicate how many elements are in the array, we are still requesting 20 bytes of CONTIGUOUS storage (We are just asking the compiler to determine the number) Because we are specifying only one subscript or offset, offset we are declaring a vector (a one-dimensional array) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 7 How are arrays stored in RAM ? ? ? •

Numeric Arrays Page 7 How are arrays stored in RAM ? ? ? • That depends of the type of data we wish to store • In our case, we are storing an integer array containing 10 elements, so we are requesting 2 * 10 = 20 bytes of CONTIGUOUS storage • If (at run-time) we find there are 20 contiguous bytes of storage available starting at location 9050: 9050 ([0]) 9051 ([0]) 9052 ([1]) 9053 ([1]) 9054 ([2]) 9055 ([2]) 00000000 00000001 00000100 9056 ([3]) 9057 ([3]) 9058 ([4]) 9059 ([4]) 9060 ([5]) 9061 ([5]) 00001001 0000 0001000 0000 00011001 9062 ([6]) 9063 ([6]) 9064 ([7]) 9065 ([7]) 9066 ([8]) 9067 ([8]) 0000 00100100 0000 00110001 0000 01000000 9068 ([9]) 9069 ([9]) 9070 9071 9072 9073 0000 01010001 01110001 11001000 01101001 00010111 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 8 At a more abstract, and understandable, level, this might appear

Numeric Arrays Page 8 At a more abstract, and understandable, level, this might appear as: 9050 & 9051 numvector[0] 9052 & 9053 numvector[1] 9054 & 9055 numvector[2] 0 1 4 Notice: • The first element on the list (element number 1) has the subscript (offset) 0 (zero) • The last element on the list (element number 10) has the subscript (offset) 9 (nine). Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 9 Why is the first element subscripted as [0] AND why

Numeric Arrays Page 9 Why is the first element subscripted as [0] AND why is it also referred to as the offset ? ? ? In fact, the subscript (at least in C) IS the offset from the base address of the array REMEMBER: REMEMBER When we first initialized our array, we stated: • The variable name numvector will be associated with the base address of the array • Given a base address, we can use the offset (or subscript) to determine the addresses of any individual element in the array How does this work ? ? ? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 10 Remember: Remember Our basic directive: “Give me an address and

Numeric Arrays Page 10 Remember: Remember Our basic directive: “Give me an address and tell me what type of data is stored there, and I will tell you the value of that data type” Arrays allow a convenient way of determining an address Given an integer array of 10 elements, we can calculate individual array addresses using the formula: Element address = base address of the array + (offset number * 2) (Since 2 -bytes are needed for an integer) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 11 Given our the Array (as stored in RAM): 9050 &

Numeric Arrays Page 11 Given our the Array (as stored in RAM): 9050 & 9051 numvector[0] 9052 & 9053 numvector[1] 9054 & 9055 numvector[2] 0 1 4 9056 & 9057 numvector[3] 9058 & 9059 numvector[4] 9060 & 9061 numvector[5] 9 16 25 9062 & 9063 numvector[6] 9064 & 9065 numvector[7] 9066 & 9067 numvector[8] 36 49 64 9068 & 9069 numvector[9] 9070 & 9071 9072 & 9073 81 ---- We can calculate element addresses as: Offset Address: 0 1 2 3 4 9050 + (0 * 2) = 9050 + (1 * 2) = 9052 9050 + (2 * 2) = 9054 9050 + (3 * 2) = 9056 9050 + (0 * 2) = 9058 Data Structures in C for Non-Computer Science Majors 5 6 7 8 9 9050 + (5 * 2) = 9060 9050 + (6 * 2) = 9062 9050 + (7 * 2) = 9064 9050 + (8 * 2) = 9066 9050 + (9 * 2) = 9068 Kirs and Pflughoeft

Numeric Arrays Page 12 Suppose we made the declaration: float fvector[10]; And the base

Numeric Arrays Page 12 Suppose we made the declaration: float fvector[10]; And the base address of the array was 12348 In RAM this might appear as: 12348 to 12351 fvector[0] 12352 to 12355 fvector[1] 12356 to 12359 fvector[2] ----- 12360 to 12363 fvector[3] 12364 to 12367 fvector[4] 12368 to 12371 fvector[5] ----- 12372 to 12375 fvector[6] 12376 to 12379 fvector[7] 12380 to 12383 fvector[8] ----- 12372 to 12375 fvector[6] ----- Offset 0 1 2 3 4 Address: We could calculate element addresses as: Offset Address: 12348 + (0 * 4) = 12348 + (1 * 4) = 12352 12348 + (2 * 4) = 12356 12348 + (3 * 4) = 12360 12348 + (4 * 4) = 12364 Data Structures in C for Non-Computer Science Majors 5 6 7 8 9 12348 + (5 * 4) = 12368 12348 + (6 * 4) = 12372 12348 + (7 * 4) = 12376 12348 + (8 * 4) = 12380 12348 + (9 * 4) = 12384 Kirs and Pflughoeft

Numeric Arrays Consider the following C Code: Page 13 #include <stdio. h> void main()

Numeric Arrays Consider the following C Code: Page 13 #include <stdio. h> void main() { int vector[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; int index; printf("the base address of vector is %lun", &vector); for (index = 0; index < 10; index++) printf("vector[%d] = %d; stored at address %lun", index, vector[index], &vector[index]); } The (slightly modified) output would be: the base address of vector is 41553194 vector[0] = 0; stored at address 41553194 vector[1] = 1; stored at address 41553196 vector[2] = 4; stored at address 41553198 vector[3] = 9; stored at address 41553200 vector[4] = 16; stored at address 41553202 vector[5] = 25; stored at address 41553204 vector[6] = 36; stored at address 41553206 vector[7] = 49; stored at address 41553208 vector[8] = 64; stored at address 41553210 vector[9] = 81; stored at address 41553212 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 14 What if we entered illegal subscripts? For example, for the

Numeric Arrays Page 14 What if we entered illegal subscripts? For example, for the previous code, what if we entered the for loop parameters: for (index = 8; index < 12; index++) printf ("vector[%d] = %d; stored at address %lun", index, vector[index], &vector[index]); (This is illegal because index should not take on any values larger than 9) The (slightly modified) output might appear as: vector[8] = 64; stored at address 41553210 vector[9] = 81; stored at address 41553212 vector[10] = -13107; stored at address 41553214 vector[11] = -15863; stored at address 41553216 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 15 How can there be a vector[10]? Or vector[11]? ? •

Numeric Arrays Page 15 How can there be a vector[10]? Or vector[11]? ? • vector[10] is nothing more than address 10 * 2 = 20 bytes offset from the base (41553194 + 20 = 41553114) • vector[11] is nothing more than address 11 * 2 = 22 bytes offset from the base (41553194 + 22 = 41553116) The command we issued was: index++ Which increments the contents of index (at the time, 9) by 1 &vector[index] Therefore, the location: Would yield the addresses given above But why is the value stored at, for example, vector[10], -13107 ? ? ? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 16 If we were to go to address: We might find

Numeric Arrays Page 16 If we were to go to address: We might find 41553114 4155315 11001101 Neg. &vector[10] 01100110010 + 1 01100110011 which equates to One’s Compliment Two’s Compliment = -(213 + 212 + 29 + 28 + 25 + 24 + 21 + 20) = -(8192 + 4096 + 512 + 256 + 32 + 16 + 2 + 1) = -13, 107 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 17 Multi-dimensional Arrays Suppose I wished to store the Squares AND

Numeric Arrays Page 17 Multi-dimensional Arrays Suppose I wished to store the Squares AND the cubes of the digits from 0 through 9 We could store them as two vectors: int vect 1[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}, vect 2[10] = {0, 1, 8, 27, 64, 125, 216, 343, 512, 729}; Assuming that we did, AND we found that the base addresses of the two arrays were: vect 1 = &vect 1[0] = 12350 AND vect 2 = &vect 2[0] = 12424 the relevant section of RAM might appear as: Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 18 12350 & 12351 12352 & 12353 12354 & 12355 12356

Numeric Arrays Page 18 12350 & 12351 12352 & 12353 12354 & 12355 12356 & 12357 12358 & 12359 0 1 4 9 16 12360 & 12361 12362 & 12363 12364 & 12365 12366 & 12367 12368 & 12369 25 36 49 64 81 . . . . 12424 & 12425 12426 & 12427 12428 & 12429 12430 & 12431 12432 & 12433 0 1 8 27 64 12434 & 12435 12436 & 12437 12438 & 12439 12440 & 12441 12442 & 12443 125 216 343 512 729 Where: vect 1 = &vect 1[0] = 12350 AND vect 2 = &vect 2[0] = 12424 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 19 OR we could store the values as ONE table: Table

Numeric Arrays Page 19 OR we could store the values as ONE table: Table sqrscubes 0 0 RowCol 0 1 2 0 0 1 1 4 8 3 4 9 27 16 64 5 6 25 125 36 217 7 49 343 8 64 512 9 81 729 Data Structures in C for Non-Computer Science Majors Where: The Row and Column numbers are the table offsets Kirs and Pflughoeft

Numeric Arrays Page 20 What difference does it make if we use one or

Numeric Arrays Page 20 What difference does it make if we use one or two arrays ? ? ? • With two vectors we need 2 blocks of RAM each containing 20 contiguous bytes of RAM • With one array (matrix) we need 1 block of RAM which contains 40 contiguous bytes • With two vectors we need two variables (two base addresses) • With one matrix we need one variable (one base address) • The ORDER in which the data is stored in RAM differs How is RAM storage different ? ? ? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 21 Consider the following C code: void main() { int matrix[10][2],

Numeric Arrays Page 21 Consider the following C code: void main() { int matrix[10][2], index; for (index = 0; index < 10; index++) { matrix[index][0] = index * index; matrix[index][1] = index * index; } } • Using the datatype int implies that we will require 2 bytes per element • since we are creating a 2 -dimensional array of type int, we are requesting 2 * 10 = 40 CONTINGUOUS bytes of RAM • the variable name matrix will be associated with the base address of the array Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 22 If the base address for the array matrix was: 63128

Numeric Arrays Page 22 If the base address for the array matrix was: 63128 The relevant portion of RAM might appear as: 63128 & 63129 63130 & 63131 63132 & 63133 63134 & 63135 63136 & 63137 0 0 1 1 4 63138 & 63139 63140 & 63141 63142 & 63143 63144 & 63145 63146 & 63147 8 9 27 16 64 63148 & 63149 63150 & 63151 63152 & 63153 63154 & 63155 63156 & 63157 25 125 36 217 49 63158 & 53159 63160 & 63161 63162 & 63163 63164 & 63165 63166 & 63167 343 64 512 81 729 NOTICE that the data is stored BY ROWS or by row offset Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 23 Looking at RAM in terms of offsets, we find: 63128

Numeric Arrays Page 23 Looking at RAM in terms of offsets, we find: 63128 & 63129 63130 & 63131 63132 & 63133 63134 & 63135 63136 & 63137 matrix[0][0] matrix[1][0] matrix[1][1] matrix[2][0] 63138 & 63139 63140 & 63141 63142 & 63143 63144 & 63145 63146 & 63147 matrix[2][1] matrix[3][0] matrix[3][1] matrix[4][0] matrix[4][1] 63148 & 63149 63150 & 63151 63152 & 63153 63154 & 63155 63156 & 63157 matrix[5][0] matrix[5][1] matrix[6][0] matrix[6][1] matrix[7][0] 63158 & 53159 63160 & 63161 63162 & 63163 63164 & 63165 63166 & 63167 matrix[7][1] matrix[8][0] matrix[8][1] matrix[9][0] matrix[9][1] Which corresponds to the manner in which we originally laid out our table. How do we calculate these addresses ? ? ? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 24 Since we know that each row contains 2 elements (4

Numeric Arrays Page 24 Since we know that each row contains 2 elements (4 bytes), and each element requires 2 -bytes of storage, we must (slightly) modify our previous formula: Element address = base address of the array + (row offset * bytes per row) + (column offset * bytes per element) Since we are dealing with an integer array which has the base address 63128 the formula is: Element address = 63128 + (row offset * 4) + (column offset * 2) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Row/Col [0][0] [0][1] [1][0] [1][1] [2][0] [2][1] [3][0] [3][1] [4][0] [4][1] Page

Numeric Arrays Row/Col [0][0] [0][1] [1][0] [1][1] [2][0] [2][1] [3][0] [3][1] [4][0] [4][1] Page 25 Address Row/Col 63128 + 0*4 + 0*2 = 63128 + 0*4 + 1*2 = 63130 63128 + 1*4 + 0*2 = 63132 63128 + 1*4 + 1*2 = 63134 63128 + 2*4 + 0*2 = 63136 63128 + 2*4 + 1*2 = 63138 63128 + 3*4 + 0*2 = 63140 63128 + 3*4 + 1*2 = 63142 63128 + 4*4 + 0*2 = 63144 63128 + 4*4 + 1*2 = 63146 [5][0] [5][1] [6][0] [6][1] [7][0] [7][1] [8][0] [8][1] [9][0] [9][1] Address 63128 + 5*4 + 0*2 = 63148 63128 + 5*4 + 1*2 = 63150 63128 + 6*4 + 0*2 = 63152 63128 + 6*4 + 1*2 = 63154 63128 + 7*4 + 0*2 = 63156 63128 + 7*4 + 1*2 = 63158 63128 + 8*4 + 0*2 = 63160 63128 + 8*4 + 1*2 = 63162 63128 + 9*4 + 0*2 = 63164 63128 + 9*4 + 1*2 = 63166 63128 & 63129 63130 & 63131 63132 & 63133 63134 & 63135 63136 & 63137 63138 & 63139 63140 & 63141 63142 & 63143 63144 & 63145 63146 & 63147 63148 & 63149 63150 & 63151 63152 & 63153 63154 & 63155 63156 & 63157 63158 & 63159 63160 & 63161 63162 & 63163 63164 & 63165 63166 & 63167 matrix[0][0] matrix[0][1] matrix[1][0] matrix[1][1] matrix[2][0] matrix[2][1] matrix[3][0] matrix[3][1] matrix[4][0] matrix[4][1] matrix[5][0] matrix[5][1] matrix[6][0] matrix[6][1] matrix[7][0] matrix[7][1] matrix[8][0] matrix[8][1] matrix[9][0] matrix[9][1] Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 26 The formula works regardless of how many dimensions there are.

Numeric Arrays Page 26 The formula works regardless of how many dimensions there are. Consider the C declaration: float multiarray[5][4][3][2]; We are requesting a total of: Contiguous bytes of RAM 4 * (5 * 4 * 3 * 2) = 4 * 120 = 480 Notice that: • Each change in the last (4 th) offset requires: • Each change in the 3 rd offset requires: • Each change in the 2 nd offset requires: • Each change in the 1 st offset requires: 4 -bytes 8 -bytes 24 -bytes 96 -bytes Which makes sense since the first offset can change 5 times and we know we require a total of 480 (= 5 * 96) bytes Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 27 The General formula, therefore, would be: Element address = base

Numeric Arrays Page 27 The General formula, therefore, would be: Element address = base address of the array + (first offset * 96) + (second offset * 24) + (third offset * 8) + (fourth offset * 4) If we found that the base address of our variable multiarray was: 21578 We could calculate the following (sample) addresses as: Array Element Address multiarray[0][0][2][1] multiarray[1][2][0][0] multiarray[1][3][2][1] multiarray[2][1][1][0] multiarray[4][3][2][1] 21578 + 0*96 + 0*24 + 2*8 + 1*4 = 21578 + 1*96 + 2*24 + 0*8 + 0*4 = 21578 + 1*96 + 3*24 + 2*8 + 1*4 = 21578 + 2*96 + 1*24 + 1*8 + 0*4 = 21578 + 4*96 + 3*24 + 2*8 + 1*4 = Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft 21598 21722 21915 21802 22054

Numeric Arrays Page 28 Searching an Array Assume that we wished to determine if

Numeric Arrays Page 28 Searching an Array Assume that we wished to determine if the number 25 was in our original list (of squared values): #include <stdio. h> void main() main { int vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; int index; for (index = 0; index < 10 && vector[index] != 25; index++); if (index > 9) printf(“The value was not foundn”); else printf(“The value %d was found at offset %dn”, vector[index], index); } Let’s Consider this program line by line Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 29 int vector[10] = {0, 1, 4, 9, 16, 25, 36,

Numeric Arrays Page 29 int vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; Reserve (& initialize) 2 * 10 = 20 bytes at base address vector int index; Reserve 2 bytes at base address index for (index = 0; Set (once) the contents of location index to zero (0) 15234 & 15235 0 12350 & 12351 12352 & 12353 12354 & 12355 12356 & 12357 12358 & 12359 0 1 4 9 16 12360 & 12361 12362 & 12363 12364 & 12365 12366 & 12367 12368 & 12369 25 36 49 64 81 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 30 int vector[10] = {0, 1, 4, 9, 16, 25, 36,

Numeric Arrays Page 30 int vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; Reserve (& initialize) 2 * 10 = 20 bytes at base address vector int index; Reserve 2 bytes at base address index for (index = 0; index < 10 && vector[index] != 25; True 15234 & 15235 0 Therefore, execute the command 12350 & 12351 12352 & 12353 12354 & 12355 12356 & 12357 12358 & 12359 0 1 4 9 16 12360 & 12361 12362 & 12363 12364 & 12365 12366 & 12367 12368 & 12369 25 36 49 64 81 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 31 int vector[10] = {0, 1, 4, 9, 16, 25, 36,

Numeric Arrays Page 31 int vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; Reserve (& initialize) 2 * 10 = 20 bytes at base address vector int index; Reserve 2 bytes at base address index for (index = 0; index < 10 && vector[index] != 25; index++); What Command ? ? ? • There is none needed in this case • We know that the element we are looking for has not been found (vector[index] != 25) • We also know that the list hasn’t been exhausted (index < 10) • We need only move to the next element Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 32 int vector[10] = {0, 1, 4, 9, 16, 25, 36,

Numeric Arrays Page 32 int vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; Reserve (& initialize) 2 * 10 = 20 bytes at base address vector int index; Reserve 2 bytes at base address index for (index = 0; index < 10 && vector[index] != 25; index++); Throughout the loop, the logic would be: Pass No. 1 2 3 4 5 6 (contents of) index 0 1 2 3 4 5 index <= 10 ? Yes Yes Yes Vector[index] != 25 ? (= 0) Yes (= 1) Yes (= 4) Yes (= 9) Yes (= 16) Yes (= 25) NO We are out of the loop Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 33 int vector[10] = {0, 1, 4, 9, 16, 25, 36,

Numeric Arrays Page 33 int vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; Reserve (& initialize) 2 * 10 = 20 bytes at base address vector int index; Reserve 2 bytes at base address index for (index = 0; index < 10 && vector[index] != 25; index++); Once out of the loop we need to print our findings: if (index > 9) False º º º º º else printf(“The value %d was found at offset %dn”, vector[index], index); The value 25 was found at offset 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 34 Pointers and Arrays: Assume that we wished to determine if

Numeric Arrays Page 34 Pointers and Arrays: Assume that we wished to determine if the number 25 was in our original list (of squared values): #include <stdio. h> void main() main { int vector[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; int * index; index = vector; while ((index <= &vector[9]) && (* index != 25)) index++; if (index > &vector[9]) printf(“The value was not foundn”); else printf(“The value %d was found at address %pn”, *index, index); } Let’s Consider this program line by line Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 35 int vector[] = {0, 1, 4, 9, 16, 25, 36,

Numeric Arrays Page 35 int vector[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; Reserve (& initialize) 2 * 10 = 20 bytes at base address vector (assume that vector has the base address 8900) int *index; Reserve 4 -bytes of storage at location index If we go to location index, we would find a signed integer value (assume that index has the base address 8932) index = vector; Store the address of vector (i. e. , 8900) at location index while ((index <= &vector[9]) && (*index != 25)) Continue processing as long as: • The contents of index (presently 8900) is less than or equal to &vector (= 8900 + 9*2 = 8918) AND • The contents of the address stored at location index (at location 8900 we will presently find the integer 0) is not equal to the integer 25 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 36 Inside the loop, there is only one statement: index++; index

Numeric Arrays Page 36 Inside the loop, there is only one statement: index++; index • Increment the contents of location index • since location index contains an address which points to an integer, incrementing means increasing the value by 2 Throughout the loop, the logic would be: = 8918 index <= (contents of) Pass No. index &vector[9] ? *index != 25 ? 1 2 3 4 5 6 8900 8902 8904 8906 8908 8910 Yes Yes Yes 0 1 4 9 16 25 Yes Yes Yes NO We are out of the loop Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 37 When we come out of the loop, there are two

Numeric Arrays Page 37 When we come out of the loop, there are two possibilities: EITHER: index > &vector[9] Meaning that the contents of location index (which contains an an address) are greater than the address of the last legal array address (i. e. , 8918) In which case we print out: The value was not found OR We print out the value: The value 25 was found at address 8910 (Assuming we were looking for the value 25) How could the contents of location index ever be greater than the array address 8918 ? ? ? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 38 If we were looking for the value 108 (not on

Numeric Arrays Page 38 If we were looking for the value 108 (not on the list): Pass No. 1 2 3 4 5 6 7 8 9 10 11 (contents of) index 8900 8902 8904 8906 8908 8910 8912 8914 8916 8918 8920 = 8918 index <= &vector[9] ? *index != 108 ? Yes 0 Yes 1 Yes 4 Yes 9 Yes Yes 16 Yes 25 Yes 36 Yes 49 Yes 64 Yes 81 Yes NO Unknown Probably Not And we are out of the loop Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 39 Array Declarations Automatic Arrays • • Defined INSIDE a function

Numeric Arrays Page 39 Array Declarations Automatic Arrays • • Defined INSIDE a function Exists ONLY for the duration of the function NOT initialized When done, memory allocation freed int main() main { intarray[100], index; External Arrays • • • Known to ALL functions Do NOT Expire when a particular functions ends INITIALIZED when declared intarray[100]; int main() main { int index; Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 40 Static Arrays • • • Like Automatic Arrays, LOCAL to

Numeric Arrays Page 40 Static Arrays • • • Like Automatic Arrays, LOCAL to the function Like External arrays, RETAIN VALUES between calls INITIALIZED at declaration int main() main { static intarray[100]; int index; Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Data Structures in C for Non-Computer Science Majors Page 41 Kirs and

Numeric Arrays Data Structures in C for Non-Computer Science Majors Page 41 Kirs and Pflughoeft