Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

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);
    }             
}   
Comment

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

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

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

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

PREVIOUS NEXT
Code Example
Typescript :: add redux to react typescript 
Typescript :: exclude folder from typescript compiler tsconfig.json 
Typescript :: uncheck all checkboxes typescript 
Typescript :: combine two lists c# 
Typescript :: error NG8001 
Typescript :: window ethereum types 
Typescript :: typescript formik useFormik 
Typescript :: eslint absolute imports error 
Typescript :: after effects free download 
Typescript :: how to compile typescript 
Typescript :: ion modal dismiss 
Typescript :: @react-navigation/native route typescript 
Typescript :: vscode add all missing imports shortcut 
Typescript :: close mat dialog programmatically 
Typescript :: python get first n elements of list 
Typescript :: how to call a export constants in nodejs 
Typescript :: how to check if a string is composed only of alphabets in python 
Typescript :: push at first index typescript 
Typescript :: typescript array of objects interface 
Typescript :: useselector typescript 
Typescript :: select column values from array typescript 
Typescript :: deno current directory 
Typescript :: how ro execute typescript file 
Typescript :: import google fonts to flutter 
Typescript :: DAX check if value exists in another table 
Typescript :: how to get value from observable 
Typescript :: typescript list concat 
Typescript :: typescript interface property multiple types 
Typescript :: typescript type or null 
Typescript :: define typescript types 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =