Arrays (Chapter 5) Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional
Definition (Wikipedia) In computer science an array[1] is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage.
Applications Sample Data Vectors and Matrices –Systems of Linear Equations
Sample Data
2D Data
Matrices and Vectors
Declaration int main() { float sample;
Declaration int main() { float sample[10];
Declaration int main() { float sample[10]; sample[0], sample[1],... sample[8], sample[9]
Initialization int main() { float sample = 5.0;
Initialization int main() { float sample[10] = {5.0, 1.2, -4.6, -7.5, 8.9, -1.0, 12.7, 0.0, 6.6, 4.2};
Initialization int main() { float sample[10] = {5.0, 1.2, -4.6, -7.5, 8.9, -1.0, 12.7, 0.0, 6.6, 4.2}; sample[0] = 5.0, sample[1] = 1.2,... sample[9] = 4.2
Use...
Gotchas First element of array is [0]
Gotchas First element of array is [0] Last element of array is [n-1] (major source of bugs!)
Gotchas First element of array is [0] Last element of array is [n-1] (major source of bugs!) No bounds check (MAJOR source of impossible-to- find bugs!)