0% found this document useful (0 votes)
2 views16 pages

Module 02 Multidimensional Arrays

This instructional module from Nueva Vizcaya State University covers the topic of multidimensional arrays in Java programming, focusing on their definition, initialization, and element access. It outlines desired learning outcomes for students, including the ability to define, initialize, and process arrays, and provides various programming examples to illustrate these concepts. The module is intended for 2nd-year students in the Bachelor of Science in Computer Engineering program and spans a time frame of 12 hours.

Uploaded by

dapyoo490
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Module 02 Multidimensional Arrays

This instructional module from Nueva Vizcaya State University covers the topic of multidimensional arrays in Java programming, focusing on their definition, initialization, and element access. It outlines desired learning outcomes for students, including the ability to define, initialize, and process arrays, and provides various programming examples to illustrate these concepts. The module is intended for 2nd-year students in the Bachelor of Science in Computer Engineering program and spans a time frame of 12 hours.

Uploaded by

dapyoo490
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Republic of the Philippines

NUEVA VIZCAYA STATE UNIVERSITY


Bambang, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No. 02: CPE5-23S-2022-2023

College : College of Engineering


Campus : Bambang Campus

DEGREE PROGRAM BSCPE COURSE NO. CPE5


SPECIALIZATION Java Programming COURSE TITLE Data Structures and Algorithms
YEAR LEVEL 2nd Year TIME FRAME 12hrs WK NO. 4-5 IM NO. 02

I. UNIT TITLE/CHAPTER TITLE


Multidimensional Arrays
II. LESSON TITLE
1. What are multidimensional arrays?
2. Defining multidimensional arrays
3. Initializing multidimensional arrays
4. Accessing elements in a multidimensional array
5. Processing multidimensional arrays
III. LESSON OVERVIEW
This lesson covers topics in multidimensional array structures including strings and string
methods. This lesson is focused on single dimensional array structure and how to process the elements
in an array.
IV. DESIRED LEARNING OUTCOMES
1. Students are able to define and initialize arrays.
2. Students will be able to access and process elements of an array.
3. Incorporate control structures with array structures.
V. LESSON CONTENT
1. What are Multidimensional Arrays?
 An multidimensional array is an array with more than one dimension. In a matrix, the two
dimensions are represented by rows and columns.
2. Defining Multidimensional Arrays
 To declare a multidimensional array, define the variable type with square brackets:
Syntax:
data_type[1st dim][2nd dim][]..[nth dim] array_name = new data_type [size1] [size2] …[sizen];

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

Three dimensional array:


int[][][] threeD_arr = new int[10][20][30];

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.

NVSU-FR-ICD-05-00 (081220) Page 1 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
3. Initializing Multidimensional Arrays

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

Three dimensional array:


int[][][] threeD_arr = new int[10][20][30];
Note: Like single dimensional arrays indexes in a multidimensional arrays always start from (0,0)
or (0,0,0). That is, the first element of an array is at (row 0, column 0) or
(row 0, column 0, page 0). If the size of an array is (r * c), then the last element of the array will
be at index (r-1, c-1) or if size is (r * c * p) then last element is at (r-1, c-1, p-1).
4. Accessing Elements in a Multidimensional Array
Program Sample 3.1: Accessing elements in a multidimensional array
//initialize 2D array num
int[][] num = new int[5][5];

//assign elements to specific indexes at array num


num[0][0] = 10;
num[1][1] = 20;
num[2][2] = 30;
num[3][3] = 40;
num[4][4] = 50;

//print selected indexes of array num


System.out.println("Accessing Elements of Array num:");
System.out.println("Element at index (0,0) >> " + num[0][0]);
System.out.println("Element at index (1,1) >> " + num[1][1]);
System.out.println("Element at index (2,2) >> " + num[2][2]);
System.out.println("Element at index (3,3) >> " + num[3][3]);
System.out.println("Element at index (4,4) >> " + num[4][4]);

Output: Program Sample 3.1


Accessing Elements of Array num:
Element at index (0,0) >> 10
Element at index (1,1) >> 20
Element at index (2,2) >> 30
Element at index (3,3) >> 40
Element at index (4,4) >> 50

NVSU-FR-ICD-05-00 (081220) Page 2 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.2: Accessing all elements using looping statements


//initialize 2D array num
int[][] num = new int[5][5];

//assign elements to specific indexes at array num


num[0][0] = 10;
num[1][1] = 20;
num[2][2] = 30;
num[3][3] = 40;
num[4][4] = 50;

//print all elements of array num using for statement


