0
array java help
i want to write a code to sum all elements in the array by the column. https://code.sololearn.com/ca9obHQyHt6R write it as a code for more understandnig because my english language poor
14 Respostas
+ 1
STOP
Change your last loops
+ 1
STOP
No check again summing column by column.
If you want sum of all elements then doesn't make sense to get by column.
I have given example also in code, you can check.
+ 1
STOP
{1, 4},
{5, 6}
Tell me what should be output in this case?
0
The code works, it sums all the element. What is the problem?
0
No read the full qustionIpang
0
Okay, sorry ...
So for this array example, what is the expected output?
{
{1, 2, 3},
(4, 5, 6)
}
0
Ipang i didnt understand what you mean
0
How do you want to sum by the column, if the 2D array has data such as the sample array I've written?
0
This will give the sum of 1st column
import java.util.Scanner;
public class Summing2Darrayelements {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
// 2.1
// fourth way
int [][] TwoDArray= new int [2][2];
// initializing 2D array using for loop
System.out.println("Please enter the elements :");
for(int row=0; row< TwoDArray.length; row++) {
for(int col=0; col< TwoDArray[row].length; col++) {
TwoDArray[row][col]= input.nextInt();
}}
// summing array elements
int total=0;
int col=0;
for(int row=0; row<2; row++) {
total+= TwoDArray[row][col];
}
System.out.println("the total sum of array element is: "+total);
}
}
0
STOP
Do this
int total=0;
for(int row=0; row< TwoDArray.length; row++) {
total = 0;
for(int col = 0; col < TwoDArray[row].length; col++) {
total += TwoDArray[col][row];
}
System.out.println("Column wise sum of array element is: " + total);
}
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ where? In witch line?
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Here it only sum the first column, I want it to sum all columns
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ its not sum all column