Question

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 the UPPER RIGHT to the LOWER LEFT

6. Let the number be actually formatted diagonally as they are printed. So your output should look similar to the following:


    7
   10       
       76     
         34      
           54     
     12


// This program illustrates how two-dimensional arrays are
// passed as parameters to methods.

public class TwoDimArraysAsParam //Line 1
{ //Line 2
public static void main(String[] args) //Line 3
{ //Line 4
int[][] board ={{23,5,6,15,18,7},
{4,16,24,67,10,7},
{12,54,23,76,11,7},
{1,12,34,22,8,7},
{81,54,32,67,33,7},
{12,34,76,78,9,7}}; //Line 5
//System.out.println("board.length=" + board.length);
// System.out.println("board[0].length=" + board[0].length);

  
TwoDimArraysMethods.printMatrix(board); //Line 6
System.out.println(); //Line 7

TwoDimArraysMethods.sumRows(board); //Line 8
System.out.println(); //Line 9

TwoDimArraysMethods.largestInRows(board); //Line 10
  
TwoDimArraysMethods.printDiagonal(board); //Line 6
System.out.println(); //Line 9
//Line 7


} //end main //Line 11
}    //Line 12


// This class contains methods to process elements in two-
// dimensional arrays.

public class TwoDimArraysMethods
{
public static void printMatrix(int[][] matrix)
{
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[row].length; col++)
System.out.printf("%7d", matrix[row][col]);

System.out.println();
}
} //end printMatrix

public static void sumRows(int[][] matrix)
{
int sum;

//sum of each individual row
for (int row = 0; row < matrix.length; row++)
{
sum = 0;

for (int col = 0; col < matrix[row].length; col++)
sum = sum + matrix[row][col];

System.out.println("The sum of the elements of row "
+ (row + 1) + " = " + sum + ".");
}
} //end sumRows

public static void largestInRows(int[][] matrix)
{
int largest;

//Largest element in each row
for (int row = 0; row < matrix.length; row++)
{
largest = matrix[row][0]; //assume that the first
//element of the row is
//largest
for (int col = 1; col < matrix[row].length; col++)
if (largest < matrix[row][col])
largest = matrix[row][col];

System.out.println("The largest element of row "
+ (row + 1) + " = " + largest + ".");
}
} //end largestInRows
  
public static void printDiagonal(int[][] matrix)
{
  
   String format = new String();
  
  
for (int row = 0; row > matrix.length; row++)
{
   format = "%" + (row * 7 + 1) + "d" ; /* Space out each number more to the right
   * (8 additionalspaces)
   * with each iteration. Note how "format" is
   * used with the "printf" below.
   */
   for (int col = 0; col < matrix[row].length; col++)
if(row == col)
System.out.printf(format, matrix[row][col]);
  
  
System.out.println();
}
}
}

  
  

      

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

Mu changes are in bold

Added commentary for my lines

TwoDimArraysAsParam.java

// This program illustrates how two-dimensional arrays are
// passed as parameters to methods.
public class TwoDimArraysAsParam                   //Line 1
{                                                   //Line 2
   public static void main(String[] args)           //Line 3
   {                                               //Line 4
       int[][] board ={{23,5,6,15,18,7},
               {4,16,24,67,10,7},
               {12,54,23,76,11,7},
               {1,12,34,22,8,7},
               {81,54,32,67,33,7},
               {12,34,76,78,9,7}};               //Line 5
       //System.out.println("board.length=" + board.length);
   // System.out.println("board[0].length=" + board[0].length);
  
       TwoDimArraysMethods.printMatrix(board);       //Line 6
       System.out.println();                       //Line 7
       TwoDimArraysMethods.sumRows(board);           //Line 8
       System.out.println();                       //Line 9
       TwoDimArraysMethods.largestInRows(board);   //Line 10
       System.out.println();                       //Line 11
       TwoDimArraysMethods.printDiagonal(board);   //Line 12
       System.out.println();                       //Line 13
       TwoDimArraysMethods.printDiagonalRL(board);   //Line 14
       System.out.println();                       //Line 15

   } //end main                                   //Line 16
}                                                   //Line 17

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

