300 likes | 417 Views
COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi. Chapter 6 Array Single dimensional array Search & sort. Chapter 7 Multi-dimensional arrays Declare Create Access Problem solving using two dimensional array Sudoku …. Multi-Dimensional Arrays.
E N D
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
Chapter 6 • Array • Single dimensional array • Search & sort
Chapter 7 • Multi-dimensional arrays • Declare • Create • Access • Problem solving using two dimensional array • Sudoku • …
We want to store the distances between 20 different cities. • How?
// Declare array refVar of type dataType dataType[][] refVar; // Create array refVar = newdataType[size1][size2]; • Declaring and creating 2-dimensional array • Example // Declare array double[][] myTable; // Create array myTable= newdouble[10][10];
// Combine declaration and creation in one // single statement double[][] myTable= newdouble[10][10]; // Alternative syntax doublemyTable[][] = newdouble[10][10]; • Declaring and creating 2-dimensional array
int[][] matrix = new int[5][5] • 2-dimensional array illustration • Declaring and creating an array 0 1 2 3 4 0 1 2 3 4
final intBLANK = 0; // location empty final intWHITE = 1;// white piece final intRED = 2; // blue piece int[][] checkerBoard= new int[8][8] • We want to store the position on a checkers’ board.
String[] pieces = {“king”, “queen”, “rook”, “knight”, “bishop”, “pawn”}; int[][] chessBoard= new int[8][8] • We want to store the position on a chess board.
matrix[2][1] =7; • 2-dimensional array illustration • Accessing elements 0 1 2 3 4 0 1 2 3 4
int [][] nums = new int[5][4]; • Internally, java stores a 2D array as “an array of arrays” nums
What is nums.length? • 5 • What is nums[0].length? • 4 int [][] nums = new int[5][4]; nums
int[][] matrix2 = { {1, 2, 3}, {10, 3, 0}, {10, 7, 80} }; 0 1 2 • 2-dimensional array illustration • Initialization 0 1 2 matrix2
int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} }; • Rows might have different lengths matrix
for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { // use matrix[row][column] } } • Loops and 2D arrays
java.util.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(); } } • Initializing 2D array with input values
for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = (int)(Math.random() * 100); } } • Initializing 2D arrays with random values
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(); } • Printing 2D arrays
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 all elements of a 2D array
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); } • Summing elements of a 2D array by each column
for (inti = 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); // Swap matrix[i][j] with matrix[i1][j1] int temp = matrix[i][j]; matrix[i][j] = matrix[i1][j1]; matrix[i1][j1] = temp; } } • Random shuffling of a 2D array
Passing 2D array to a method PassTwoDimensionalArray Run
Write a program that grades multiple-choice test. GradeExam Run
Finding two points nearest to each other Run FindNearestPoints
Sudoku • Every column contains the numbers 1 to 9 • Every row contains the numbers 1 to 9 • Every 3×3 box contains the numbers 1 to 9
Write a program to checking a Sudoku solution CheckSudokuSolution Run
In Java, you can create n-dimensional arrays for any integer n. • Generalization of 2D case • Example double[][][] scores = new double[10][5][2];
Write a program that calculates the total score for students in a class. Suppose the scores are stored in a three-dimensional array named scores. The first index in scores refers to a student, the second refers to an exam, and the third refers to the part of the exam. Suppose there are 7 students, 5 exams, and each exam has two parts--the multiple-choice part and the programming part. So, scores[i][j][0] represents the score on the multiple-choice part for the i’s student on the j’s exam. Your program displays the total score for each student. TotalScore