2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.

Slides:



Advertisements
Similar presentations
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Advertisements

Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
A First Book of ANSI C Fourth Edition
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 9 Multidimensional Arrays and the ArrayList Class.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
CS 116: Object Oriented Programming II. Topics Vectors Multidimensional Arrays ArrayList.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Introduction to Computer Science / Procedural – 67130
CS2011 Introduction to Programming I Arrays (I)
INC 161 , CPE 100 Computer Programming
Presentation transcript:

2 dimensional arrays Steven Wood ©2005

Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet

3D example Can have more than 2 dimensions!

How to think of a 2d array A 2-D array is an array whose components are themselves arrays

Creating an 2d array A two-dimensional array is an array of arrays. In this example: The first index is the 12 months, indexed from 0 to 11. Each month array is an array of 31 days, indexed from 0 to 30 double rainfall[][] = new double[12][31]; Month index Day index

Using initializer lists int myTable[][] = { {23, 45, 65, 34, 21, 67, 78}, {46, 14, 18, 46, 98, 63, 88}, {98, 81, 64, 90, 21, 14, 23}, {54, 43, 55, 76, 22, 43, 33} };

Setting each value individually int myTable[][]; myTable[0,0] = 23; myTable[0,1] = 45; … myTable[3,5] = 43; myTable[3,6] = 33;

Example Track daily temperature for 4 weeks double[][] weektemp = new double[4][7];

Finding # of rows (weeks) to determine the number of rows in a 2D array, use length int numWeeks = weektemp.length; //4

Finding # of columns (days) to determine the number of columns in a 2D array, find length of first row int numDays = weektemp[0].length; // 7 Side note: a 2d array does not have to be rectangular, but we won’t look at this situation

Our example Create a computized mark book for a teacher Assume only 3 students and 3 marks for each Assume all marks have same weighting (e.g. all percents) MarkBook - String theClass[][] - String name +MarkBook(String name, int students, int marks) +initClass() +displayClass()

A model of the information Thomas Robert Katie Store student average in these Class average Name + 3 marks + Average  5 columns blank +3 students+ average  5 columns

Sample runs…

Do this #1… Download the program MarkBook.java Find the constructor Find the line that declares & allocates space for the array It needs to be completed How big should the array be (look at the previous slide) Make sure that you use variables to set the size!

Do this #2… Find the initClass() method Use the picture of the array contents from 2 slides previous Remove the comment marks and complete the assignment statements that fill the array with values. E.g. //theClass[2][0] = theClass[2][0] = “Robert”; Etc.

Example of using a loops int[][] array = new int[10][5]; // print array in rectangular form for (int r=0; r<array.length; r++) { //# of rows // print columns for each row for (int c=0; c<array[r].length; c++) { System.out.print(" " + array[r][c]); } System.out.println(""); }

Do this #3… In MarkBook.java find the displayClass method Use the information on the last slide to complete the method so that it displays the contents of the array Update the main method so that you use the displayClass method to print out the array!

Finding a student’s average Which student? (Which row?) set a variable Add up all the marks (total) All the columns (use a loop!) Hint: the inside loop for printing… Find the average (total / # of marks) Put in proper array spot (Row? Column?) use variables not “1, 4”

Do this #4… Complete the method that will find the average for a student (to 1 decimal) Place the average in the proper spot in the array Make sure you update the main method to use this method!

Finding a test average Which test? (Which column?) set a variable Add up all the marks (total) All the rows (use a loop!) Hint: the outside loop for printing… Find the average (total / # of marks) Put in proper array spot (Row? Column?) use variables not “4, 1”

Do this #5… Complete the method that will find the average for a test (to 1 decimal) Place the average in the proper spot in the array Make sure that you update the main method to use this method

Do this #6… Add a method that will find the class average It may depend on the student averages being calculated already Place it in the array

Do this #7… Improve the initClass method so that the values can be entered in from the keyboard Maybe the entire class doesn’t have to be entered at one time  more realistic!

Do this #8… Add a method that will allow you to change/update a student’s information You maybe should then recalculate the averages (test, student, class) Add a method to delete a student Add a method to delete a test

Sources Text