TwoDimArraysMethods.java

// This class contains methods to process elements in two-
// dimensional arrays.
public class TwoDimArraysMethods
{
   public static void printMatrix(int[][] matrix)
   {
       for (int row = 0; row < matrix.length; row++)
       {
           for (int col = 0; col < matrix[row].length; col++)
               System.out.printf("%7d", matrix[row][col]);
           System.out.println();
       }
   } //end printMatrix
   public static void sumRows(int[][] matrix)
   {
       int sum;
           //sum of each individual row
       for (int row = 0; row < matrix.length; row++)
       {
           sum = 0;
           for (int col = 0; col < matrix[row].length; col++)
               sum = sum + matrix[row][col];
           System.out.println("The sum of the elements of row "
                           + (row + 1) + " = " + sum + ".");
       }
   } //end sumRows
   public static void largestInRows(int[][] matrix)
   {
       int largest;
           //Largest element in each row
       for (int row = 0; row < matrix.length; row++)
       {
           largest = matrix[row][0]; //assume that the first
                                   //element of the row is
                                   //largest
           for (int col = 1; col < matrix[row].length; col++)
               if (largest < matrix[row][col])
                   largest = matrix[row][col];
           System.out.println("The largest element of row "
                           + (row + 1) + " = " + largest + ".");
       }
   } //end largestInRows
  
   public static void printDiagonal(int[][] matrix)
   {
  
       String format = new String();
      
       for (int row = 0; row < matrix.length; row++)
       {
           format = "%" + (row * 7 + 1) + "d" ; /* Space out each number more to the right
                                               * (8 additionalspaces)
                                               * with each iteration. Note how "format" is
                                               * used with the "printf" below.
                                               */
           for (int col = 0; col < matrix[row].length; col++)
           if(row == col)
               System.out.printf(format, matrix[row][col]);
  
          
           System.out.println();
       }
   }
  
   // Newly added function
   public static void printDiagonalRL(int[][] matrix)
   {
       String format = new String();
       int size = matrix.length;
       for (int row = 0; row < size; row++)
       {
           // to give more indentation for row with less index using size-i-1 times 7
           format = "%" + ((size-row-1) * 7 + 1) + "d" ; /* Space out each number more to the right
                                               * (8 additionalspaces)
                                               * with each iteration. Note how "format" is
                                               * used with the "printf" below.
                                               */
           for (int col = 0; col < matrix[row].length; col++)
               // to print the diagonal from the UPPER RIGHT to the LOWER LEFT,
               // value of row + value col should be equal to size-1
               if(row + col == size - 1)
                   System.out.printf(format, matrix[row][col]);

           System.out.println();
       }
   }

}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Output:

Add a comment
Know the answer?
Add Answer to:
Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right...
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 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...

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

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggli...

    Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggling with a part of a question assigned by the professor. He gave us this code to fill in: import java.util.Arrays; import java.util.Random; public class RobotGo {     public static Random r = new Random(58);     public static void main(String[] args) {         char[][] currentBoard = createRandomObstacle(createBoard(10, 20), 100);         displayBoard(startNavigation(currentBoard))     }     public static int[] getRandomCoordinate(int maxY, int maxX) {         int[] coor = new...

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

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

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

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

  • This is the assignment..... Write a class DataSet that stores a number of values of type...

    This is the assignment..... Write a class DataSet that stores a number of values of type double. Provide a constructor public DataSet(int maxNumberOfValues) and a method public void addValue(double value) that add a value provided there is still room. Provide methods to compute the sum, average, maximum and minimum value. ​This is what I have, its suppose to be using arrays also the double smallest = Double.MAX_VALUE; and double largest = Double.MIN_VALUE;​ are there so I don't need to create...

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