Two-Dimensional Arrays

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
Arrays.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
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.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
Chapter 9 Introduction to Arrays
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
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.
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.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
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.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
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.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
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.
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
COS 312 DAY 12 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress report due Assignment corrected 1 MIA – 2 A’s, 2 B’s, 1 D, 1 F and 1 MIA – Code\pig.
© 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. Arrays are objects that help us organize large amounts of information.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
EGR 2261 Unit 10 Two-dimensional Arrays
Lecture 8: 2D Arrays and Nested Loops
Two-Dimensional Arrays
Computer Programming BCT 1113
Foundations of Programming: Arrays
A simple way to organize data
JavaScript: Functions.
Arrays An Array is an ordered collection of variables
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Topic 26 Two Dimensional Arrays
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chapter 7 Multidimensional Arrays
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays 6-Dec-18.
Coding Concepts (Data Structures)
Multidimensional Arrays
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Multidimensional Arrays
Multidimensional array
Multidimensional Arrays
Multi-Dimensional Arrays
Dr Tripty Singh Arrays.
INC 161 , CPE 100 Computer Programming
Arrays in Java.
Arrays 2-May-19.
Outline Declaring and Using Arrays Arrays of Objects
C++ Array 1.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Arrays.
Presentation transcript:

Two-Dimensional Arrays The arrays we’ve looked at so far have all been one-dimensional arrays because they store a simple list of values. A two-dimensional array has values in two “dimensions” which are like the rows and columns of a table or a grid. Many board games are based on a grid: checkers, chess, Scrabble, etc. Many apps are based on grids too: CandyCrush, 2048, Ruzzle. You can probably think of others. Two-dimensional arrays are a very common topic on the free response section of the AP test.

2D 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, a two-dimensional array in Java is an array of arrays (row major) A two-dimensional array is declared by specifying the size of each dimension separately: int[][] scores = new int[12][50]; A two-dimensional array element is referenced using two index values value = scores[3][6] The array stored in one row or column can be specified using one index We must use two indexes to refer to a value in a two-dimensional array, one for the row and another for the column.

Two-Dimensional Arrays Expression Type Description scores int[][] 2D array of integers, or array of integer arrays scores[5] int[] array of integers scores[5][12] int integer When you know the values that you want to store in the 2-D array, you can declare the array and store the values in it immediately. This operation is just like what you do for a one-dimensional array. Two pairs of brackets, are used to tell the computer that it is not a regular variable. Every cell in a 2-D array is assigned a pair of coordinates that are based on the row number and the column number. The first pair of brackets represents the row number and the second pair of brackets represents the column number. The rows and columns both begin at zero and end with one less than the number of rows or columns.

TwoDArray.java The TwoDArray program instantiates a two-dimensional array of integers. The size of the dimensions is specified when the array is created. In this case the array is 5 rows by 10 columns. Nested for loops are used to led the array with values and to print them in a table. Carefully trace the processing to see how the nested loops visit each elements in the two-dimensional array. Note that the outer loops are governed by table.length, which is the number of rows, and the inner loops are governed by table[row].length, which is the number of columns in that row. Please pause the video if you need more time to study the code.

TwoDArray Teminal Window Here is the output of the two-dimensional array. The combination of print and “\t” allows the printout to be in multiple columns.

SodaSurvey.java As with one-dimensional arrays, an initializer list can be used to instantiate a two-dimensional array, where each element is itself an array initializer list. This technique is used in the SodaSurvey program shown here. Suppose a soda company held a teste test for four new flavors to see how teens liked them. The company got 10 students to try each new flavor and give it a score from 1 to 5,, where 1 means “gross” and 5 means “awesome.” The 2-d array called scores in the SodaSurvey program stores the results of that survey. Each row is a soda and each column in that row is the student who tasted it. More generally, each row holds the responses that all students gave or one particular soda flavor, and each column holds the responses of one student for all soda. The SodaSurvey program computes and prints the average responses for each soda and each student. The sums of each soda and student are first stored in 1-D arrays of integers. Then the averages are computed and printed. Remember that although the data is integers, the for loops used for the math are casting one of the integers to a double so a double can be outputted. Please pause the video if you need more time to study the code.

SodaSurvey Terminal Window This what the output of the SodaSurvey program looks like.