array java help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

26th Sep 2021, 8:48 AM
STOP
STOP - avatar
14 Answers
+ 1
STOP Change your last loops
26th Sep 2021, 9:40 AM
A͢J
A͢J - avatar
+ 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.
26th Sep 2021, 11:58 AM
A͢J
A͢J - avatar
+ 1
STOP {1, 4}, {5, 6} Tell me what should be output in this case?
26th Sep 2021, 12:27 PM
A͢J
A͢J - avatar
0
The code works, it sums all the element. What is the problem?
26th Sep 2021, 9:17 AM
Ipang
0
No read the full qustionIpang
26th Sep 2021, 9:23 AM
STOP
STOP - avatar
0
Okay, sorry ... So for this array example, what is the expected output? { {1, 2, 3}, (4, 5, 6) }
26th Sep 2021, 9:29 AM
Ipang
0
Ipang i didnt understand what you mean
26th Sep 2021, 9:30 AM
STOP
STOP - avatar
0
How do you want to sum by the column, if the 2D array has data such as the sample array I've written?
26th Sep 2021, 9:34 AM
Ipang
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); } }
26th Sep 2021, 9:34 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
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); }
26th Sep 2021, 9:36 AM
A͢J
A͢J - avatar
26th Sep 2021, 9:37 AM
STOP
STOP - avatar
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
26th Sep 2021, 10:33 AM
STOP
STOP - avatar
26th Sep 2021, 12:00 PM
STOP
STOP - avatar