{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

13-Arrays-4up

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

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

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

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

View Full Document
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 0 1 2 3 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]; } 0 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 20
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}