Chapter 8 - Multidimensional Arrays PDF

Document Details

Uploaded by Deleted User

Taibah University

Dr. Tawfik Ismail

Tags

multidimensional arrays java programming computer programming

Summary

This document is about multidimensional arrays in Java programming. It includes examples and explanations for 1D, 2D, and 3D arrays. The document was created by Dr. Tawfik Ismail for Taibah University's GE209 Computer Programming course.

Full Transcript

GE209 Computer Programming for Engineers Chapter 8: Multidimensional Arrays Dr. Tawfik Ismail Professor in Telecommunication Engineering College of Engineering Email: [email protected] xD Array 3D Array...

GE209 Computer Programming for Engineers Chapter 8: Multidimensional Arrays Dr. Tawfik Ismail Professor in Telecommunication Engineering College of Engineering Email: [email protected] xD Array 3D Array 2D Array 1D Array 2 Two-Dimensional Array ▪ A two-dimensional array stores data in a grid format, consisting of rows and columns. It stores elements of the same type. 1. Declaring a Two-Dimensional Array // Syntax → dataType[][] arrayName; // Example → int[][] matrix; // Declares a 2D array of integers → double[][] myList; // mylist is a 2D double array index 0 1 2 3 2. Creating and Initializing a Two-Dimensional Array 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 → int[][] matrix = new int; // matrix is an integer 2D array with 3 rows and 4 columns, each initialized to 0. index 0 1 2 3 → int[][] matrix = { {10, 20, 30, 40}, 0 10 20 30 40 {50, 60, 70, 80}, 1 50 60 70 80 2 90 100 110 120 {90, 100, 110, 120} }; // matrix is an integer 2D array with 3 rows and 4 columns, initializing with specific values 3 Two-Dimensional Array 3. Accessing Array Elements index 0 1 2 3 0 35 20 30 40 → matrix = 35; // Sets the first element in the matrix to 35 1 50 60 70 80 2 90 100 110 150 → matrix = 150; // Sets the element at row 2, column 3 to 150 public class Test { public static void main(String[] args) { int[][] matrix = { {10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120} }; for (int i = 0; i < matrix.length; i++) { // Outer loop: rows for (int j = 0; j < matrix[i].length; j++) { // Inner loop: columns System.out.print(matrix[i][j] + " "); } System.out.println(); // Move to the next row } } } → matrix.length is a method return an integer with the value of array rows length 4 → matrix.length[i] is a method return an integer with the value of array column length of [i] Write a program that calculates the sum and average of all elements in a 2D array: public class SumAndAverage2DArray { public static void main(String[] args) { int[][] matrix = { {10, 20, 30}, {40, 50, 60}, {70, 80, 90} }; int sum = 0; // To store the sum of elements int count = 0; // To count the total number of elements for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { sum += matrix[i][j]; // Add each element to the sum count++; } } // Calculate the average double average = (double) sum / (double) count; // Display the results System.out.println("Sum of all elements: " + sum); System.out.println("Average of all elements: " + average); } } 5 Passing 2D Array to Method import java.util.Scanner; public class PassTwoDimensionalArray { public static void main(String[] args) { int[][] m = getArray(); System.out.println("\nSum of all elements is " + sum(m)); } // Display sum of elements public static int[][] getArray() { Scanner input = new Scanner(System.in); int[][] m = new int; System.out.println("Enter " + m.length + " rows and " + m.length + " columns: "); for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[i].length; j++) { m[i][j] = input.nextInt(); } } return m; } public static int sum(int[][] m) { int total = 0; for (int row = 0; row < m.length; row++) { for (int column = 0; column < m[row].length; column++) { total += m[row][column]; } } return total; } } 6 Ragged Arrays ▪ A ragged array (jagged array) is a two-dimensional array where the rows have different lengths. // Syntax int[][] raggedArray = { {10, 20}, // First row with 2 elements {30, 40, 50, 60}, // Second row with 4 elements index 0 1 2 3 0 10 20 {70, 80, 90} // Third row with 3 elements 1 30 40 50 60 }; 2 70 80 90 public class Test { public static void main(String[] args) { int[][] raggedArray = { {10, 20}, {30, 40, 50, 60}, {70, 80, 90} }; for (int i = 0; i < raggedArray.length; i++) { // Outer loop for rows for (int j = 0; j < raggedArray[i].length; j++) { // Inner loop for columns System.out.print(raggedArray[i][j] + " "); } System.out.println(); } } } 7 Three-dimensional Arrays ▪ A three-dimensional (3D) array is an array of 2D arrays. It organizes data in three dimensions referred to blocks, rows, and columns. It provides a cube-like structure for data storage. → int[][][] threeDArray = new int[blocks][rows][columns]; → int[][][] threeDArray = new int; → int[][][] threeDArray = { { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }, // block 1 { {13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24} } // block 2 }; 8 Write a program uses a 3D array to store the GPAs of 3 students across 4 courses in 2 semesters. The student IDs starting from 1001 and course IDs starting from 001. Each score is out of 4. import java.util.Scanner; public class StudentGrades3D { public static void main(String[] args) { int noStudents = 3; int noCourses = 4; int noSemesters = 2; double[][][] grades = new double [noSemesters][noStudents][noCourses]; // Student IDs and Course IDs int[] studentIDs = new int[noStudents]; int[] courseIDs = new int[noCourses]; // Initialize student IDs (starting from 1001) for (int i = 0; i < noStudents; i++) { studentIDs[i] = 1001 + i; } // Initialize course IDs (starting from 001) for (int i = 0; i < noCourses; i++) { courseIDs[i] = 1 + i; } 9 // Fill the grades array with values (0.0-4.0) Scanner input = new Scanner(System.in); for (int semester = 0; semester < noSemesters; semester++) { for (int student = 0; student < noStudents; student++) { System.out.println("Enter the " + noCourses + " courses GPAs (0 - 4) for Student " + (1001 + student) + " in the Semester " + (1 + semester) + ": "); for (int course = 0; course < noCourses; course++) { grades[semester][student][course] = input.nextDouble(); } } } // Display the grades and calculate overall averages for (int student = 0; student < noStudents; student++) { double totalGradeSum = 0; // Sum of all grades for this student int gradeCount = 0; // Total number of grades for this student System.out.println("Student ID: " + studentIDs[student]); for (int semester = 0; semester < noSemesters; semester++) { System.out.println(" Semester " + (semester + 1) + ":"); for (int course = 0; course < noCourses; course++) { double grade = grades[semester][student][course]; System.out.println(" Course " + String.format("%03d", courseIDs[course]) + ": " + grade); // Add grade to total and increase the count totalGradeSum += grade; gradeCount++; } } 10 // Calculate and display the overall average double overallAverage = totalGradeSum / gradeCount; System.out.println(); System.out.printf(" Overall Average for Student %d: %.2f%n", studentIDs[student], overallAverage); System.out.println(); } } } 11 12 Assignment #8 ▪ Answer the following problems on Chapter 8, page (308) 1) Problem 8.1 (page 308) 2) Problem 8.4 (page 308) 3) Problem 8.6 (page 309) 4) Problem 8.13 (page 312) 5) Problem 8.21 (page 316) 6) Problem 8.28 (page 318) Lab Assignment #8 ▪ Answer the following problems on Chapter 8 1) Problem 8.2.2 (page 293) [F, F, T, T, T, T] 2) Problem 8.2.5 (page 293) 3) Problem 8.3.1 (page 295) 4) Problem 8.3.2 (page 295) 5) Problem 8.4.1 (page 296) 6) Problem 8.8.3 (page 307)

Use Quizgecko on...
Browser
Browser