Saltar al contenido

Escriba un programa que devuelva la suma de todos los elementos en una columna especificada de un array ejemplo de código java

Al fin después de mucho trabajar pudimos encontrar el resultado de este conflicto que tantos usuarios de nuestra web han tenido. Si tienes alguna información que compartir puedes aportar tu información.

Ejemplo 1: agregar elementos en una columna o fila especificada en un formato bidimensional array Java

packagemultidimensionalarrays;publicclassMultidimensionalArrayspublicstaticvoidmain(String[] args)double sumOfRow =0;double[][] matrix =newdouble[3][4];java.util.Scanner input =newjava.util.Scanner(System.in);//ScannerSystem.out.println("Enter a 3 by 4 matrix row by row: ");//Prompt user to enter matrix numbersfor(int row =0; row < matrix.length; row++)for(int col =0; col < matrix[0].length; col++)
                matrix[row][col]= input.nextDouble();double[] sumOfCol =newdouble[matrix[0].length];for(int i =0; i < matrix.length; i++)//rowfor(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);

Ejemplo 2: agregar elementos en una columna o fila especificada en un formato bidimensional 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 samefor(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);

Recuerda que puedes recomendar esta reseña si te ayudó.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *