Question

Can someone explain this code with comments I am supposed to dispay an array an add...


Can someone explain this code with comments I am supposed to dispay an array an add each columns and add each row

An application uses a two-dimensional array declared as follows: int[][] days = new int[29][5]; a. Write code that sums each row in the array and displays the results. b. Write code that sums each column in the array and displays the results.

class TwoDimensionalArrayDemo
{
  public static void main( String[] arg ) {
      
  int[][] days = new int[29][5];

 for (int i = 0; i < days.length; i++) {
      for (int j = 0; j < days[i].length; j++) {
          days[i][j] = i + j;
      }
  }

  
  for (int[] a : days) {
      for (int i : a) {
          System.out.print(i + "\t");
      }
      System.out.println("\n");
  } 
  System.out.println("column totals"); 
   int totalRow = 0;
  for( int col = 0; col < 5; col++)
  {
          totalRow = 0;
      for( int row = 0; row < 29; row++)
        totalRow = totalRow + days [row][col];
          System.out.println(totalRow);
  }
  System.out.println("row totals");
   int totalCol;
      for( int row = 0; row < 29; row++)
      {
       totalCol = 0;
    for( int col = 0; col < 5; col++)
           totalCol = totalCol + days [row] [col];
    System.out.println(totalCol);
}

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

// Defines a class TwoDimensionalArrayDemo for matrix implementation

public class TwoDimensionalArrayDemo

{

// main method definition

public static void main( String[] arg)

{

/*

* Creates an array of 29 X 5

* Number of rows is 29

* Number of columns is 5

* new keyword is used to dynamically allocate memory

*/

int[][] days = new int[29][5];

// Loops till number of rows (days.length will provide number of rows)

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

{

// Loops till number of columns

// (days[i].length will provide number of columns in ith row)

for (int j = 0; j < days[i].length; j++)

{

// Adds i and j value and stores it at ith row and jth column position

days[i][j] = i + j;

}// End of inner for loop (for column)

}// End of outer for loop (for row)

/*

* Enhanced for loop is used to access matrix

* Extract each row from the matrix and stores it in a

*/

for (int[] a : days)

{

// Each column value is extracted from array a

for (int i : a)

{

// Displays current cell value stored in i with tab space

System.out.print(i + "\t");

}// End of inner for loop

// Displays new line character to move to next line for next row

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

}// End of outer for loop

// Displays heading

System.out.println("column totals");

// To store column sum

int totalCol = 0;

// Loops till number of columns

for( int col = 0; col < 5; col++)

{

// Resets the total to 0 for each column sum

totalCol = 0;

// Loops till number of rows

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

// Calculates total by moving row index and keeping column constant

totalCol = totalCol + days [row][col];

// Displays current column total

System.out.println("Column " + (col + 1) + " total = " + totalCol);

}// End of outer for loop

// Displays heading

System.out.println("row totals");

// To store row sum

int totalRow = 0;

// Loops till number of rows

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

{

// Resets the total to 0 for each row sum

totalRow = 0;

// Loops till number of columns

for( int col = 0; col < 5; col++)

// Calculates total by moving column index and keeping row constant

totalRow = totalRow + days [row] [col];

// Displays current column total

System.out.println("Row " + (row + 1) + " total = " + totalRow);

}// End of outer for loop

}// End of main method

}// End of class

Sample Output:

0 1 2 3 4

1 2 3 4 5

2 3 4 5 6

3 4 5 6 7

4 5 6 7 8

5 6 7 8 9

6 7 8 9 10

7 8 9 10 11

8 9 10 11 12

9 10 11 12 13

10 11 12 13 14

11 12 13 14 15

12 13 14 15 16

13 14 15 16 17

14 15 16 17 18

15 16 17 18 19

16 17 18 19 20

17 18 19 20 21

18 19 20 21 22

19 20 21 22 23

20 21 22 23 24

21 22 23 24 25

22 23 24 25 26

23 24 25 26 27

24 25 26 27 28

25 26 27 28 29

26 27 28 29 30

27 28 29 30 31

28 29 30 31 32

column totals

Column 1 total = 406

Column 2 total = 435

Column 3 total = 464

Column 4 total = 493

Column 5 total = 522

row totals

Row 1 total = 10

Row 2 total = 15

Row 3 total = 20

Row 4 total = 25

Row 5 total = 30

Row 6 total = 35

Row 7 total = 40

Row 8 total = 45

Row 9 total = 50

Row 10 total = 55

Row 11 total = 60

Row 12 total = 65

Row 13 total = 70

Row 14 total = 75

Row 15 total = 80

Row 16 total = 85

Row 17 total = 90

Row 18 total = 95

Row 19 total = 100

Row 20 total = 105

Row 21 total = 110

Row 22 total = 115

Row 23 total = 120

Row 24 total = 125

Row 25 total = 130

Row 26 total = 135

Row 27 total = 140

Row 28 total = 145

Row 29 total = 150

Add a comment
Know the answer?
Add Answer to:
Can someone explain this code with comments I am supposed to dispay an array an add...
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
  • The last element in each array in a 2D array is incorrect. It’s your job to...

    The last element in each array in a 2D array is incorrect. It’s your job to fix each array so that the value 0 is changed to include the correct value. In the first array, the final value should be the length of the first array. In the second array, the final value should be the sum of the first value, and the second to last value in the array. In the third array, the final value should be the...

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

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • Java language only **Mandatory Rules** Put Descriptive comments on the code displayed on top of previous...

    Java language only **Mandatory Rules** Put Descriptive comments on the code displayed on top of previous comments alreadys displayed On Top of Code write summary of what code will do in comments At the end of the code have the output in comment form I Will thumbs up good Work thanks! public class MagicSquare { static int[][] createMagicSquare(int square[][]) { // Initialize position for 1 int i = 3/2; int j = 3-1; // One by one put all values...

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

  • 1. What is the output of the following code segment? int array[] = { 8, 6,...

    1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; System.out.println( "Index Value" ); for ( int i = 0; i < array.length; i++ ) System.out.printf( "%d %d\n", i, array[ i ] ); 2. What is the output of the following code segment? char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; String output = "The sentence...

  • whats the answers How many total integers elements are in an array with 4 rows and...

    whats the answers How many total integers elements are in an array with 4 rows and 7 columns? 07 28 11 What is the resulting array contents, assuming that itemList is an array of size 4 having contents -55, -1, 0, 9? for (i = 0; i < 4; ++i) { itemsList[i] - i; -54,0, 1, 10 O 0, 1, 2, 3 O 1, 2, 3, 4 O-55,-1, 0,9 Given an integer array myVals of size N_SIZE (i.e. int[] myVals...

  • Consider the following matrix transpose routines: typedef int array[4][4]; void transpose (array dst, array src) {...

    Consider the following matrix transpose routines: typedef int array[4][4]; void transpose (array dst, array src) {     int i, j;     for (i=0; i<4; i++) {    for (j=0; j<4; j++) {           dst[i][j] = src[i][j];    }     } } void transpose2 (array dst, array src) {     int i, j;     for (i=0; i<4; i++) {    for (j=0; j<4; j++) {           dst[j][i] = src[j][i];    }     } } Assume this code runs on...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

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

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