Question

How do I modify this to let the user input the size of the matrix? The...

How do I modify this to let the user input the size of the matrix? The lowest size is 4 and the largest size is 16.

import java.util.*;

public class Test1

{

     public static void main(String[] args)

     {

int[][] matrix = new int[4][4];    // create 4 by 4 matrix (need user input???)

          // generate 1's and 0's for each each rows and columns

     // and track largest row index with the most ones

        int largestRI = 0;

        int largest = -1;

        for (int i = 0; i < matrix.length; i++)

        {

            int rowCount = 0;

              for (int k = 0; k < matrix[i].length; k++)

              {

                   matrix[i][k] = (int)(Math.random() * 2);

                   rowCount += matrix[i][k];

              }

              if (rowCount > largest)

              {

                   largestRI = i;

                largest = rowCount;

              }

        }

        // find largest column index

        int largestCI = 0;

          largest = -1;

          for (int k = 0; k < matrix[0].length; k++)

          {

              int columnCount = 0;

              for (int i = 0; i < matrix.length; i++)

              {

                   columnCount += matrix[i][k];

              }

              if (columnCount > largest)

              {

                   largest = columnCount;

                   largestCI = k;

              }

        }

      

       // display matrix

       for (int i = 0; i < matrix.length; i++)

       {

           for (int k = 0; k < matrix[i].length; k++)

           {

           System.out.printf("%d", matrix[i][k]);

           }

           System.out.printf("\n");

       }

       System.out.println("The largest row index: " + largestRI);

       System.out.println("The largest column index: " + largestCI);

     }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Test1

{

   public static void main(String[] args)

   {
       int size;
       Scanner sc = new Scanner(System.in);
       while (true) {
           System.out.println("Enter size of matrix: ");
           size = sc.nextInt();
           if (size >= 4 && size <= 16)
               break;
           System.out.println("Error: Invalid size.. Must be between 4-16");
       }

       int[][] matrix = new int[size][size]; // create 4 by 4 matrix (need user input???)

       // generate 1's and 0's for each each rows and columns

       // and track largest row index with the most ones

       int largestRI = 0;

       int largest = -1;

       for (int i = 0; i < matrix.length; i++)

       {

           int rowCount = 0;

           for (int k = 0; k < matrix[i].length; k++)

           {

               matrix[i][k] = (int) (Math.random() * 2);

               rowCount += matrix[i][k];

           }

           if (rowCount > largest)

           {

               largestRI = i;

               largest = rowCount;

           }

       }

       // find largest column index

       int largestCI = 0;

       largest = -1;

       for (int k = 0; k < matrix[0].length; k++)

       {

           int columnCount = 0;

           for (int i = 0; i < matrix.length; i++)

           {

               columnCount += matrix[i][k];

           }

           if (columnCount > largest)

           {

               largest = columnCount;

               largestCI = k;

           }

       }

       // display matrix

       for (int i = 0; i < matrix.length; i++)

       {

           for (int k = 0; k < matrix[i].length; k++)

           {

               System.out.printf("%3d", matrix[i][k]);

           }

           System.out.printf("\n");

       }

       System.out.println("The largest row index: " + largestRI);

       System.out.println("The largest column index: " + largestCI);

   }

}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
How do I modify this to let the user input the size of the matrix? The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right...

    Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right corner to the bottom left corner and i dont know how to do it please help me thank you dd another method to the bottom of the "TwoDimArraysMethods.java" file called "printDiagonalRL()"                                         public static void printDiagonalRL(int[][] matrix) 4. Call this method in the main file ("TwoDimArraysAsParam.java") passing it "board." e.g. TwoDimArraysMethods.printDiagonal(board); 5. Your new method should print any numbers along the diagonal from...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • Write a getCount method in the IntArrayWorker class that returns the count of the number of...

    Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • this assignment need to be done in java only Requirements: Ask the user to enter the...

    this assignment need to be done in java only Requirements: Ask the user to enter the size of an array (int value) Allocate a 2D array of int that size (if the user enters in 5, then allocate a 5x5 array) Using a Random, initialize each element of the array to be either 0 or 1 Output the array in a table format (see below for an example) Output the fraction of your array that is ones and the fraction...

  • please use eclipse the sample output is incorrect CPS 2231 Chapter 8 Lab 1 Spring 2019...

    please use eclipse the sample output is incorrect CPS 2231 Chapter 8 Lab 1 Spring 2019 1. Write a class, SumOrRows. The main method will do the following methods: a. Call a method, createAndFillArray which: defines a 2 dim array of 3 rows and 4 columns, prompts the user for values and stores the values in the array. b. Calls a method, printArray, which: prints the Array as shown below c. Cails a method, calcSums, which: returns à 1 dimensional...

  • draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner;...

    draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner; public class Matrix {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);    System.out.print("Enter number of rows: first matrix ");    int rows = scanner.nextInt();    System.out.print("Enter number of columns first matrix: ");    int columns = scanner.nextInt();    System.out.print("Enter number of rows: seconed matrix ");    int rowss = scanner.nextInt();    System.out.print("Enter number of columns seconed matrix:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT