Table of Contents
Project 9: Answer
import java.util.Scanner;
public class MatrixOperations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input M and N
System.out.print("Enter number of rows (M): ");
int M = sc.nextInt();
System.out.print("Enter number of columns (N): ");
int N = sc.nextInt();
// Validate input
if (M <= 2 || M >= 10 || N <= 2 || N >= 10) {
System.out.println("INVALID INPUT");
return;
}
int[][] A = new int[M][N];
// Input elements
System.out.println("Enter elements of the matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = sc.nextInt();
}
}
// (a) Display the input matrix
System.out.println("\nInput Matrix:");
displayMatrix(A);
// (b) Rotate the matrix by 270° anti-clockwise
System.out.println("\nMatrix after 270° anti-clockwise rotation:");
int[][] rotated = rotate270AntiClockwise(A);
displayMatrix(rotated);
// (c) Calculate sum of odd elements
int sumOdd = sumOddElements(A);
System.out.println("\nSum of odd elements: " + sumOdd);
sc.close();
}
// Function to display a matrix
public static void displayMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int val : row) {
System.out.print(val + "\t");
}
System.out.println();
}
}
// Function to rotate matrix by 270° anti-clockwise
public static int[][] rotate270AntiClockwise(int[][] matrix) {
int M = matrix.length;
int N = matrix[0].length;
int[][] rotated = new int[N][M]; // Note dimensions swapped
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
rotated[N - 1 - j][i] = matrix[i][j];
}
}
return rotated;
}
// Function to sum odd elements
public static int sumOddElements(int[][] matrix) {
int sum = 0;
for (int[] row : matrix) {
for (int val : row) {
if (val % 2 != 0) {
sum += val;
}
}
}
return sum;
}
}
How it works:
-
Input Validation:
Checks ifMandNare >2 and <10. Prints INVALID INPUT otherwise. -
Matrix Input & Display:
Reads integers into a 2D array and prints it. -
Rotation 270° Anti-Clockwise:
-
270° anti-clockwise rotation is equivalent to 90° clockwise rotation of the transposed matrix.
-
The formula used:
rotated[N-1-j][i] = matrix[i][j].
-
-
Sum of Odd Elements:
Iterates through all elements and sums those which are odd (val % 2 != 0).
Example Run:
Input:
M = 3
N = 4
Elements: 8 7 9 3 -2 0 4 5 1 3 6 -4
Output:
Input Matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
Matrix after 270° anti-clockwise rotation:
3 5 -4
9 4 6
7 0 3
8 -2 1
Sum of odd elements: 38
code
import java.util.Scanner;
public class Question2Matrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input M and N
System.out.print("Enter number of rows (M): ");
int M = sc.nextInt();
System.out.print("Enter number of columns (N): ");
int N = sc.nextInt();
// Validate input
if (M <= 2 || M >= 10 || N <= 2 || N >= 10) {
System.out.println("INVALID INPUT");
sc.close();
return;
}
int[][] A = new int[M][N];
// Input elements
System.out.println("Enter elements of the matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = sc.nextInt();
}
}
// (a) Display Original Matrix
System.out.println("\nORIGINAL MATRIX:");
displayMatrix(A);
// (b) Rotate by 270° anti-clockwise
int[][] rotated = rotate270AntiClockwise(A);
System.out.println("\nROTATED MATRIX (270° ANTI CLOCKWISE):");
displayMatrix(rotated);
// (c) Sum of odd elements
int sumOdd = sumOddElements(A);
System.out.println("\nSUM OF THE ODD ELEMENTS = " + sumOdd);
sc.close();
}
// Function to display a matrix
public static void displayMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int val : row) {
System.out.print(val + "\t");
}
System.out.println();
}
}
// Function to rotate matrix 270° anti-clockwise
public static int[][] rotate270AntiClockwise(int[][] matrix) {
int M = matrix.length;
int N = matrix[0].length;
int[][] rotated = new int[N][M]; // Swap dimensions
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
rotated[N - 1 - j][i] = matrix[i][j];
}
}
return rotated;
}
// Function to calculate sum of odd elements
public static int sumOddElements(int[][] matrix) {
int sum = 0;
for (int[] row : matrix) {
for (int val : row) {
if (val % 2 != 0) {
sum += val;
}
}
}
return sum;
}
}