Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.
Warm up Determine the value stored in each of the following: – gradeTable[ 0 ][ 0 ] – gradeTable[ 1 ][ 1 ] – gradeTable[ 3 ][ 4 ] – gradeTable[ 5 ][ 2.
Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
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.
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.
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. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Chapter 9: Arrays and Strings
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
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.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Lecturer : Sakuni Sellapperuma. Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
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.
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.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
2D Arrays A multi dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] gradeTable = new int[7][5]; With the.
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.
Arrays.
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.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
ARRAYS Multidimensional realities Image courtesy of
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to programming in java Lecture 21 Arrays – Part 1.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
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.
Two-Dimensional Arrays
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
ECE Application Programming
Yong Choi School of Business CSU, Bakersfield
Introduction To Programming Information Technology , 1’st Semester
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Multidimensional array
Arrays Week 2.
Multi-Dimensional Arrays
Presentation transcript:

Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3

Table of Student Grades Imagine a class of 7 students that have a quiz every week for 5 weeks. The instructor records the grades in a table. A particular cell of the table is designated by student number and week number. For example: – The grade for student 0 week 1 is 42 – The grade for student 3 week 4 is 93 – The grade for student 6 week 2 is 78

Table of Student Grades (Cont…) A compact notation for specifying a cell uses the row and column indexes like this: gradeTable[ row ][ col ] For example: – gradeTable[ 0 ][ 1 ] is 42 – gradeTable[ 3 ][ 4 ] is 93 – gradeTable[ 6 ][ 2 ] is 78

2D Arrays in Java Rows are numbered from 0 to N-1, where N is the number of rows Columns are numbered from 0 to M-1, where M is the number of columns. A 2D array with N rows and M columns will have N times M number of cells. However, it is possible for a 2D array to have a different number of cells in each row.

2D Array Declaration The declaration: – int[][] myArray ; – says that myArray is expected to hold a reference to a 2D array of int. Without any further initialization, it starts out holding null. The declaration: – int[][] myArray = new int[3][5] ; – All the cells of the array are initialized to zero. The declaration: – int[][] myArray = { {8,1,2,2,9}, {1,9,4,0,3}, {0,3,0,0,7} }; creates an array of the same dimensions (same number of rows and columns) as the previous array and initializes the cells to specific values.

2D Array of int

Different Numbers of Cells per Row Each row of a 2D array may have a different number of cells. In the following example, the array uneven has – 3 cells in its first row, – 2 in its second row, – and 5 in its last row.

Length of a 2D Array The length of a 2D array is the number of rows it has. What does the program print out? Answer: 3

Length of each Row Each row of a 2D array can have a different number of cells, so each row has its own length:

Printing a 2D Array

Review Given the following: int [][] myArray = new int [4][5]; The array myArray has ____ rows and ____ columns. a)4,4 b)5,4 c)4,5 d)5,5

Review (Cont…) Which of the following is not a valid two- dimensional array definition? a)Date [][] ex = new Date [2][100]; b)long [] yo = new long [3][4]; c)String [][] thing = new String [10][10]; d)float [][] why = new float [1][1]; e)double [][] d = new double [1][2];

Review (Cont…) Which of the following array definitions defines an array with three rows and two columns? a)int [][] x = {{5, 5}, {5, 5}}; b)int [][] x = {{5, 5, 5}, {5, 5, 5}}; c)int [][] x = {{5, 5}, {5, 5}, {5, 5}}; d)int [][] x = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}; e)int [][] x = {{5, 5, 5}, {5, 5, 5}, {5, 5, 5}};

Review (Cont…) Given the following: double [][] array = {{3.4, 6.2, 8.1, 9.3, 7.2}, {5.5, 2.9, 8.5, 9.8, 1.1}}; What is array[1].length? a)The number of columns in row 1. b)The number of rows in the array. c)An error. d)The number of columns in the array. e)The number of columns in row 0.

Review (Cont…) Given the following: int[][] items = { {0, 1, 3, 4}, {4, 3, 99, 0, 7 }, {3, 2} } ; Which of the following statements replaces the 99 with 77? a)items[1][2] = 77; b)items[2][1] = 77; c)items[ 99 ] = 77; d)items[2][3] = 77;

Review (Cont…) Examine the following: double[][] values = { {1.2, 9.0, 3.2}, {9.2, 0.5, 1.5, -1.2}, {7.3, 7.9, 4.8} } ; what is in values[3][0] ? a)7.3 b)7.9 c)9.2 d)There is no such array element.

Practice Question Write a program which calculates the sum of all the values in the following array. double [][] array = {{3.4, 6.2, 8.1, 9.3, 7.2}, {5.5, 2.9, 8.5, 9.8, 1.1}, {1.5, 2.9, 3.2, 7.1, 5.8}};

Homework Assignment # 3 (Total marks: 5) Submission deadline: 19 th Nov 2013 (More details: See Lecture 22)