Multi-Dimensional Arrays. A Table of Values Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) Year5.00%5.50%6.00%

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
Arrays Chapter 6 Chapter 6.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Java Syntax Primitive data types Operators Control statements.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Introduction to Arrays. Useful Array Operations  Finding the Highest Value int [] numbers = new int[50]; int highest = numbers[0]; for (int i = 1; i.
Java Unit 9: Arrays Declaring and Processing Arrays.
Lecturer: Dr. AJ Bieszczad Chapter 6 COMP 150: Introduction to Object-Oriented Programming 6-1 l Array Basics l Arrays in Classes and Methods l Programming.
2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.
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.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Title Slid CSC 444 Java Programming Arrays By Ralph B. Bisland, Jr.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
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.
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 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.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
ARRAYS Multidimensional realities Image courtesy of
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
ENUMERATED TYPES Java Basics. Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
EKT472: Object Oriented Programming
Multidimensional Arrays
Presentation transcript:

Multi-Dimensional Arrays

A Table of Values Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) Year5.00%5.50%6.00% 1$1050$1055$1060 2$1103$1113$1124 3$1158$1174$1191

Row and Column Indices for an Array Indices012 0$1050$1055$1060 1$1103$1113$1124 2$1158$1174$1191

Multi-Dimensional Array Basics  We declare a variable, table, to be a two- dimensional array as follows: int [][] table = new int [10][6];  This is the same as for one-dimensional arrays except we use a second pair of square brackets in two places.  You can have arrays of any number of dimensions.

Multi-Dimensional Array Basics (cont’d)  In a two-dimensional array we consider the first index to represent the row number and the second to represent the column.  As was true of the indexed variables for one-dimensional arrays, indexed variables of multi-dimensional arrays are variables of the base type and can be used anyplace that a variable of the base type is allowed.

Example of Using a Two- Dimensional Array /** Displays a two-dimensional table showing how interest rates affect bank balances. */ public class InterestTable { public static void main(String[] args) { int[][] table = new int[10][6]; int row, column; for (row = 0; row < 10; row++) for (column = 0; column < 6; column++) table[row][column] = balance( , row + 1, ( *column)); System.out.println("Balances for Various Interest Rates"); System.out.println("Compounded Annually"); System.out.println("(Rounded to Whole Dollar Amounts)"); System.out.println("Years 5.00% 5.50% 6.00% 6.50% 7.00% 7.50%"); System.out.println( ); for (row = 0; row < 10; row++) { System.out.print((row + 1) + " "); for (column = 0; column < 6; column++) System.out.print("$" + table[row][column] + " "); System.out.println( ); } }

Example of Using a Two- Dimensional Array (cont’d) /** Returns the balance in an account that starts with startBalance and is left for the indicated number of years with rate as the interest rate. Interest is compounded annually. The balance is rounded to a whole number. */ public static int balance(double startBalance, int years, double rate) { double runningBalance = startBalance; int count; for (count = 1; count <= years; count++) runningBalance = runningBalance*(1 + rate/100); return (int) (Math.round(runningBalance)); } }

Multi-Dimensional Array Parameters and Returned Values  Methods may have multi-dimensional array parameters and may return a multi- dimensional array.  The situation is similar to that of the one- dimensional case, except that you use more square brackets.

Example Using a Multi- Dimensional Array Parameter /** Displays a two-dimensional table showing how interest rates affect bank balances. */ public class InterestTable2 { public static void main(String[] args) { int[][] table = new int[10][6]; int row, column; for (row = 0; row < 10; row++) for (column = 0; column < 6; column++) table[row][column] = balance( , row + 1, ( *column)); System.out.println("Balances for Various Interest Rates"); System.out.println("Compounded Annually"); System.out.println("(Rounded to Whole Dollar Amounts)"); System.out.println("Years 5.00% 5.50% 6.00% 6.50% 7.00% 7.50%"); System.out.println( ); showTable(table); }

Example Using a Multi- Dimensional Array Parameter (cont’d) /** Precondition: The array displayArray has 10 rows and 6 columns. Postcondition: The array contents are displayed with dollar signs. */ public static void showTable(int[][] displayArray) { int row, column; for (row = 0; row < 10; row++) { System.out.print((row + 1) + " "); for (column = 0; column < 6; column++) System.out.print("$" + displayArray[row][column] + " "); System.out.println( ); } } public static int balance(double startBalance, int years, double rate) { double runningBalance = startBalance; int count; for (count = 1; count <= years; count++) runningBalance = runningBalance*(1 + rate/100); return (int) (Math.round(runningBalance)); } }

