Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Similar documents
Chapter 9 Introduction to Arrays. Fundamentals of Java

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Last Class. More on loops break continue A bit on arrays

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Arrays. Theoretical Part. Contents. Keywords. Programming with Java module 3

Lecture Transcript While and Do While Statements in C++

Arrays in Java Multi-dimensional Arrays

Computer Programming

15. Multidimensional Arrays

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

Chapter 7 Multidimensional Arrays

TOPICS TO COVER:-- Array declaration and use.

Introduction to Object-Oriented Programming

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Two Dimensional Arrays

The for Loop. Lesson 11

Computer Programming: C++

Arrays. Eng. Mohammed Abdualal

DATA STRUCTURES USING C

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Chapter 7: Arrays and the ArrayList Class

Review of Important Topics in CS1600. Functions Arrays C-strings

APCS Semester #1 Final Exam Practice Problems

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

OCA Java SE 7 Programmer I Certification Guide By Mela Gupta. Arrays

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

All answers will be posted on web site, and most will be reviewed in class.

Lecture 5: Arrays. A way to organize data. MIT AITI April 9th, 2005

Array. Array Declaration:

CP122 CS I. Iteration

Arrays. Chapter 7 Part 3 Multi-Dimensional Arrays

Arrays and Collections. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Computer Science II (20082) Week 1: Review and Inheritance

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CMSC131. Seating Chart

C Programming for Engineers Structured Program

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

9/19/2018 Programming Data Structures. Polymorphism And Abstract

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Lab Session # 6 Functions. ALQUDS University Department of Computer Engineering

CS 230 Programming Languages

Computer Programming

ECE 122. Engineering Problem Solving with Java

Arrays. Array Basics. Chapter 8 Spring 2017, CSUS. Chapter 8.1

Repetition Structures

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

STUDENT LESSON A12 Iterations

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Object Oriented Programming. Java-Lecture 6 - Arrays

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

How to declare an array in C?

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

Chapter 3: Arrays and More C Functionality

Computer Science 210 Data Structures Siena College Fall 2018

CS159. Nathan Sprague. September 11, 2015

Mobile App:IT. Methods & Classes

CS1150 Principles of Computer Science Methods

Example: Computing prime numbers

Data dependent execution order data dependent control flow

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

Array Basics: Outline

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;

Full file at

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

Chapter 8 Multi-Dimensional Arrays

Module 201 Object Oriented Programming Lecture 10 - Arrays. Len Shand

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CS110D: PROGRAMMING LANGUAGE I

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

9/24/2018 Programming Data Structures

COMP 202. Programming With Arrays

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Expressions and Casting

CS2102, D15 Exam 1. Name:

H212 Introduction to Software Systems Honors

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

CS 61B Discussion Quiz 1. Questions

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example:

JVM (java) compiler. A Java program is either a library of static methods (functions) or a data type definition

Visual C# Instructor s Manual Table of Contents

C++ Arrays and Vectors

Chapter 12 Two-Dimensional Arrays

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Transcription:

Arrays: Higher Dimensional Arrays CS0007: Introduction to Computer Programming

Review If the == operator has two array variable operands, what is being compared? The reference variables held in the variables. How do you compare two arrays to see if they hold the same elements? Iterate through the arrays and compare them element by element. How are the sum and average array algorithms similar? Both iterate through the array and add each element to an accumulator variable. How are the minimum and maximum element algorithms similar? The current minimum or maximum element is compared to each element, if an element is smaller (minimum case) or larger (maximum case) than the current minimum or maximum, the element replaces it. If you intend to use a partially filled array, what information must be kept in addition to the array itself? The current number of elements in the array How does the Sequential Search Algorithm work? Look at every element in the array starting at the first element and check if it is the target value.

Two Dimensional Arrays Imagine now we want to keep 10 test scores for 30 students. How would we represent this in a program with what we ve learned? Answer: You would need either 30 10-element arrays or 10 30-element arrays. This was the problem arrays were supposed to solve! What we have been working with so far are considered one dimensional arrays, because they only extend along one dimension (one set of data). Java also allows for higher dimensional arrays, for example, two dimensional arrays. Two dimensional arrays can be though of having a table of rows and columns in memory. So, a better solution to the above question is what? Answer: Have a 30 x 10 or a 10 x 30 two dimensional array!

Two Dimensional Array Example New Topics: Two Dimensional Arrays

Two Dimensional Arrays Declaring a two dimensional array is very similar to declaring a one dimensional array. For example, to declare an integer array variable with 3 rows and 4 columns called numbers you would: int[][] numbers = new int[3][4]; The two sets of brackets on the left side of the assignment operator indicates the variable is a reference variable to a two dimensional array. The [3] on the right side indicates the array object will have 3 rows. The [4] on the right side indicates the array object will have 4 columns. Each array element has two indices, one indicating the row and one the column. For example, to access the element in the first row and third column of the above declared numbers array you would: numbers[0][2]

Two Dimensional Arrays To initialize a two dimensional array you encapsulate each row in curly brackets, separating elements by commas. You separate each row by commas and put the entire array in curly brackets: int[][] numbers = {{1,2,3}, {4,5,6}, {7,8,9}}; or int[][] numbers = {{1,2,3}, {4,5,6}, {7,8,9}}; Another way to think of two dimensional arrays is that a two dimensional is a one dimensional array of one dimensional arrays. Said another way, it is a reference variable to an array that holds reference variables to arrays. This is apparent when using the length data member in two dimensional arrays: int[][] numbers = new int[3][4]; numbers.length the number of rows numbers[0].length the number of columns in the first row Example: TwoDimLengths.java

Class Programming Practice: ClassScores.java Create a program where a user can enter 3 students scores on 3 quizzes. Then, the program should tell the user the average for each student, each quiz, and overall class average for all quizzes.

Ragged Arrays Since the rows of a two dimensional array are arrays themselves, they can have different lengths: When the rows of a two dimensional array are of different lengths, the array is known as a ragged array. int[][] ragged = new int[4][]; ragged[0] = new int[3]; ragged[1] = new int[4]; ragged[2] = new int[5]; ragged[3] = new int[6]; Example: RaggedArray.java

Higher Dimensional Arrays Java does not impose a limit on how many dimensions an array can be. For instance we can create a three dimensional array: int[][][] numbers = new int[2][3][4]; You can have higher dimensional arrays by simply defining the size of the dimension. You can also have ragged arrays of higher dimensions. For instance you can have a three dimensional array with 3 twodimensional arrays of sizes 3x3, 4x5, and 2x6.