13-Arrays-4up

13-Arrays-4up - CMPT 125/128 Dr B Fraser Topics Slides#13 1...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
24/07/11 1 Slides #13 Arrays Chapter 8 CMPT 125/128 ± Dr. B. Fraser 24/07/11 2 Topics 1) How can we store many values at once? 2) How can arrays (and elements) be passed to functions? 3) How can we store objects in an array? 24/07/11 3 Arrays 24/07/11 4 Arrays ± Array: ² ± Arrays vs Vectors ² A vector is an object with complex operations. ² Arrays are not objects:. .. ± Array Declaration: ² Specify type of elements, and # elements. int daysPerMonth[12]; ² Once created, the array size. .. ± Directly access to any element: ² For N elements. .. ² daysPerMonth[0] = 31; // January Idx Val Jan 0 31 Feb 1 28 Mar 2 31 Apr 3 30 May 4 31 Jun 5 30 Jul 6 31 Aug 7 31 Sep 8 30 Oct 9 31 Nov 10 30 Dec 11 31
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
24/07/11 5 Initializing an Array ± Array can be initialized when declared: int somePrimes[] ² # of values in the initializer list. .. ± Initialized Array of strings: string monthNames[] = { "January", "February", .... "December" }; hoursWorked.cpp 24/07/11 6 Array Bounds ± C++ does not do. .. on array index operations. ² Out of bounds access. .. ² Could be anything else in memory! int price[4]; price[0] = 10; // price[4] = 11; // price[-1] = 12; // outOfBounds.cpp 0123 int price[] 24/07/11 7 Copying an array ± To copy an array. .. const int SIZE = 10; int first[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; int second[SIZE]; for (int i = 0; i < SIZE; i++) { second[i] = first[i]; } 0123456789 2 4 6 8 10 12 14 16 18 20
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 5

13-Arrays-4up - CMPT 125/128 Dr B Fraser Topics Slides#13 1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online