Knowee
Questions
Features
Study Tools

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.

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.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Sure, here is a simple Java program that does what you asked for:

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

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns - 1; j += 2) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][j + 1];
                matrix[i][j + 1] = temp;
            }
        }

        System.out.println("The modified matrix is:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Here is a sample input/output scenario:

Enter the number of rows:
3
Enter the number of columns:
3
Enter the elements of the matrix:
1 2 3
4 5 6
7 8 9
The modified matrix is:
2 1 3 
5 4 6 
8 7 9 

In this scenario, the user inputs a 3x3 matrix. The program then swaps the elements in the even columns (0-based index), resulting in the modified matrix.

This problem has been solved

Solution 2

Sure, here is a simple Java program that does what you asked for:

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 cols = scanner.nextInt();
        int[][] matrix = new int[rows][cols];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j += 2) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][j + 1];
                matrix[i][j + 1] = temp;
            }
        }

        System.out.println("The modified matrix is:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

This program first prompts the user to enter the number of rows and columns of the matrix. Then, it prompts the user to enter the elements of the matrix. After that, it swaps the elements in even columns of the matrix. Finally, it displays the modified matrix.

For example, if the user inputs:

3
3
1 2 3
4 5 6
7 8 9

The output will be:

2 1 4
5 4 8
8 7 9

This is because the elements in the even columns (0-based index) have been swapped with the elements in the next column.

This problem has been solved

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

Problem StatementRaj, a coding hobbyist, is working on a binary matrix manipulation program. He wants to create a simple tool to input binary values in the form of a matrix and display the entered binary pattern. Write a program that takes the dimensions (rows and columns) of a binary matrix as input, reads the binary values, and then displays the entered binary pattern grid.Input format :The first line of input consists of two space-separated integers r, and c, representing the number of rows and columns of the binary matrix.The next r lines consist of c space-separated binary values (0 or 1) representing the elements of the array.Output format :The output displays the entered binary pattern grid.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ rows, cols ≤ 5Each cell of the matrix contains either 0 or 1.Sample test cases :Input 1 :4 31 0 11 1 01 1 10 0 0Output 1 :1 0 1 1 1 0 1 1 1 0 0 0 Input 2 :5 51 0 1 0 10 1 0 1 01 0 1 0 10 1 0 1 01 0 1 0 1Output 2 :1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 Input 3 :1 10Output 3 :0

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

1/3

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.