Question

In: Computer Science

JAVA Write a program that prompts the user to enter a matrix number of rows and...

JAVA

Write a program that prompts the user to enter a matrix number of rows and number of columns.
In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it.
Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use global variables.

Here is a sample run of the program:

Enter number of rows and columns 4 6
1 0 0 0 1 0
0 0 0 0 1 0
1 0 1 0 1 1
1 1 1 1 0 1
3 2 3
Process finished with exit code 0

Solutions

Expert Solution

CODE -

import java.util.Scanner;

import java.util.Random;

public class matrix {

    // FUNCTION TO FIND AND DISPLAY SUM OF EACH COLUMN OF THE MATRIX AT EVEN INDEX

    static void sumColumns(int[][] matrix)

    {

        // FINDING THE NUMBER OF ROWS IN THE MATRIX

        int row = matrix.length;

        // FINDING THE NUMBER OF COLUMNS IN THE MATRIX

        int column = matrix[0].length;

        // LOOP TO FIND AND PRINT SUM OF EACH COLUMNS AT EVEN INDEX

        for(int i=0; i<column; i = i+2)

        {

            int sum_column = 0;

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

                sum_column += matrix[j][i];

            System.out.print(sum_column + " ");

        }

    }

    // MAIN FUNCTION

    public static void main(String[] args)

    {

        int row, column;

        Scanner keyboard = new Scanner(System.in);

        Random rand = new Random();

        // TAKING NUMBER OF ROW AND NUMBER OF COLUMN AS INPUT FROM THE USER

        System.out.print("Enter number of rows and columns ");

        row = keyboard.nextInt();

        column = keyboard.nextInt();

        // CREATING A MATRIX OF SIZE ENTERED BY THE USER

        int[][] matrix = new int[row][column];

        // FILLING THE MATRIX RANDOMLY WITH 0 AND 1

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

            for(int j=0; j<column; j++)

                matrix[i][j] = rand.nextInt(2);

        // DISPLAYING THE MATRIX

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

        {

            for(int j=0; j<column; j++)

                System.out.print(matrix[i][j] + " ");

            System.out.println();

        }

        // CALLING THE sumColumns FUNCTION.

        sumColumns(matrix);

        keyboard.close();

    }

}

SCREENSHOTS -

CODE -

OUTPUT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

JAVA posted multiple times Write a program that prompts the user to enter a matrix number...
JAVA posted multiple times Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use...
JAVA posted multiple times Write a program that prompts the user to enter a matrix number...
JAVA posted multiple times Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use...
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
JAVA Write a program that will compare two names. The program prompts the user to enter...
JAVA Write a program that will compare two names. The program prompts the user to enter two names for a comparison. If the names are same, the program states that. If the names are different, the program converts both names to UPPERCASE, and compares then again. If they are equal, the programs displays a message stating that names are equal if CASE is ignored. Otherwise, the program prints names with a message that names are not equal.  Check also if there...
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT