Arrays, Part 3: Multidimensional arrays

Similar documents
Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Arrays - Review. Two-Dimensional Arrays. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

H212 Introduction to Software Systems Honors

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Arrays - Review. Arrays as Parameters. Arrays as Parameters. Arrays and File Input. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Arrays and File Input

Arrays and File Input

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Chapter 7. Arrays are objects that help us organize large amounts of information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

CSC 1051 Data Structures and Algorithms I

Selection Statements and operators

Introduction to Arrays

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Data Structures and Algorithms I

Selection Statements and operators

Selection and Repetition Revisited

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I

Selection and Repetition Revisited

Selection and Repetition

7.3 Arrays of Strings (Objects) 7.3 Arrays of Strings (Objects) 7.3 Tunes.java. 7.3 Arrays of Objects 9/11/13. ! A UML diagram for the Tunes program

Chapter 8 Arrays. Java Software Solutions. Foundations of Program Design Seventh Edition. John Lewis William Loftus

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Iterators and File Input

Selection and Repetition

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Basics of Java Programming variables, assignment, and input

COMP 202. Programming With Arrays

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Arrays in Java Multi-dimensional Arrays

Happy Cinco de Mayo!!!!

Using Classes and Objects

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Java I/O and Control Structures Algorithms in everyday life

Improved algorithm. Java code. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm

Java code. Updated program. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm. Improved algorithm

Java I/O and Control Structures

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

COMP 202. Programming With Arrays

CSC 1051 Arrays - Review questions

Happy Cinco de Mayo!!!!

Lecture 3: Variables and assignment

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Algorithms and Conditionals

2: Basics of Java Programming

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

Conditional Statements

&KDSWHU$UUD\VDQG9HFWRUV

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Where do objects come from? Good question!

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

File I/O and Exceptions

Designing Classes part 2

11/19/2014. Arrays. Chapter 6: Arrays. Arrays. Arrays. Java Software Solutions for AP* Computer Science A 2nd Edition

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Chapter 8 Multi-Dimensional Arrays

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 6: Arrays. Presentation slides for. Java Software Solutions. for AP* Computer Science 3rd Edition

Graphical User Interfaces

CSC 1051 Algorithms and Data Structures I. Final Examination December 20, Name: KEY. Question Value Score

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Chapter 6 Arrays, Part D 2d Arrays and the Arrays Class

COMP More About Arrays. Yi Hong June 05, 2015

Arrays. Chapter 7. Walter Savitch Frank M. Carrano

Graphical User Interfaces

15. Multidimensional Arrays

CMSC131. Seating Chart

Graphical User Interfaces

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Array Basics: Outline

Lecture 13: Two- Dimensional Arrays

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

CSIS 10A Practice Final Exam Solutions

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Lesson 35..Two-Dimensional Arrays

Data Representation and Applets

Data Representation Classes, and the Java API

AP Computer Science A Unit 7. Notes on Arrays

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Designing Classes Part 2

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Transcription:

Arrays, Part 3: Multidimensional arrays CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus

Two-Dimensional Arrays A one-dimensional array stores a list of elements A two-dimensional array can be thought of as a table of elements, with rows and columns one dimension two dimensions

Two-Dimensional Arrays To be precise, in Java a two-dimensional array is an array of arrays A two-dimensional array is declared by specifying the size of each dimension separately: int[][] table = new int[12][50]; A array element is referenced using two index values: value = table[3][6] The array stored in one row can be specified using one index

declaration coursegrade 2D Arrays - Overview double[][] coursegrade = new double[3][10]; 2D array element coursegrade[1][4] 0 1 2 3 4 5 6 7 8 9 7.9 8.7 9.4 8.2 6.7 9.8 8.7 8.1 7.4 9.1 0 1 2 0 1 2 3 4 5 6 7 8 9 9.3 5.8 6.9 5.5 9.0 8.3 7.7 9.2 9.8 8.2 0 1 2 3 4 5 6 7 8 9 8.9 8.0 8.4 6.2 7.7 7.3 9.6 6.1 7.8 7.3 array element (a row) coursegrade[2]