for(int i = 0; i < num.length; i++)
for(int j = 0; j < num[0].length; j++)
System.out.println("Element at index ["+ i + "]" + "["+ j + "]" +" >> " + num[i][j]);

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

//This procedure is similar in printing patterns using looping statements.

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

Output: Program Sample 3.2


Accessing Elements of Array:
Element at index [0][0] >> 10
Element at index [0][1] >> 0
Element at index [0][2] >> 0
Element at index [0][3] >> 0
Element at index [0][4] >> 0
Element at index [1][0] >> 0
Element at index [1][1] >> 20
Element at index [1][2] >> 0
Element at index [1][3] >> 0
Element at index [1][4] >> 0
Element at index [2][0] >> 0
Element at index [2][1] >> 0
Element at index [2][2] >> 30
Element at index [2][3] >> 0
Element at index [2][4] >> 0
Element at index [3][0] >> 0
Element at index [3][1] >> 0
Element at index [3][2] >> 0
Element at index [3][3] >> 40
Element at index [3][4] >> 0
Element at index [4][0] >> 0
Element at index [4][1] >> 0
Element at index [4][2] >> 0
Element at index [4][3] >> 0
Element at index [4][4] >> 50

NVSU-FR-ICD-05-00 (081220) Page 3 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.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;

int row = num.length;


int col = num[0].length;

for(i = 0; i < row; i++)


for(j = 0; j < col; j++)
{
System.out.print( num[i][j] + " " ); // print element
if( j == col – 1 ) // test if j = 4
System.out.print("\n"); // go to next line
}

Output: Program Sample 3.3


10 0 0 0 0
0 20 0 0 0
0 0 30 0 0
0 0 0 40 0
0 0 0 0 50
Note: We only assigned 5 values to the num array. Java will assign the value 0 to the rest of the
Indexes.

Program Sample 3.4: Initializing the elements of a matrix using looping statements
int i,j;
int[][] num = new int[5][5];

int row = num.length;


int col = num[0].length;

for(i = 0; i < row; i++)


for(j = 0; j < col; j++)
{
num[i][j] = i + j;
}

//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…

for(i = 0; i < row; i++)


for(j = 0; j < col; j++)
{
System.out.print( num[i][j] + " " );
if( j == col - 1)
System.out.print("\n");
}

Note: it more efficient to use looping statements in initializing and displaying the contents of arrays

NVSU-FR-ICD-05-00 (081220) Page 4 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

Output: Program Sample 3.4


0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8

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("Enter number of rows >> " );


row = input.nextInt();

System.out.print("Enter number of cols >> " );


col = input.nextInt();

int[][] matrix = new 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();
}

System.out.print("\nThe elements of the matrix \n\n");


for(int x = 0; x < row; x++)
{
for(int y = 0; y < col; y++)
{
System.out.print(matrix[x][y] + "\t");
}
System.out.print("\n");
}
}
}

System.in field permits you to read input from the keyboard.


Scanner class is used to get user input
nextLine() method of Java Scanner class is used to get the input string that was skipped of the
Scanner object

NVSU-FR-ICD-05-00 (081220) Page 5 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

Output: Program Sample 3.5


Enter number of rows >> 3
Enter number of cols >> 3

Enter Element at Index (0,0) >> 1


Enter Element at Index (0,1) >> 2
Enter Element at Index (0,2) >> 3
Enter Element at Index (1,0) >> 4
Enter Element at Index (1,1) >> 5
Enter Element at Index (1,2) >> 6
Enter Element at Index (2,0) >> 7
Enter Element at Index (2,1) >> 8
Enter Element at Index (2,2) >> 9

The elements of the matrix

1 2 3
4 5 6
7 8 9

NVSU-FR-ICD-05-00 (081220) Page 6 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
5. Processing Multidimensional Arrays

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:

Enter number of rows >> 3


Enter number of cols >> 4

Enter 12 Elements separated by space >> 1 2 3 4 5 6 7 8 9 1 2 3

The sum of elements of the matrix >> 51.0


The ave of elements of the matrix >> 4.25

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;

//enter number of rows


System.out.print("Enter number of rows >> " );
row = input.nextInt();

//enter number of columns


System.out.print("Enter number of cols >> " );
col = input.nextInt();

//define a multidimensional array with size row * col


int[][] matrix = new int[row][col];
sum = 0;

System.out.print("\nEnter "+(row*col) + " Elements separated by space >> ");

//the succeeding lines of codes will be executed after the user


//has pressed enter key on the keyboard

for(int x = 0; x < row; x++)


