Major Array Pitfall Example Indexes range from 0 to (array_size-1) ◆Example: double temperature[24];//24 is array size /Declares array of 24 double values called temperature ◆They are indexed as: temperature[0],temperature[1].temperature[23] ◆Common mistake: temperature[24]5; Index 24 is "out of range"! No warning,possibly disastrous results Copyright006 Pearson Addison-Wesley.All rights reserved. 5-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-11 Major Array Pitfall Example ¨ Indexes range from 0 to (array_size – 1) ¨ Example: double temperature[24]; // 24 is array size // Declares array of 24 double values called temperature ¨ They are indexed as: temperature[0], temperature[1] . temperature[23] ¨ Common mistake: temperature[24] = 5; ¨ Index 24 is "out of range"! ¨ No warning, possibly disastrous results
Defined Constant as Array Size Always use defined/named constant for array size ◆Example: const int NUMBER_OF_STUDENTS 5; int score[NUMBER_OF_STUDENTS]; Improves readability Improves versatility Improves maintainability Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-12 Defined Constant as Array Size ¨ Always use defined/named constant for array size ¨ Example: const int NUMBER_OF_STUDENTS = 5; int score[NUMBER_OF_STUDENTS]; ¨ Improves readability ¨ Improves versatility ¨ Improves maintainability
Uses of Defined Constant Use everywhere size of array is needed In for-loop for traversal: for (idx 0;idx NUMBER_OF_STUDENTS;idx++) { ∥Manipulate array } In calculations involving size: lastIndex =(NUMBER OF STUDENTS-1); When passing array to functions(later) ◆If size changes→requires only ONE change in program! Copyright006 Pearson Addison-Wesley.All rights reserved. 5-13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-13 Uses of Defined Constant ¨ Use everywhere size of array is needed ¨ In for-loop for traversal: for (idx = 0; idx < NUMBER_OF_STUDENTS; idx++) { // Manipulate array } ¨ In calculations involving size: lastIndex = (NUMBER_OF_STUDENTS – 1); ¨ When passing array to functions (later) ¨ If size changes requires only ONE change in program!
Arrays in Memory Recall simple variables: Allocated memory in an "address" Array declarations allocate memory for entire array Sequentially-allocated Means addresses allocated "back-to-back" Allows indexing calculations Simple "addition"from array beginning (index 0) Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-14 Arrays in Memory ¨ Recall simple variables: ¨Allocated memory in an "address" ¨ Array declarations allocate memory for entire array ¨ Sequentially-allocated ¨Means addresses allocated "back-to-back" ¨Allows indexing calculations ¨Simple "addition" from array beginning (index 0)
An Array in Memory Display 5.2 An Array in Memory int a[6]: Address of a[0] 1022 1023 On this computer each 1024 a[e] indexed variable uses 1025 2 bytes,so a[3]begins 1026 a[1 2xδ=6 ytes after 1027 the start of a[0]. 41028 a[2] 1029 1030 a[3] 1031 1032 a[4] There is no indexed 1033 variable a[6],but if 1034 a[5] there were one,it Some variable would be here. named stuff Some variable named moreStuff There is no indexed variable a[7].but if there were one,it would be here. Copyright2006 Pearson Addison-Wesley.All rights reserved. 5-15
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-15 An Array in Memory