Originally Posted by
coasterguy10
For finding the sum of the rows you want to keep row m the same and only add up what is in that row. The code you have now is set to a loop that will add the element of every index to the sum. To fix the problem, delete your first for loop, "for (m = 0; m < arr.length; m++)". This way row m will stay constant and the loop will not add elements from other rows. Your sum should just be sum+= arr[m][num]. The same goes for adding the elements in the specified column. Right now your loop would not work correctly even if you wanted to add everything in the array because of your second for loop. Your parameters state that num2 must be less then n for the loop to continue, but what if n was 0? The loop wouldn't even start because 0 is not less then 0 (I know this sounds patronizing). I would just take out the second for loop and keep your variables as num and n. Then change the for loop to read, "for (int num = 0; num < arr[n].length; num++)". Then your sum should be written as sum+= arr[num][n]; You put n in the second set of braces because that is what determines the column of the array. By keeping n the same for eveything the program sums, once it adds all the elements in column n, the loop will stop. The same for why m goes in the first set of braces for adding elements in the row. And make sure you import Scanner at the top of your program so you can read in the input. Hope this helps and makes sense.