CSC 142 K 1 CSC 142 Multidimensional Arrays [Reading: chapter 10]

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
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.
Multidimensional Arrays in Java Vidhu S. Kapadia.
©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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
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.
Java Unit 9: Arrays Declaring and Processing Arrays.
A First Book of ANSI C Fourth Edition
2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Chapter 9 Multidimensional Arrays and the ArrayList Class.
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Arrays and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
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.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
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.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
1.8 Multidimensional Arrays academy.zariba.com 1.
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.
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
ARRAYS Multidimensional realities Image courtesy of
Arrays. Arrays are objects that help us organize large amounts of information.
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 6: Using Arrays.
Two-Dimensional Arrays
Two Dimensional Arrays
EGR 115 Introduction to Computing for Engineers
Arrays An Array is an ordered collection of variables
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
MSIS 655 Advanced Business Applications Programming
CSC 142 Arrays [Reading: chapter 12].
INC 161 , CPE 100 Computer Programming
Arrays Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

CSC 142 K 1 CSC 142 Multidimensional Arrays [Reading: chapter 10]

CSC 142 K 2 2D arrays  How to represent a table of data?  e.g. the daily rainfall over a period of 4 weeks Mo Tu We Th Fr Sa Su week day Need a 2 dimensional array: 4 rows times 7 columns double[][] rainfall = new double[4][7]; rainfall[1][2]=2.9; //index starts at 0

CSC 142 K 3 Understanding 2D arrays  rainfall is a 2D array made of 4 rows and 7 columns  A 2D array is a 1D array whose elements are 1D arrays  rainfall is a 1D array of length 4 (4 rows). Each element of rainfall (rainfall[0], …, rainfall[3]) is a 1D array of length 7.  rainfall.length is 4  rainfall[i].length is 7 (for i=0,1,2 or 3)

CSC 142 K 4 Another example (1)  Process the daily rainfall tabulated per month January February... month day Number of columns: 31 for Jan, 28 for Feb, etc … 1 st option: overdimension the array double[][] rainfall=new double[12][31]; 2 nd option: each row has the right number of columns double[][] rainfall = new double[12][]; rainfall[0] = new double[31];//January rainfall[1] = new double[28];//February

CSC 142 K 5 Another example (2)  Compute and print the monthly average rainfall public double average(double[] a) { double avg = 0; for (int i=0; i<a.length; i++) avg += a[i]/a.length; return avg; } //in some other method for(int i=0; i<rainfall.length; i++) System.out.println("Month "+(i+1)+ ": " + average(rainfall[i]));

CSC 142 K 6 2D arrays initialization  As for 1D arrays, the default value is 0 for numbers (double, int, …), '\0' for chars, false for booleans and null for objects.  To initialize a 2D array  element by element, e.g. a[2][3] = 4  all at once (when declaring the array) int[][] a = {{1,2},{3},{1,2,3,4}}; //1 st row has 2 elements: 1 and 2 // 2 nd row has 1 element: 3 // 3 rd row has 4 elements: 1,2,3 and 4 //a[0].length is 2, a[1].length is 1 //a[2].length is 4, a.length is 3

CSC 142 K 7 2D array of objects  Create a game such as minesweeper  The game board is made of squares  Create a Square class (with methods to handle a mouse click, …)  Create a Game class, that has a 2D array of Squares to represent the game board. //10 by 10 Squares Square[][] gameBoard=new Square[10][10]; // Initialize the game board for(int i=0; i<gameBoard.length; i++) for(int j=0; j<gameBoard[i].length; j++) gameBoard[i][j]=new Square();

CSC 142 K 8 Memory view of a 2D array Color[][] palette = new Color[2][]; palette[1] = new Color[4]; palette[1][2] = new Color(12,18,122);//dark blue // how is palette stored in memory? palette null Color object

CSC 142 K 9 Higher dimensions  Can have arrays of any dimensions  think of them as array of arrays of arrays… // A 4 dimensional array of integers int[][][][] a = new int[3][4][6][8];  e.g., useful when doing mathematical computations with tensors (T klmn )