Two-Dimensional Arrays Expression Type Description table int[][] 2D array of integers, or array of integer arrays table[5] int[] array of integers table[5][12] int integer See TwoDArray.java See SodaSurvey.java

//******************************************************************** // TwoDArray.java Author: Lewis/Loftus // // Demonstrates the use of a two-dimensional array. //******************************************************************** public class TwoDArray // Creates a 2D array of integers, fills it with increasing // integer values, then prints them out. public static void main (String[] args) int[][] table = new int[5][10]; // Load the table with values for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; // Print the table for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) System.out.print (table[row][col] + "\t"); System.out.println();

//******************************************************************** // TwoDArray.java Author: Lewis/Loftus // // Demonstrates the use of a two-dimensional array. //******************************************************************** Output 0 1 2 3 4 5 6 7 8 9 10 public 11 class 12 TwoDArray 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 // 41 Creates 42 a 2D array 43 of integers, 44 45 fills it 46 with increasing 47 48 49 // integer values, then prints them out. public static void main (String[] args) int[][] table = new int[5][10]; // Load the table with values for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; // Print the table for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) System.out.print (table[row][col] + "\t"); System.out.println();

//******************************************************************** // SodaSurvey.java Author: Lewis/Loftus // // Demonstrates the use of a two-dimensional array. //******************************************************************** import java.text.decimalformat; public class SodaSurvey // Determines and prints the average of each row (soda) and each // column (respondent) of the survey scores. public static void main(string[] args) int[][] scores = 3, 4, 5, 2, 1, 4, 3, 2, 4, 4, 2, 4, 3, 4, 3, 3, 2, 1, 2, 2, 3, 5, 4, 5, 5, 3, 2, 5, 5, 5, 1, 1, 1, 3, 1, 2, 1, 3, 2, 4 ; continue final int SODAS = scores.length; final int PEOPLE = scores[0].length; int[] sodasum = new int[sodas]; int[] personsum = new int[people]; Copyright 2014 Pearson Education, Inc.

continue for (int soda=0; soda < SODAS; soda++) for (int person=0; person < PEOPLE; person++) sodasum[soda] += scores[soda][person]; personsum[person] += scores[soda][person]; DecimalFormat fmt = new DecimalFormat("0.#"); System.out.println("Averages:\n"); for (int soda=0; soda < SODAS; soda++) System.out.println("Soda #" + (soda+1) + ": " + fmt.format((float)sodasum[soda]/people)); System.out.println (); for (int person=0; person < PEOPLE; person++) System.out.println("Person #" + (person+1) + ": " + fmt.format((float)personsum[person]/sodas)); Copyright 2014 Pearson Education, Inc.

continue Output for (int soda=0; soda Averages: < SODAS; soda++) for (int person=0; person < PEOPLE; person++) Soda #1: 3.2 sodasum[soda] Soda += scores[soda][person]; #2: 2.6 personsum[person] Soda #3: += scores[soda][person]; 4.2 Soda #4: 1.9 DecimalFormat fmt = new DecimalFormat ("0.#"); System.out.println Person ("Averages:\n"); #1: 2.2 Person #2: 3.5 for (int soda=0; soda Person < SODAS; #3: 3.2 soda++) System.out.println Person ("Soda #4:#" 3.5 + (soda+1) + ": " + fmt.format Person ((float)sodasum[soda]/people)); #5: 2.5 Person #6: 3 System.out.println (); for (int person=0; Person person < #7: PEOPLE; 2 person++) System.out.println Person ("Person #8: 2.8 #" + (person+1) + ": " + fmt.format Person ((float)personsum[person]/sodas)); #9: 3.2 Person #10: 3.8 Copyright 2014 Pearson Education, Inc.

Multidimensional Arrays An array can have many dimensions if it has more than one dimension, it is called a multidimensional array Each dimension subdivides the previous one into the specified number of elements Each dimension has its own length constant Because each dimension is an array of array references, the arrays within one dimension can be of different lengths these are sometimes called ragged arrays