Implementation of Multi- Dimensional Arrays  In Java, multi-dimensional arrays are implemented using one-dimensional arrays.  The array table declared as follows: int [][] table = new int[10][6]; is a one-dimensional array of length 10 whose elements are one-dimensional arrays of length 6.  In other words, multi-dimensional arrays are arrays of arrays.

The Method showTable Redefined /** The array displayArray can have any dimensions. Postcondition: The array contents are displayed with dollar signs. */ public static void showTable(int[][] displayArray) { int row, column; for (row = 0; row < displayArray.length; row++) { System.out.print((row + 1) + " "); for (column = 0; column < displayArray[row].length; column++) System.out.print("$" + displayArray[row][column] + " "); System.out.println( ); } }

Programming Example Using Multi-Dimensional Arrays /** Class for a one-week record of the time worked by each employee. Uses a five-day week (Mon.-Fri.). main has a sample application. */ public class TimeBook { private int numberOfEmployees; private int[][] hours; //hours[i][j] has the hours for employee j on day i. private int[] weekHours; //weekHours[i] has the week’s hours worked for //employee i+1. private int[] dayHours; //dayHours[i] has the total hours worked by all //employees on day i. Monday is 0, Tuesday 1, etc. /** Reads hours worked for each employee on each day of the week into the two-dimensional array hours. (The method for input is just a stub in this preliminary version.) Computes the total weekly hours for each employee and the total daily hours for all employees combined. */ public static void main(String[] args) { TimeBook book = new TimeBook(3); book.setHours( ); book.update( ); book.showTable( ); }

Programming Example Using Multi-Dimensional Arrays (cont’d) public TimeBook(int theNumberOfEmployees) { numberOfEmployees = theNumberOfEmployees; hours = new int[5][numberOfEmployees]; //the 5 is for the 5 days Monday through Friday. weekHours = new int[numberOfEmployees]; dayHours = new int[5]; } public void setHours( ) //This is just a stub. { hours[0][0] = 8; hours[0][1] = 0; hours[0][2] = 9; hours[1][0] = 8; hours[1][1] = 0; hours[1][2] = 9; hours[2][0] = 8; hours[2][1] = 8; hours[2][2] = 8; hours[3][0] = 8; hours[3][1] = 8; hours[3][2] = 4; hours[4][0] = 8; hours[4][1] = 8; hours[4][2] = 8; } public void update( ) { computeWeekHours( ); computeDayHours( ); }

Programming Example Using Multi-Dimensional Arrays (cont’d) private void computeWeekHours( ) { int dayNumber, employeeNumber, sum; for (employeeNumber = 1; employeeNumber <= numberOfEmployees; employeeNumber++) {//Process one employee: sum = 0; for(dayNumber = 0; dayNumber < 5; dayNumber++) sum = sum + hours[dayNumber][employeeNumber - 1]; //sum contains the sum of all the hours worked //in one week by employee with number employeeNumber. weekHours[employeeNumber - 1] = sum; } } private void computeDayHours( ) { int dayNumber, employeeNumber, sum; for (dayNumber = 0; dayNumber < 5; dayNumber++) {//Process one day (for all employees): sum = 0; for (employeeNumber = 1; employeeNumber <= numberOfEmployees; employeeNumber++) sum = sum + hours[dayNumber][employeeNumber - 1]; //sum contains the sum of all hours worked by all //employees on day dayNumber. dayHours[dayNumber] = sum; } }

Programming Example Using Multi-Dimensional Arrays (cont’d) public void showTable( ) { int row, column; System.out.print("Employee "); for (column = 0; column < numberOfEmployees; column++) System.out.print((column + 1) + " "); System.out.println("Totals"); System.out.println( ); for (row = 0; row < 5; row++) { System.out.print(day(row) + " "); for (column = 0; column < hours[row].length; column++) System.out.print(hours[row][column] + " "); System.out.println(dayHours[row]); } System.out.println( ); System.out.print("Total = "); for (column = 0; column < numberOfEmployees; column++) System.out.print(weekHours[column] + " "); System.out.println( ); }

Programming Example Using Multi-Dimensional Arrays (cont’d) //Converts 0 to "Monday", 1 to "Tuesday" etc. //Blanks used to make all strings the same length. private String day(int dayNumber) { String dayName = null; switch (dayNumber) { case 0: dayName = "Monday "; break; case 1: dayName = "Tuesday "; break; case 2: dayName = "Wednesday"; break; case 3: dayName = "Thursday "; break; case 4: dayName = "Friday "; break; default: System.out.println("Fatal Error."); System.exit(0); break; } return dayName; } }