Numeric Arrays Page 1 Pointers and Arrays Assume

  • Slides: 6
Download presentation
Numeric Arrays Page 1 Pointers and Arrays: Assume that we wished to determine if

Numeric Arrays Page 1 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 2 int vector[] = {0, 1, 4, 9, 16, 25, 36,

Numeric Arrays Page 2 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 3 Inside the loop, there is only one statement: index++; index

Numeric Arrays Page 3 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 4 When we come out of the loop, there are two

Numeric Arrays Page 4 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 5 If we were looking for the value 108 (not on

Numeric Arrays Page 5 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

Page 6 This Concludes The Slides for this Section Choose an Option: Repeat Slides

Page 6 This Concludes The Slides for this Section Choose an Option: Repeat Slides for this Section Go To Next Set of Slides For this Chapter Go To Slide Index For Chapter 4 Go To Slide Index For Chapter 5 Go To Slide Index For Textbook Go To Home Page Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft