Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]

Slides:



Advertisements
Similar presentations
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Advertisements

Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Arrays Chapter 6.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Arrays. Introduction This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Java SE 8 for Programmers, Third Edition
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Section 11.4 Tree Diagrams, Tables, and Sample Spaces Math in Our World.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6 Methods: A Deeper Look. Objectives In this chapter you will learn: How static methods and fields are associated with an entire class rather.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
 Pearson Education, Inc. All rights reserved Passing Arrays to Methods To pass array argument to a method – Specify array name without.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Chapter 7 - Pointers Outline 7.1 Introduction
© 2016 Pearson Education, Ltd. All rights reserved.
Structure, Unions, typedef and enumeration
8 Arrays.
Chapter 7 - Pointers Outline 7.1 Introduction
Introduction to Programming
7 Arrays.
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Presentation transcript:

Chapter 7 Arrays

A 12-element array

Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[] = new int[ 12 ]; Equivalent to int c[]; // declare array variable c = new int[ 12 ]; // create array –We can create arrays of objects too String b[] = new String[ 100 ];

Problem Develop a test case that create an array of 10 integers and print out the initial values of this array’s elements

Solution 1: Using the Conventional for Loop

Solution 2: Using the for-each Loop

Using an Array Initializer int n[] = { 10, 20, 30, 40, 50 }; –Creates a five-element array –Index values of 0, 1, 2, 3, 4

Problem Develop a test case that create an array of the following 10 integers 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 Print out the values of this array’s elements

Solution 1: Using the Conventional for Loop

Exercise Use the for-each loop to rewrite the previous test case

Problem Develop a test case that create an array of 10 even integers, starting from 2 Print out the values of this array’s elements

Problem Develop a test case that create an array of the following 10 integers 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 Calculate the sum of all elements in the array

Solution 1

Question Can we make the sum() method to be the static one?

Solution 2: Using varargs

Problem Using bar charts to display array data (grade distribution) graphically Bar labels: “00-09”, “10-19”, …, “90-99”, and “100” See an example in the next slide

Example Input: Grade distribution data: 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 Output: The bar chart

Exercise Compare the following solution with the previous one

Using the Elements of an Array as Counters

Revisiting the Randomness Verification Problem Problem statement: To show that the numbers produced by nextInt() occur with approximately equal likelihood, develop an object that rolls 6000 times of a die. Thus, each integer from 1 to 6 should appear approximately 1000 times

Solution 1: Without Using Array

Solution 2: Using Arrays

Question Do a comparison between the Solution 1 and Solution 2

Using Arrays to Analyze Survey Results

Problem Develop a method that receives an array of responses about the rating of food quality and returns an array of counts corresponding to each rating 1-10 Rating scale: 1 mean awful, 10 means excellent See an example in the next slide

Example Input: 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 Expected output: 2, 2, 2, 2, 5, 11, 5, 7, 1, 3

Case Study: Card Shuffling and Dealing Simulation

Sub-Problem 1 A card has a face and a suit –A face can be one of the following 13 values: Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King –A suit can be one of the following 4 values: Hearts, Diamonds, Clubs, and Spades 1.Develop enums for Face and Suit 2.Develop a Card class with the toString() method

Sub-Problem 2 Develop the DeckOfCards class by making the following test pass

Sub-Problem 3 Develop a method that can shuffle a deck of cards by making the following test pass

Sub-Problem 4 Develop a method that can deal one card –Throw an NoSuchElementException if there is no card left

Case Study: Class GradeBook Using an Array/List to Store Grades

Problem Statement A grade book has a course name and a list of grades Develop a grade book class that can perform the following tasks 1.Constructing a grade book object with a given course name and a given array of grades 2.Returning the minimum grade 3.Returning the maximum grade 4.Returning the average grade 5.Drawing the bar chart of grades

Task 1: Constructor

Example Course name: CS101 Introduction to Java Programming Grades: 87, 68, 94, 100, 83, 78, 85, 91, 76, 87

Task 2: Minimum Grade

Q: How can we use Collections.min( ) ? A: Use java.util.List instead of array

Task 3: Maximum Grade

Task 4: Average Grade

Example Input: 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 Output: 84.9

Task 5: Bar Chart

Example Input: 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 Output:

Multidimensional Arrays

Two-dimensional array with three rows and four columns

Multidimensional Arrays (Cont.) Arrays of one-dimensional array –Declaring two-dimensional array b[2][2] int b[][] = { { 1, 2 }, { 3, 4 } }; –1 and 2 initialize b[0][0] and b[0][1] –3 and 4 initialize b[1][0] and b[1][1] int b[][] = { { 1, 2 }, { 3, 4, 5 } }; –row 0 contains elements 1 and 2 –row 1 contains elements 3, 4 and 5

Multidimensional Arrays (Cont.) Two-dimensional arrays with rows of different lengths –Lengths of rows in array are not required to be the same E.g., int b[][] = { { 1, 2 }, { 3, 4, 5 } };

Multidimensional Arrays (Cont.) Creating two-dimensional arrays with array- creation expressions –Can be created dynamically 3 -by- 4 array int b[][]; b = new int[ 3 ][ 4 ]; Rows can have different number of columns int b[][]; b = new int[ 2 ][ ]; // create 2 rows b[ 0 ] = new int[ 5 ]; // create 5 columns for row 0 b[ 1 ] = new int[ 3 ]; // create 3 columns for row 1

Outline Use nested array initializers to initialize array1 Use nested array initializers of different lengths to initialize array2

Outline array[row].length returns number of columns associated with row subscript Use double-bracket notation to access two-dimensional array values

7.9 Multidimensional Arrays (Cont.) Common multidimensional-array manipulations performed with for statements –Many common array manipulations use for statements E.g., for ( int column = 0; column < a[ 2 ].length; column++ ) a[ 2 ][ column ] = 0;

Case Study: Class GradeBook Using a Two-Dimensional Array

Problem Statement A grade book has a course name and a list of grades. Each student has more than one grades Develop a grade book class that can perform the following tasks 1.Constructing a grade book object with a given course name and a given 2D-array of grades 2.Returning the minimum grade 3.Returning the maximum grade 4.Returning a list of average grades 5.Drawing the bar chart of grades

Examples Grades Test1Test2Test3 Student 1: Student 2: Student 3: Student 4: Student 5: Student 6: Student 7: Student 8: Student 9: Student 10:

Task 1: Constructor

Task 2: Minimum Grade

Task 3: Maximum Grade

Task 4: Average Grades

Task 4: Drawing Bar Chart

Case Study: The Sudoku Puzzle

What You Have Learnt? To be filled