for(int y = 0; y < col; y++)
{
//the initial value of x and y is 0 hence,
//the first element is stored at index (0,0)
matrix[x][y] = input.nextInt();

//the new value of sum is equal to the present value of sum..


//plus the element at matrix[x][y]
sum = sum + matrix[x][y];
}

//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 7 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.7: Create a program that will add the contents of matrix A and matrix B then
store the result at matrix C. Display the corresponding result.
1 2 3 9 8 7
A = 4 5 6 B = 6 5 4
7 7 9 3 2 1
Sample Output:
Matrix A
1 2 3
4 5 6
7 8 9
Matrix B
9 8 7
6 5 4
3 2 1
Matrix C ( A + B )
10 10 10
10 10 10
10 10 10

public class Java_Matrix_06


{
public static void main(String[] args)
{
int A[][] = { //matrix A
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int B[][] = { //matrix B
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int C[][] = new int[3][3]; //matrix C
for(int i = 0; i < 3; i++) //add matrix A & B
for(int j =0; j < 3; j++)
C[i][j] = A[i][j] + B[i][j];
System.out.println("Matrix A"); //display matrix A
for(int i = 0; i < 3; i++)
for(int j =0; j < 3; j++)
{
System.out.print( A[i][j] + " " );
if(j == 2)
System.out.println("");
}
System.out.println("");
System.out.println("Matrix B"); //display matrix B
for(int i = 0; i < 3; i++)
for(int j =0; j < 3; j++)
{
System.out.print( B[i][j] + " " );
if(j == 2)
System.out.println("");
}
System.out.println("");
System.out.println("Matrix C (A + B)"); //display matrix C
for(int i = 0; i < 3; i++)
for(int j =0; j < 3; j++)
{
System.out.print( C[i][j] + " " );
if(j == 2)
System.out.println(" ");
}
}

}
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:

Enter elements of matrix A: 1 2 3 4 5 6 7 8 9


Enter elements of matrix B: 9 8 7 6 5 4 3 2 1

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);

System.out.print("Enter elements of matrix A: ");


for(int x = 0; x < 3; x++)
for(int y = 0; y < 3; y++)
A[x][y] = input.nextInt();

System.out.print("Enter elements of matrix B: ");


for(int x = 0; x < 3; x++)
for(int y = 0; y < 3; y++)
B[x][y] = input.nextInt();

for(int i = 0; i < 3; i++)


for(int j =0; j < 3; j++)
{
sum[i][j] = A[i][j] + B[i][j];
dif[i][j] = A[i][j] - B[i][j];
prd[i][j] = A[i][j] * B[i][j];
}

System.out.print("\nSum >> ");


for(int i = 0; i < 3; i++)
for(int j =0; j < 3; j++)
System.out.print( sum[i][j] + "\t" );
System.out.println("");

System.out.print("Diff >> ");


for(int i = 0; i < 3; i++)
for(int j =0; j < 3; j++)
System.out.print( dif[i][j] + "\t" );
System.out.println("");

System.out.print("Prod >> ");


for(int i = 0; i < 3; i++)
for(int j =0; j < 3; j++)
System.out.print( prd[i][j] + "\t" );
System.out.println("");
}
}

NVSU-FR-ICD-05-00 (081220) Page 9 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.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}
};

//count the number of rows


rows=Spiral.length;

for(i = rows - 1, j = 0; i > 0; i--, j++)


{
//print top row
for(k = j; k < i; k++)
System.out.print(Spiral[j][k] + " ");

//print last column


for(k = j; k < i; k++)
System.out.print(Spiral[k][i] + " ");

//print last row


for(k = i; k > j; k--)
System.out.print(Spiral[i][k] + " ");

//print first column


for(k = i; k > j; k--)
System.out.print(Spiral[k][j] + " ");
}

//if odd size matrix print the middle value

//count the number of columns


cols = Spiral[0].length;
if(cols % 2 == 1)
{
//compute where the middle index is located
int middle = (cols - 1) / 2;
System.out.print(Spiral[middle][middle]);
}
}
}

NVSU-FR-ICD-05-00 (081220) Page 10 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.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

Enter Number of lines >> 3

Select Type of Parity


One [1] for Even Parity
Zero [0] for Odd Parity

Enter Type of Parity >> 1

Enter bits by line...


11100000
11111000
10101010

Bits with parity bit...


111000001-111110001-101010100

TRY AGAIN [Y/N]? >>

Sample Output

Enter Number of lines >> 2

Select Type of Parity


One [1] for Even Parity
Zero [0] for Odd Parity

Enter Type of Parity >> 0

