Write a Java program to manipulate a matrix by swapping the elements in even columns. The program should prompt the user to input the number of rows and columns of the matrix, followed by the elements of the matrix. Afterward, it should swap the elements in even columns of the matrix and display the modified matrix. Demonstrate the functionality of your program with a sample input/output scenario. Sample Input 3 // Enter the number of rows4 // Enter the number of columns1 // Enter the elements of the matrix23456789101112Sample OutputBefore1 2 3 45 6 7 89 10 11 12After2 1 4 36 5 8 710 9 12 11
Question
Write a Java program to manipulate a matrix by swapping the elements in even columns. The program should prompt the user to input the number of rows and columns of the matrix, followed by the elements of the matrix. Afterward, it should swap the elements in even columns of the matrix and display the modified matrix. Demonstrate the functionality of your program with a sample input/output scenario. Sample Input 3 // Enter the number of rows4 // Enter the number of columns1 // Enter the elements of the matrix23456789101112Sample OutputBefore1 2 3 45 6 7 89 10 11 12After2 1 4 36 5 8 710 9 12 11
Solution
Here is a Java program that accomplishes the task:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows:");
int rows = scanner.nextInt();
System.out.println("Enter the number of columns:");
int columns = scanner.nextInt();
int[][] matrix = new int[rows][columns];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = scanner.nextInt();
}
}
System.out.println("Before:");
printMatrix(matrix);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j += 2) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][j + 1];
matrix[i][j + 1] = temp;
}
}
System.out.println("After:");
printMatrix(matrix);
}
private static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}
Sample Input:
Enter the number of rows:
3
Enter the number of columns:
4
Enter the elements of the matrix:
1
2
3
4
5
6
7
8
9
10
11
12
Sample Output:
Before:
1 2 3 4
5 6 7 8
9 10 11 12
After:
2 1 4 3
6 5 8 7
10 9 12 11
This program first takes the number of rows and columns as input from the user. Then it takes the elements of the matrix as input. It then prints the original matrix, swaps the elements in even columns, and prints the modified matrix.
Similar Questions
Ben is studying matrices and wants to write a program that swaps two rows of a matrix (N, M), the program takes an input matrix, swaps the specified rows, and then displays the modified matrix.Assist him with the program.Input format :The first line consists of two space-separated integers N and M, representing the number of rows and columns in the matrix.The next N lines contain M space-separated integers each, representing the elements of the matrix.The last line consists of two space-separated integers r1 and r2, representing the row numbers to be interchanged.Output format :If the input row numbers r1 or r2 are invalid (less than 1 or greater than the number of rows), the output prints "Invalid row numbers".Otherwise, the modified matrix is printed after swapping the specified rows.
Write a program for matrix multiplication in java using threads.Input format :The first line consists of the row and column value of Matrix 1 separated by a single spaceThe second line consists of the row and column value of Matrix 2 separated by a single spaceThe next input Matrix 1 elements of the matrix.The next input Matrix 2 elements of the matrix.Output format :Display the matrix after multiplication.Refer to the sample input and output for format specifications.Code constraints :Integers only.Sample test cases :Input 1 :2 22 21 23 45 67 8Output 1 :19 22 43 50
You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix:Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≤ j < 5).You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.InputThe input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.OutputPrint a single integer — the minimum number of moves needed to make the matrix beautiful.ExamplesinputCopy0 0 0 0 00 0 0 0 10 0 0 0 00 0 0 0 00 0 0 0 0outputCopy3inputCopy0 0 0 0 00 0 0 0 00 1 0 0 00 0 0 0 00 0 0 0 0outputCopy1
Write a java program to perform matrix multiplicationinput:m-no. of rowsn-no.of columsmatrix1=[]matrix2=[]output:matrix3=[]
5 4 3 7 68 1 2 9 27 3 4 1 03 6 9 2 31 4 9 3 0d) Find the mirror image of the matrix without using an additional array.e) Display the new array in the tabular format.6 7 3 4 52 9 2 1 80 1 4 3 73 2 9 6 30 3 9 4 1Save your program as 2AQ2b.c
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.