1 / 30

COP3502 Programming Fundamentals for CIS Majors 1

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.

ozzy
Télécharger la présentation

COP3502 Programming Fundamentals for CIS Majors 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

  2. Chapter 6 • Array • Single dimensional array • Search & sort

  3. Chapter 7 • Multi-dimensional arrays • Declare • Create • Access • Problem solving using two dimensional array • Sudoku • …

  4. Multi-Dimensional Arrays

  5. We want to store the distances between 20 different cities. • How?

  6. We want to store the position on a checkers’ board. • How?

  7. // 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];

  8. // 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

  9. 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

  10. 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.

  11. String[] pieces = {“king”, “queen”, “rook”, “knight”, “bishop”, “pawn”}; int[][] chessBoard= new int[8][8] • We want to store the position on a chess board.

  12. matrix[2][1] =7; • 2-dimensional array illustration • Accessing elements 0 1 2 3 4 0 1 2 3 4

  13. int [][] nums = new int[5][4]; • Internally, java stores a 2D array as “an array of arrays” nums

  14. What is nums.length? • 5 • What is nums[0].length? • 4 int [][] nums = new int[5][4]; nums

  15. int[][] matrix2 = { {1, 2, 3}, {10, 3, 0}, {10, 7, 80} }; 0 1 2 • 2-dimensional array illustration • Initialization 0 1 2 matrix2

  16. int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} }; • Rows might have different lengths matrix

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. Passing 2D array to a method PassTwoDimensionalArray Run

  25. Write a program that grades multiple-choice test. GradeExam Run

  26. Finding two points nearest to each other Run FindNearestPoints

  27. 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

  28. Write a program to checking a Sudoku solution CheckSudokuSolution Run

  29. In Java, you can create n-dimensional arrays for any integer n. • Generalization of 2D case • Example double[][][] scores = new double[10][5][2];

  30. 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

More Related