Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays

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

Multidimensional Arrays. CSE 114, Computer Science 1 Stony Brook University

Chapter 7 Multidimensional Arrays

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

Chapter 8 Multi-Dimensional Arrays

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

15. Multidimensional Arrays

Arrays. Eng. Mohammed Abdualal

Chapter 8 Multidimensional Arrays

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

Computer Programming: C++

Computer Programming, I. Laboratory Manual. Final Exam Solution

Computer Programming: C++

Lecture #8-10 Arrays

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Computer Programming: C++

Instructor: Eng.Omar Al-Nahal

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

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Declaring and ini,alizing 2D arrays

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Learning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods

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.

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Spring 2010 Java Programming

Computer Programming, I. Laboratory Manual. Experiment #5. Strings & Text Files Input

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

CSC 1051 Arrays - Review questions

How to declare an array in C?

CS115 Principles of Computer Science

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Give one example where you might wish to use a three dimensional array

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

Combinational Digital Design. Laboratory Manual. Experiment #3. Boolean Algebra Continued

Object Oriented Programming. Java-Lecture 6 - Arrays

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Arrays in Java Multi-dimensional Arrays

Two-Dimensional Arrays

Programming with Java

Inf1-OOP. Arrays. Many Variables of the Same Type. Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin

Introduction to Algorithms and Data Structures

Inf1-OOP. Arrays 1. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF - 1

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Chapter 6 Arrays, Part D 2d Arrays and the Arrays Class

Linked Structures - Review Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Quadratic: the time that it takes to sort an array is proportional to the. square of the number of elements.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Lecture 5: Methods CS2301

Arrays. Arrays. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

Copyright Y. Daniel Liang 1. Here are the hints for selected Java REVEL Programming Projects.

Arrays. Inf1-OP. Arrays. Many Variables of the Same Type. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein

Inf1-OP. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Array. Array Declaration:

COMPUTER SCIENCE. 4. Arrays. Section 1.4.

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

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

System.out.printf("Please, enter the value of the base : \n"); base =input.nextint();

CS 231 Data Structures and Algorithms Fall Arrays Lecture 07 - September 19, Prof. Zadia Codabux

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

Chapter 9 Introduction to Arrays. Fundamentals of Java

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

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

Over and Over Again GEEN163

Computer Science is...

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Two Dimensional Arrays

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Problem Solving With Loops

CS 101 Spring 2007 Midterm 2 Name: ID:

Matrices. Jordi Cortadella Department of Computer Science

Lecture 2. Two-Dimensional Arrays

Inf1-OP. Arrays 1. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. January 30, School of Informatics

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

final int CHICAGO = 0; final int BOSTON = 1; final int MIAMI = 4;

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Problems with simple variables

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

A+ Computer Science MATRICES

Handout 4 Conditionals. Boolean Expressions.

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

Example. Generating random numbers. Write a program which generates 2 random integers and asks the user to answer the math expression.

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Reading Input from Text File

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

Fundamental of Programming (C)

Eng. Mohammed S. Abdualal

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

AP Computer Science A Unit 7. Notes on Arrays

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

STUDENT LESSON A12 Iterations

1. What does the following code fragment write to the monitor?

Top-down programming design

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Transcription:

Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #9 Multi-Dimensional Arrays

