adding elements in a specified column or row in a two dimensional array java

package multidimensionalarrays;

public class MultidimensionalArrays {

    public static void main(String[] args) {
        double sumOfRow = 0;
        double[][] matrix = new double[3][4];
        java.util.Scanner input = new java.util.Scanner(System.in); //Scanner
        System.out.println("Enter a 3 by 4 matrix row by row: ");
        //Prompt user to enter matrix numbers
        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[0].length; col++) {
                matrix[row][col] = input.nextDouble();
            }
        }
        double[] sumOfCol =new double[matrix[0].length];  
        for (int i = 0; i < matrix.length; i++) { //row
            for (int j = 0; j < matrix[i].length; j++) { //column
                sumOfRow += matrix[i][j];
                sumOfCol[j] += matrix[i][j];
            }
            System.out.println("Sum of the elements at row " + row + " is: " + sumOfRow);
        }
        System.out.println("Sum of the elements at column " + col + " is: " + sumOfCol);
    }             
}   

4.6
5
P.B 80 points

                                    /* After the prompt code segment and sumOfCol in the main method */

    // Row (major index)
    for (int row = 0; row &lt; matrix.length; row++) {
        int rowSum = 0;
        for (int col = 0; col &lt; matrix[row].length; col++) {
            rowSum += matrix[row][col];
        }
        System.out.println(&quot;Sum of the elements at row &quot; + row + &quot; is: &quot; + rowSum);
    }

    // Column (minor index)
    // Assuming the length of each row is the same
    for (int col = 0; col &lt; matrix[0].length; col++) {
        int colSum = 0;
        for (int row = 0; row &lt; matrix.length; row++) {
            colSum += matrix[row][col];
        }
        System.out.println(&quot;Sum of the elements at col &quot; + col + &quot; is: &quot; + colSum);
    }

4.6 (5 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
adding elements in a specified column or row in a two dimensional array java add row of 3 dimensional array java java can i add rows to a 2d array returns the sum of all elements in a specified column of a two-dimensional array. java returns the sum of all elements in a specified column of a two-dimensional array. arrays are instantiated as rows x column how to create 2D matrix of two column declaration of two dimension with 2 columns and 3 rows for NameString arrays how to displays the elements of 2D arrays in java array two dimensional col and row how to modify a 2d char matrix in a 2d int matrix in java how to change a bideminsional array line to a number java how to set an element in a 2d array in java set 2d array equal to objects string 2d array table how to declare n row and 2 column in java java 2d array 1 is int and 1 is string Last argument should be a two dimensional array '[[]]'. First element in array was a String two dimensinal array table constructor how to initialize entire row of 2d array java setting an array row and column individually java i have values on my 2d array java, how can i implement the values on my jbuttobns] Write a program that returns the sum of all elements in a specified column of a two-dimensional array java two dimensional array draw in java Construct 2 int 2D arrays(a &amp; b) of same dimension. how to get the most direct line in a 2 dimensional array how to contruct a 2d array given two sets Declare and instantiate a static 2-D array of type integer, called scores that that has 3 rows and 4 columns. create 2d array from 1 entry in another 2d array Firstly ask the user to enter a 3-by-4 array. The user should enter the array as follows java Firstly ask the user to enter a 3-by-4 array. The user should enter the array as follows: why the second dimension needs to be sized in 2d array Which statement would correctly retrieve and store the column size of the 2D Array given below? Dim sales(4,4) as Double Write a program that returns the sum of all elements in a specified column of a two-dimensional array. write a program that returns the sum of all elements in a specified column of a two-dimensional array sum 2d array to 1d javascript define different elements of row matrix in python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source