Module 02 Multidimensional Arrays
Module 02 Multidimensional Arrays
dataType can be primitive data types like int, char, double, byte, etc. or Java objects
dim is the dimension of the array created
arrayName is an identifier (a variable)
size1 size2 sizen sizes of the dimensions respectively
Allocating arrays in memory:
Two dimensional array:
int[][] twoD_arr = new int[10][20];
Size of multidimensional arrays: The total number of elements that can be stored in a
multidimensional array can be calculated by multiplying the size of all the dimensions.
Example:
The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.
Similarly, array int[][][] x = new int[5][10][20] can store a total of (5*10*20) = 1000 elements.
In the Java multidimensional array, each memory location is associated with two or more
number that represents a particular index/element in the array.
row
Index of 2D array (r,c)
(0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3)
column (2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3)
(4, 0) (4, 1) (4, 2) (4, 3)
A 2D Array
//initialize array
Two dimensional array:
int[][] twoD_arr = new int[10][20];
//Note: In printing two dimensional arrays using looping statements, you need to use
//nested loops. The outer loop is used to traverse the row(s) of the matrix and the
//inner loop is used to traverse the column of the matrix.
//A common practice is to print the contents of the matrix starting from the first
//row (left to right) to the rightmost element of the last row.
Note: we can use num.length to determine the number of rows in the matrix and
num[0].length to determine the number of columns in array num.
Program Sample 3.3: We can rewrite the solution for Program Sample 3.2 in a way that the
elements of array num is displayed in a matrix format
//initialize 2D array num
int[][] num = new int[5][5];
int i,j;
num[0][0] = 10;
num[1][1] = 20;
num[2][2] = 30;
num[3][3] = 40;
num[4][4] = 50;
Program Sample 3.4: Initializing the elements of a matrix using looping statements
int i,j;
int[][] num = new int[5][5];
//the first index to be assigned a value is index (0,0) followed by (0,1), (0,2),
//(0,3), (0,4), (1,0) and so on…
Note: it more efficient to use looping statements in initializing and displaying the contents of arrays
Program Sample 3.5: Allowing the user to enter the elements of an array
import java.util.Scanner;
public class Java_Matrix_04
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int row, col;
System.out.print("\n");
for(int x = 0; x < row; x++)
for(int y = 0; y < col; y++)
{
System.out.print("Enter Element at Index (" + x + "," + y + ") >> ");
matrix[x][y] = input.nextInt();
}
1 2 3
4 5 6
7 8 9
Program Sample 3.6: Create a program that determines the sum and average of all the elements
in a matrix entered by the user. Let the user define the size of the array
Sample Output:
import java.util.Scanner;
public class Java_matrix_05
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int row, col;
float sum,ave;
//compute average
ave = sum / (row * col);
//display result
System.out.print("\nThe sum of elements of the matrix >> " + sum);
System.out.print("\nThe ave of elements of the matrix >> " + ave);
}
}
}
NVSU-FR-ICD-05-00 (081220) Page 8 of 16
“In accordance with Section 185, Fair Use of Copyrighted Work of
Republic Act 8293, the copyrighted works included in this material may
be reproduced for educational purposes only and not for commercial distribution”
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 02: CPE5-23S-2022-2023
Program Sample 3.8: Revise program sample 8 so that it will display the sum(A+ B), difference
(A – B) and product (A * B) of matrix A and B. Allow the user to input the
elements of both matrix.
Sample Output:
Sum >> 10 10 10 10 10 10 10 10 10
Diff >> -8 -6 -4 -2 0 2 4 6 8
Prod >> 9 16 21 24 25 24 21 16 9
import java.util.Scanner;
public class Java_Matrix_07
{
public static void main(String[] args)
{
int A[][] = new int[3][3];
int B[][] = new int[3][3];
int sum[][] = new int[3][3];
int dif[][] = new int [3][3];
int prd[][] = new int [3][3];
Scanner input = new Scanner(System.in);
Program Sample 3.9: Given a square matrix N*N, create a program that will display the contents
of the matrix in a spiral order. The elements of the matrix is shown below.
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Sample Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
public class Java_Matrix_08
{
public static void main(String[] args)
{
int rows,cols,i,j,k;
int Spiral[][] = {
{ 1, 2, 3, 4, 5},
{16, 17, 18, 19, 6},
{15, 24, 25, 20, 7},
{14, 23, 22, 21, 8},
{13, 12, 11, 10, 9}
};
Program Sample 3.10: PARITY BITS: A parity bit is a simple mechanism for detecting errors in data
transmitted over an unreliable connection such as a telephone line. The basic idea is that an additional
bit is transmitted after each group of 8 bits so that a single bit error in the transmission can be detected.
Parity bits can be computed for either even parity or odd parity. If even parity is selected then the parity
bit that is transmitted is chosen so that the total number of one (1) bits transmitted (8 bits of data plus the
parity bit) is even. When odd parity is selected the parity bit is chosen so that the total number of one (1)
bits transmitted is odd.
Write a program that computes the parity bit for groups of 8 bits entered by the user using even or odd
parity. Your program read strings containing 8 bits per line. After each string is entered by the user your
program should display the bits with its corresponding parity bit separated by a dash.
Also, ask the user if he/she wishes to enter another set of bits.
Sample Output
Sample Output
do
{
System.out.print("Enter Number of lines >> ");
lines = sc.nextInt();
String data;
}
}
Given a list of 10 numbers, determine if the twins are there. The Input: The first input line contains a
positive integer, n, indicating the number of data sets to check. The sets are on the following n input lines,
one set per line. Each set consists of exactly 10 single-space-separated distinct integers (each integer
between 11 and 99 inclusive) giving the jersey numbers for the players. Print each input set. Then, on
the next output line, print one of four messages (Mack, Zack, Both, None), indicating how many of the
twins are in the set. Leave a blank line after the output for each test case.
Sample Output
4
11 99 88 17 19 20 12 13 33 44
11 12 13 14 15 16 66 88 19 20
20 18 55 66 77 88 17 33 44 11
12 23 34 45 56 67 78 89 91 18
11 99 88 17 19 20 12 13 33 44
Zack
11 12 13 14 15 16 66 88 19 20
None
20 18 55 66 77 88 17 33 44 11
Both
12 23 34 45 56 67 78 89 91 18
Mack
Sample Output
5
11 12 88 99 65 24 21 22 32 24
12 23 43 23 43 56 32 17 18 92
91 92 93 22 18 22 43 12 22 45
12 65 23 35 62 32 65 86 23 17
17 18 21 34 34 23 43 43 22 32
11 12 88 99 65 24 21 22 32 24
None
12 23 43 23 43 56 32 17 18 92
Both
91 92 93 22 18 22 43 12 22 45
Mack
12 65 23 35 62 32 65 86 23 17
Zack
17 18 21 34 34 23 43 43 22 32
Both
do
{
System.out.println();
rows = sc.nextInt();
System.out.println();
mack = 0;
zack = 0;
System.out.println();
}
VI. EVALUATION (Note: Not to be included in the student’s copy of the IM)
IX. REFERENCES
Books:
Chapman, Davis.: Sams Teach Yourself VISUAL C++ 6 in 21 days., Sams Publishing, 201
West 103rd St., Indianapolis, Indiana, 46290 USA, 1998
Koffman, E.B., and Hanley J.R.: Problem Solving and Program Design in C, 2nd ed.,
Addison-Wesley Publishing Company,1996.
Online Resources:
https://www.techiedelight.com/iterate-over-characters-string-java/
https://www.geeksforgeeks.org/multidimensional-arrays-in-java/
https://www.geeksforgeeks.org/matrix/
https://www.programiz.com/java-programming/arrays
https://www.tutorialspoint.com/java
https://www.w3schools.com/
https://www.learnjavaonline.org/
https://www.programiz.com/java-programming