Enter bits by line...


11000000
10000001

Bits with parity bit...


110000001-100000011

TRY AGAIN [Y/N]? >>

NVSU-FR-ICD-05-00 (081220) Page 11 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
package java_case_problem_01.matrix;
import java.util.Scanner;
public class Java_Case_Problem_01Matrix
{
public static void main(String[] args)
{
int lines, parity;
Scanner sc = new Scanner(System.in);
char repeat;

do
{
System.out.print("Enter Number of lines >> ");
lines = sc.nextInt();

System.out.println("\nSelect Type of Parity");


System.out.println("One [1] for Even Parity");
System.out.println("Zero [0] for Odd Parity");

System.out.print("\nEnter Type of Parity >> ");


parity = sc.nextInt();

char message[][] = new char[lines][8];

String data;

System.out.print("\nEnter bits by line...\n");


for(int x = 0; x < lines; x++)
{
//if there is a line (in this case 8 digit number)
//get input line from user and store to variable data
if(sc.hasNext())
{
data = sc.next();
}
//if there are no more input lines, exit loop.
else
{
break;
}
//for each line of input, store individual characters…
//at message array
for (int y = 0; y < 8; y++)
{
message[x][y] = data.charAt(y);
}
}

int paritybit, count = 0;


System.out.print("\nBits with parity bit...");
for(int x = 0; x < lines; x++)
{
//in this problem we are concerned with the number of one’s
//per input line so we need to first count the number of one’s
//in all of the input lines
for (int y = 0; y < 8; y++)
{
if(message[x][y] == '1')
count++;

//after testing the character, display it on screen


System.out.print( message[x][y] );
}

NVSU-FR-ICD-05-00 (081220) Page 12 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

//if parity is equal to 1,


//append “0” if count is an even number
//else append “1”.
if(parity == 1)
{
if(count % 2 == 0)
System.out.print('0');
else
System.out.print('1');
}

//if parity is equal to 0,


//append “1” if count is an even number
//else append “0”.
else if(parity == 0)
{
if(count % 2 == 0)
System.out.print('1');
else
System.out.print('0');
}

//after evaluating the first line of input,


//reset count to 0 and test the next line of input if available
count = 0;
if(x != lines - 1)
System.out.print("-");
}

//ask user to Try Again?


System.out.print("\nTry Again?[Y/N] >> ");
repeat = sc.next().charAt(0);

}while(repeat == 'Y' || repeat == 'y');

}
}

NVSU-FR-ICD-05-00 (081220) Page 13 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.11: Dr. Ben’s twins (Mack and Zack) play soccer. We will assume Mack wears jersey
number 18 and Zack wears 17. So, Dr. Ben has to look for these two numbers when trying to find the
twins.

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

TRY AGAIN [Y/N]? >>

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

TRY AGAIN [Y/N]? >>

NVSU-FR-ICD-05-00 (081220) Page 14 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
package java_case_problem_02matrix;
import java.util.Scanner;
public class Java_Case_Problem_02Matrix
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int rows, mack = 0, zack = 0;
char repeat;

do
{
System.out.println();
rows = sc.nextInt();

int matrix[][] = new int[rows][10];

//enter input lines


for(int x = 0; x < rows; x++)
for(int y = 0; y < 10; y++)
matrix[x][y] = sc.nextInt();

System.out.println();

for(int x = 0; x < rows; x++)


{
for(int y = 0; y < 10; y++)
{
//search for zack & mack one line at a time
System.out.print(matrix[x][y] + " ");
if(matrix[x][y] == 17)
zack = 1;
if(matrix[x][y] == 18)
mack = 1;
}
//after a line is tested, print if zack or mack is found
System.out.println();
if(mack == 1 && zack == 1)
System.out.print("Both");
else if(mack == 0 && zack == 0)
System.out.print("None");
else if(mack == 1 && zack == 0)
System.out.print("Mack");
else if(mack == 0 && zack == 1)
System.out.print("Zack");

mack = 0;
zack = 0;
System.out.println();
}

System.out.print("\nTry Again?[Y/N] >> ");


repeat = sc.next().charAt(0);

}while(repeat == 'Y' || repeat == 'y');


}
}

NVSU-FR-ICD-05-00 (081220) Page 15 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

VI. EVALUATION (Note: Not to be included in the student’s copy of the IM)

VII. LEARNING ACTIVITIES (To be posted in google classroom)

VIII. ASSIGNMENT (To be posted in google classroom)

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

NVSU-FR-ICD-05-00 (081220) Page 16 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”

You might also like