Question

In Java Please Create A Program For The Following; Please Note: This program should be able...

In Java Please Create A Program For The Following;

Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes.

Switch2DRows
Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:

3 2 4 1
3 9 8 5
6 1 4 9
Largest Row = 1

3 9 8 5
3 2 4 1
6 1 4 9

Required methods:
public static void fillArray(int[][] arr)
// Fills a 2D array with random numbers 0-9

public static void showArray(int[][] arr)
// Displays a 2D array as rows and columns

public static int largestRow(int[][] arr)
// Returns the row with the largest sum

public static int rowSum(int[][] arr, int row)
// Returns the sum for a specific row of a 2D array

public static void largestRowFirst(int[][] arr)
// Switches the 1st row with the row that has the largest sum

    public static void main()
    {
        final int ROWS = 3;
        final int COLS = 4;
        int [][] a2d = new int[ROWS][COLS];

        fillArray(a2d);
        showArray(a2d);
        largestRowFirst(a2d);
        showArray(a2d);
    }
This program should be able accept changes to the values of constants ROWS and COLS when testing your code.

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

Answer:

Explanation:

Here are all the functions filled fillArray, showArray, largestRow , rowSum and largestRowFirst

largestRowFirst uses the largestRow function to get the index of the largest row

and the largestRow uses the rowSum to get the sum of each row.

Then the largest row is swapped with the first row.

Code:

import java.util.Random;

public class Main
{
  
public static void fillArray(int[][] arr)
{
Random r = new Random();
  
for(int i=0; i<arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
{
arr[i][j] = r.nextInt(10);
}
  
}
}
// Fills a 2D array with random numbers 0-9

public static void showArray(int[][] arr)
{
for(int i=0; i<arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
{
System.out.print(arr[i][j]+" ");
}
  
System.out.println();
}
}
// Displays a 2D array as rows and columns

public static int largestRow(int[][] arr)
{
int maxi = -1;
int max = -1;
  
for(int i=0; i<arr.length; i++)
{
if(rowSum(arr, i) > max)
{
max = rowSum(arr, i);
maxi = i;
}
  
}
  
return maxi;
}
// Returns the row with the largest sum

public static int rowSum(int[][] arr, int row)
{
int sum = 0;
for(int j=0; j<arr[row].length; j++)
{
sum = sum + arr[row][j];
}
  
return sum;
}
// Returns the sum for a specific row of a 2D array

public static void largestRowFirst(int[][] arr)
{
int index = largestRow(arr);
  

for(int j=0; j<arr[index].length; j++)
{
int temp = arr[index][j];
arr[index][j] = arr[0][j];
arr[0][j] = temp;
}
}
// Switches the 1st row with the row that has the largest sum

public static void main(String[] args)
{
final int ROWS = 3;
final int COLS = 4;
int [][] a2d = new int[ROWS][COLS];

fillArray(a2d);
showArray(a2d);
largestRowFirst(a2d);
showArray(a2d);
}
}

Output:

5 5 4 2 3 6 5 9 9 3 6 4 3 6 5 9 5 5 4 2 9 3 6 4 ... Program finished with exit code 0 Press ENTER to exit console.

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!

Add a comment
Know the answer?
Add Answer to:
In Java Please Create A Program For The Following; Please Note: This program should be able...
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
  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like...

    C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc()) int **createArray(int rows, int cols) parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated void freeArray (int **array, int rows, int cols) the parameters to this function are: -the array created by createArray() -the number of rows in the array -the number...

  • C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like...

    C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc()) int **createArray(int rows, int cols) parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated void freeArray (int **array, int rows, int cols) the parameters to this function are: -the array created by createArray() -the number of rows in the array -the number...

  • Help pls! and kindly explain how you do this as well. Thaank you Write a program...

    Help pls! and kindly explain how you do this as well. Thaank you Write a program to test whether a square is a 3x3 magic square. A magic square is a grid with 3 rows and 3 columns, like the figure below. A magic square has the following properties: the grid contains only the numbers 1 through 9 the sum of each row, each column, and each diagonal all add up to the same number Notes: I have provided the...

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to...

    please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to be done! * * TODO#1: Change the name and date accordingly * Created by , 11/25/2019 */ public class CS140_Arrays_Done { //TODO#2: Define two private static constants: rows (set to 5) & cols (set to 3) //array for the values private static double[][] values = { { 9, 10, 8 }, { 3.5, 10, 8.5 }, { 10, 8.5, 9 }, { 8.5, 10,...

  • //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...

  • I need help as quick as possible, thanks beforehand. Please provide the test output The Lo...

    I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

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
ADVERTISEMENT