array - Programming Arrays 102 Prog. Fundamentals I: Arrays...

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

View Full Document Right Arrow Icon
Arrays Programming
Background image of page 1

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

View Full DocumentRight Arrow Icon
P102 Prog. Fundamentals I: Arrays / Slide 2 Arrays An array is a collection of data elements that are of the same type (e.g., a collection of integers, collection of characters, collection of doubles).
Background image of page 2
P102 Prog. Fundamentals I: Arrays / Slide 3 Arrays 1-dimensional array. 3-dimensional array (3rd dimension is the day). Oct 15 Oct 16 Oct 14
Background image of page 3

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

View Full DocumentRight Arrow Icon
P102 Prog. Fundamentals I: Arrays / Slide 4 Array Applications Given a list of test scores, determine the maximum and minimum scores. Read in a list of student names and rearrange them in alphabetical order (sorting). Given the height measurements of students in a class, output the names of those students who are taller than average.
Background image of page 4
P102 Prog. Fundamentals I: Arrays / Slide 5 Array Declaration Syntax: <type> <arrayName>[<array_size>] Ex. int Ar[10]; The array elements are all values of the type <type>. The size of the array is indicated by <array_size> , the number of elements in the array. <array_size> must be an int constant or a constant expression. Note that an array can have multiple dimensions.
Background image of page 5

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

View Full DocumentRight Arrow Icon
P102 Prog. Fundamentals I: Arrays / Slide 6 Array Declaration // array of 10 uninitialized ints int Ar[10]; -- -- -- -- Ar -- -- -- -- -- -- 4 5 6 3 0 2 8 9 7 1 0 1 2 3 4 5
Background image of page 6
P102 Prog. Fundamentals I: Arrays / Slide 7 Subscripting Declare an array of 10 integers: int Ar[10]; // array of 10 ints To access an individual element we must apply a subscript to array named Ar. A subscript is a bracketed expression. The expression in the brackets is known as the index. First element of array has index 0. Ar[0] Second element of array has index 1, and so on. Ar[1], Ar[2], Ar[3],… Last element has an index one less than the size of the array. Ar[9] Incorrect indexing is a common error.
Background image of page 7

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

View Full DocumentRight Arrow Icon
P102 Prog. Fundamentals I: Arrays / Slide 8 Subscripting // array of 10 uninitialized ints int Ar[10]; Ar[3] = 1; int x = Ar[3]; -- -- 1 -- Ar -- -- -- -- -- -- 4 5 6 3 0 2 8 9 7 1 Ar[4]Ar[5]Ar[6] Ar[3] Ar[0] Ar[2] Ar[8]Ar[9] Ar[7] Ar[1] 1 -- -- -- -- --
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/07/2011 for the course COMP 102 taught by Professor Daniel during the Spring '08 term at HKUST.

Page1 / 26

array - Programming Arrays 102 Prog. Fundamentals I: Arrays...

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

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