What are Multi-Dimensional Arrays? The preceding chapter introduced how to use one-dimensional arrays to store linear collections of elements. You can use a two-dimensional array to store a matrix or a table. For example, the following table that lists the distances between cities can be stored using a two-dimensional array named distances. double[][] distances = { {0, 983, 787, 714, 1375, 967, 1087, {983, 0, 214, 1102, 1763, 1723, 1842, {787, 214, 0, 888, 1549, 1548, 1627, {714, 1102, 888, 0, 661, 781, 810, {1375, 1763, 1549, 661, 0, 1426, 1187, {967, 1723, 1548, 781, 1426, 0, 239, {1087, 1842, 1627, 810, 1187, 239, 0, ; Declaring and creating Variables of Two-Dimensional Arrays Multi-Dimensional array is an array of arrays. For example, to declare two-dimensional array, use the following syntax: int[][] matrix; To initialize the matrix, use the new keyword. Example: create a new matrix with 5 rows and 5 columns: 2

int[][] matrix = new int[5][5]; The previous code will create the matrix and fill it with the default value: 0. To assign the value 7 to a specific element at row 2 and column 1, you can use the following syntax: matrix[2][1] = 7; The matrix will be represented in memory as array of arrays, as following: You can also use an array initializer to declare, create, and initialize a two-dimensional array. 3

Obtaining the Lengths of Two-Dimensional Arrays A two-dimensional array is actually an array in which each element is a one-dimensional array. The length of an array x is the number of elements in the array, which can be obtained using x.length. x[0], x[1],..., and x[x.length-1] are arrays. Their lengths can be obtained using x[0].length, x[1].length,..., and x[x.length-1].length For example, suppose x = new int[3][4], x[0], x[1], and x[2] are one-dimensional arrays and each contains four elements. x.length is 3, and x[0].length, x[1].length, and x[2].length are 4. Ragged Arrays Each row in a two-dimensional array is itself an array. Thus, the rows can have different lengths. An array of this kind is known as a ragged array. Here is an example of creating a ragged array: 4

If you don t know the values in a ragged array in advance, but do know the sizes say, the same as before you can create a ragged array using the following syntax: int[][] trianglearray = new int[5][]; trianglearray[0] = new int[5]; trianglearray[1] = new int[4]; trianglearray[2] = new int[3]; trianglearray[3] = new int[2]; trianglearray[4] = new int[1]; Processing Two-Dimensional Arrays Nested for loops are often used to process a two-dimensional array. Suppose an array matrix is created as follows: int[][] matrix = new int[10][10]; The following are some examples of processing two-dimensional arrays. Initializing arrays with input values. int[][] matrix = new int[10][10]; Scanner input = new Scanner(System.in); System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns: "); for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = input.nextint(); Printing arrays: int[][] matrix = new int[10][10]; for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { System.out.print(matrix[row][column] + " "); System.out.println(); 5

Summing all elements. int[][] matrix = new int[10][10]; int total = 0; for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++){ total += matrix[row][column]; Summing elements by column. for (int column = 0; column < matrix[0].length; column++) { int total = 0; for (int row = 0; row < matrix.length; row++) total += matrix[row][column]; System.out.println("Sum for column " + column + " is " + total); Random shuffling int[][] matrix = new int[10][10]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { int i1 = (int) (Math.random() * matrix.length); int j1 = (int) (Math.random() * matrix[i].length); int temp = matrix[i][j]; matrix[i][j] = matrix[i1][j1]; matrix[i1][j1] = temp; Passing Two-Dimensional Arrays to Methods You can pass a two-dimensional array to a method just as you pass a one-dimensional array. You can also return an array from a method. When passing a two-dimensional array to a method, the reference of the array is passed to the method. 6

Ex: Write a method to add two matrices. The header of the method is as follows: public static double[][] addmatrix(double[][] a, double[][] b) Solution: public static double[][] addmatrix(double[][] m1, double[][] m2) { double[][] result = new double[m1.length][m1[0].length]; for (int i = 0; i < result.length; i++) for (int j = 0; j < result[i].length; j++) result[i][j] = m1[i][j] + m2[i][j]; return result; Multidimensional Arrays A two-dimensional array consists of an array of one-dimensional arrays and a threedimensional array consists of an array of two-dimensional arrays. double[][][] scores = new double[6][5][2]; You can also use the short-hand notation to create and initialize the array as follows: double[][][] scores = { {{7.5, 20.5, {9.0, 22.5, {15, 33.5, {13, 21.5, {15, 2.5, {{4.5, 21.5, {9.0, 22.5, {15, 34.5, {12, 20.5, {14, 9.5, {{6.5, 30.5, {9.4, 10.5, {11, 33.5, {11, 23.5, {10, 2.5, {{6.5, 23.5, {9.4, 32.5, {13, 34.5, {11, 20.5, {16, 7.5, {{8.5, 26.5, {9.4, 52.5, {13, 36.5, {13, 24.5, {16, 2.5, {{9.5, 20.5, {9.4, 42.5, {13, 31.5, {12, 20.5, {16, 6.5; 7

Lab Work Ex1: Write two methods that return the sum of columns and rows individually and put the result in one-dimensional array for each result using the following methods header: public static int[] sumrows (int[][] array) public static int[] sumcolumns (int[][] array) Solution: public static int[] sumrows(int[][] array) { int[] result = new int[array.length]; int tempsum = 0; int i = 0; for (int row = 0; row < array.length; row++) { for (int column = 0; column <array[0].length; column++){ tempsum += array[row][column]; result[i] = tempsum; i++; tempsum = 0; return result; public static int[] sumcolumns(int[][] array) { int[] result = new int[array[0].length]; int tempsum = 0; int i = 0; for (int column = 0; column < array[0].length; column++) { for (int row = 0; row < array.length; row++) { tempsum += array[row][column]; result[i] = tempsum; i++; tempsum = 0; return result; 8

Ex2: Write a method that takes two int arrays as parameters and returns the multiplication of the two arrays. Solution: public static int[][] multiplicar(int[][] A, int[][] B) { int arows = A.length; int acolumns = A[0].length; int brows = B.length; int bcolumns = B[0].length; if (acolumns!= brows) { System.err.println("A:Rows: " + acolumns + " did not match B:Columns " + brows + "."); return null; int[][] C = new int[arows][bcolumns]; for (int i = 0; i < arows; i++) { // arow for (int j = 0; j < bcolumns; j++) { // bcolumn for (int k = 0; k < acolumns; k++) { // acolumn C[i][j] += A[i][k] * B[k][j]; return C; Ex3: In this question, you have n * n matrix as shown below, you have to replace each 0 in the matrix to the number of * around it, then print the result. char[][] input = { {'0', '0', '*', '0', '*', {'*', '*', '*', '0', '0', {'0', '*', '0', '*', '0', {'*', '0', '*', '0', '0', {'0', '*', '*', '0', '*', ; 9

2 4 * 3 * * * * 4 2 4 * 5 * 1 * 5 * 4 2 2 * * 3 * Solution: char[][] input = { {'0', '0', '*', '0', '*', {'*', '*', '*', '0', '0', {'0', '*', '0', '*', '0', {'*', '0', '*', '0', '0', {'0', '*', '*', '0', '*', ; int[][] output = new int[input.length][input[0].length]; for (int i = 0; i < input.length; i++) { for (int j = 0; j < input[0].length; j++) { if (input[i][j] == '*') { if (j > 0) output[i][j - 1]++; if (i > 0 && j > 0) output[i - 1][j - 1]++; if (i > 0) output[i - 1][j]++; if (i > 0 && j < input.length - 1) output[i - 1][j + 1]++; if (j < input.length - 1) output[i][j + 1]++; if (i < input.length - 1 && j < input.length- 1) output[i + 1][j + 1]++; if (i < input.length - 1) output[i + 1][j]++; if (i < input.length - 1 && j > 0) output[i + 1][j - 1]++; 10

for (int i = 0; i < input.length; i++) { for (int j = 0; j < input[0].length; j++) { if (input[i][j] == '*') { System.out.print('*' + " "); else System.out.print(output[i][j] + " "); System.out.println(); Homework 1. Write a method that sums all the numbers in the major diagonal in an n * n matrix of double values using the following header: public static double summajordiagonal(double[][] m) 2. Write a program that generates a 6-by-6 two-dimensional matrix filled with 0s and 1s, displays the matrix, and prints whether every row and every column have an even number of 1s or not. Good Luck 11