DOC PREVIEW
LETU COSC 2103 - Chapter 7 - Arrays

This preview shows page 1-2-3-4-5-33-34-35-36-66-67-68-69-70 out of 70 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 70 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 7 - Arrays7.1 Introduction7.2 ArraysPowerPoint Presentation7.2 Arrays (cont.)Slide 67.3 Declaring and Creating Arrays7.4 Examples Using Arrays7.4 Examples Using Arrays (Cont.)InitArray.java Line 9 Declare array as an array of ints Line 11 Create 10 ints for array; each int is initialized to 0 by default Line 16 array.length returns length of array Line 17 array[counter] returns int associated with index in arrayInitArray.java Each int is initialized to 0 by defaultSlide 12InitArray.java Line 11 Declare array as an array of ints Line 11 Compiler uses initializer list to allocate arrayInitArray.java Each array element corresponds to element in initializer listSlide 15InitArray.java Line 10 Declare array as an array of ints Line 12 Create 10 ints for array Line 16 Use array index to assign array valueInitArray.javaSlide 18SumArray.java Line 9 Declare array with initializer list Lines 13-14 Sum all array valuesSlide 20Histogram.java Line 9 Declare array with initializer list Line 19 For each array element, print associated number of asterisksHistogram.javaSlide 23RollDie.java Line 9 Declare frequency as array of 7 ints Lines 12-13 Generate 6000 random integers in range 1-6 Line 13 Increment frequency values at index associated with random numberSlide 25StudentPoll.java Lines 9-11 Declare responses as array to store 40 responses Line 12 Declare frequency as array of 11 int and ignore the first element Lines 16-17 For each response, increment frequency values at index associated with that responseStudentPoll.javaSlide 287.5 References and Reference Parameters7.6 Passing Arrays to MethodsPassArray.java Line 15 Declare 5-int array with initializer list Line 24 Pass array by reference to method modifyArrayPassArray.java Line 35 Pass array[3] by value to method modifyElement Lines 43-47 Method modifyArray manipulates the array directly Lines 50-53 Method modifyElement manipulates a primitive’s copy Lines 52 The original primitive is left unmodifiedPassArray.java7.7 Sorting ArraysBubbleSort.java Line 15 Declare 10-int array with initializer list Line 23 Pass array by reference to method bubbleSort to sort arrayBubbleSort.java Line 36 Method bubbleSort receives array reference as parameter Lines 39-53 Use loop and nested loop to make passes through array Lines 48-49 If pairs are in decreasing order, invoke method swap to swap pairsBubbleSort.java Lines 58-65 Method swap swaps two values in array reference7.8 Searching Arrays: Linear Search and Binary Search7.8 Searching Arrays: Linear Search and Binary Search (Cont.)LinearSearch.java Line 11 Declare array of intsLinearSearch.java Lines 39-42 Allocate 100 ints for array and populate array with even ints Line 50 Loop through array Lines 53-54 If array element at index matches search key, return indexLinearSearch.java Line 61 Invoked when user presses Enter Line 68 Invoke method linearSearch, using array and search key as argumentsSlide 43BinarySearch.java Line 14 Declare array of intsBinarySearch.java Lines 48-51 Allocate 15 ints for array and populate array with even ints Line 56 Invoked when user presses EnterBinarySearch.java Line 65 Invoke method binarySearch, using array and search key as argumentsBinarySearch.java Lines 93-94 If search key matches middle array element, return element index Lines 97-98 If search key is less than middle array element, repeat search on first array half Lines 101-102 If search key is greater than middle array element, repeat search on second array half Lines 112-137 Method build-Output displays array contents being searchedBinarySearch.java Line 128 Display an asterisk next to middle elementBinarySearch.java7.9 Multidimensional Arrays7.9 Multidimensional Arrays (Cont.)Slide 52InitArray.java Line 16 Declare array1 with six initializers in two sublists Line 17 Declare array2 with six initializers in three sublistsInitArray.java Line 34 array[row].length returns number of columns associated with row subscript Line 35 Use double-bracket notation to access two-dimensional array valuesDoubleArray.java Lines 7-9 Declare grades as 3-by-4 array Lines 7-9 Each row represents a student; each column represents an exam gradeDoubleArray.java Lines 31-32 Determine minimum and maximum for all student Lines 35-37 Determine average for each studentDoubleArray.java Lines 54-61 Use a nested loop to search for lowest grade in series Lines 74-81 Use a nested loop to search for highest grade in seriesDoubleArray.java Line 88 Method average takes array of student test results as parameter Lines 93-94 Calculate sum of array elements Line 97 Divide by number of elements to get averageDoubleArray.java7.10 (Optional Case Study) Thinking About Objects: Collaboration Among ObjectsSlide 61Slide 627.10 Thinking About Objects (cont.)Slide 64Slide 65Slide 66Slide 67Slide 68Slide 69Slide 70 2003 Prentice Hall, Inc. All rights reserved.Chapter 7 - ArraysOutline7.1 Introduction7.2 Arrays7.3 Declaring and Creating Arrays7.4 Examples Using Arrays7.5 References and Reference Parameters7.6 Passing Arrays to Methods7.7 Sorting Arrays7.8 Searching Arrays: Linear Search and Binary Search7.9 Multidimensional Arrays7.10 (Optional Case Study) Thinking About Objects: Collaboration Among Objects 2003 Prentice Hall, Inc. All rights reserved.7.1 Introduction•Arrays–Data structures–Related data items of same type–Remain same size once created•Fixed-length entries 2003 Prentice Hall, Inc. All rights reserved.7.2 Arrays•Array–Group of variables•Have same type–Reference type 2003 Prentice Hall, Inc. All rights reserved.Fig. 7.1 A 12-element array. Name of array (note that all elements of this array have the same name, c)Index (or subscript) of the element in array cc[ 0 ]c[ 1 ]c[ 2 ]c[ 3 ]c[ 4 ]c[ 5 ]c[ 6 ]c[ 7 ]c[ 8 ]c[ 9 ]c[ 10 ]c[ 11 ]-4560721543-89062-31645378 2003 Prentice Hall, Inc. All rights reserved.7.2 Arrays (cont.)•Index–Also called subscript–Position number in square brackets–Must be positive integer or integer expressiona = 5;b = 6;c[ a + b ] += 2;•Adds 2 to c[ 11 ] 2003 Prentice Hall, Inc. All rights reserved.7.2 Arrays (cont.)•Examine array c–c is the array name–c.length accesses array c’s length–c has 12 elements ( c[0], c[1], … c[11] )•The value of c[0] is –45 2003 Prentice Hall, Inc. All rights reserved.7.3 Declaring and Creating Arrays•Declaring and Creating arrays–Arrays are objects that occupy memory–Created dynamically


View Full Document

LETU COSC 2103 - Chapter 7 - Arrays

Documents in this Course
Arrays

Arrays

16 pages

Templates

Templates

17 pages

Methods

Methods

22 pages

Methods

Methods

22 pages

Arrays

Arrays

11 pages

Load more
Download Chapter 7 - Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 7 - Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 7 - Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?