Sum of two-dimensional column arrays

I have a two-dimensional array as shown below:

[33] [96] [34] [11] [65] [68] [12] [8] [= 327]

[94] [91] [3] [20] [16] [59] [74] [97] [= 454]

[29] [0] [31] [13] [4] [63] [52] [73] [= 265]

[51] [3] [55] [74] [52] [79] [61] [74] [= 449]

[18] [19] [1] [53] [33] [93] [26] [14] [= 257]

[56] [41] [4] [16] [45] [8] [57] [22] [= 249]

[43] [33] [43] [59] [61] [58] [58] [69] [= 424]

[38] [41] [42] [29] [27] [72] [85] [75] [= 409]

[36] [3] [23] [65] [40] [56] [41] [96] [= 36]

[=?] [=?] [=?] [=?] [=?] [=?] [=?] [=?]

How to get the sum of columns in an array?

here is my code to get the sum of the lines:

public static void main(String[] args) { int[][] nums = new int[9][9]; int random, total_rows=0, total_cols=0; Random randomGenerator = new Random(); for (int i=0; i < 9; i++) { for (int j=0; j < 9; j++) { random = randomGenerator.nextInt(100); nums[i][j] = random; if(j < 8) { total_rows += random; }else { System.out.print("="); nums[i][j] = total_rows; total_rows = 0; } System.out.print(nums[i][j] + " "); } System.out.println(); } } 
0
source share
7 answers

You can try:

 int[][] num = new int[9][9]; /* * ...populate the array * */ for (int i = 0; i < num.length; i++) { int sum = 0; for (int j = 0; j < num[i].length; j++) { sum += num[j][i]; } System.out.println(sum); } 
+4
source

I would separate your array generation code from your sum / display code.
Then you should notice that all you have to do is flip i and j when accessing the array in the inner loop.

+2
source

Home question :)

So, instead of giving you the code, I will tell you that you are already close to the answer.

To count the columns, you need a separate array, and then, creating each column in each row, just add this value to the desired position.

At the end of the day you will get the total number of rows, as well as the total number of columns. Given what you have done so far, it should be easy for you to do.

+1
source

Here is the code:

Demo code for the sum of rows and columns of a 2d array

 import java.util.Random; public class SumOfColumns { public static void main(String[] args) { int[][] nums = new int[9][9]; handleArray(nums); printArray(nums); } private static int getSumOfRow(int[][] array, int rowNum) { int sumOfRows = 0; for(int i = 0; i < array[rowNum].length; i++) sumOfRows += array[rowNum][i]; return sumOfRows; } private static int getColumnSum(int[][] array, int columnNum) { int sumOfColumn = 0; for(int i = 0; i < array.length; i++) sumOfColumn += array[i][columnNum]; return sumOfColumn; } private static void handleArray(int[][] array) { Random randomGenerator = new Random(); for (int i=0; i < array.length; i++) { for (int j=0; j < array[i].length; j++) { int random = randomGenerator.nextInt(100); array[i][j] = random; } } } private static void printArray(int[][] array) { System.out.printf(" "); for(int i = 0; i < array.length; i++) System.out.printf("%4d ", i); System.out.println("\n __________________________________________________"); for(int i = 0; i < array.length; i++) { System.out.printf("%4d | ", i); for(int j = 0; j < array[i].length; j++) System.out.printf("%4d ", array[i][j]); System.out.printf("| Sum = %4d", getSumOfRow(array, i)); System.out.println(); } System.out.println(" __________________________________________________"); handleSumOfColumnPrint(array); } private static void handleSumOfColumnPrint(int[][] array) { System.out.printf(" "); for(int i = 0; i < array[0].length; i++) System.out.printf("%4d ", getColumnSum(array, i)); } } 

Output

  0 1 2 3 4 5 6 7 8 __________________________________________________ 0 | 1 86 95 71 78 21 85 99 35 | Sum = 571 1 | 45 82 68 75 30 1 71 94 8 | Sum = 474 2 | 33 78 61 74 52 75 63 8 1 | Sum = 445 3 | 17 10 99 78 61 73 63 40 8 | Sum = 449 4 | 66 68 54 74 28 78 13 4 79 | Sum = 464 5 | 75 60 79 76 55 14 20 28 27 | Sum = 434 6 | 17 17 29 39 6 90 0 94 75 | Sum = 367 7 | 57 89 73 27 28 42 92 36 16 | Sum = 460 8 | 40 44 79 64 92 90 43 60 53 | Sum = 565 __________________________________________________ 351 534 637 578 430 484 450 463 302 
+1
source
 Array[][] // Vertical, Horizontal. //sum of row n: sum=0 for(int i:Array[n]){ sum+=i; } //sum of column n: sum=0 for(int i=0;i<Array.length();i++){ sum+=Array[i][n]; } 

Hope this makes sense.

0
source

Question about homework, if possible?

If so, maybe some tips would be more helpful?

You calculate the total amount of a row when generating a 2d array - since you do this row by row, it is easy for you to calculate this for rows (since you can create it as you move), but more complicated for columns, since you actually lose track of previous rows on each iterations.

If you only generate an array in the first one for the loop, you will have a fully populated 2d array, but no row / column totals. But the data remains in your array, so now you can write code to calculate these totals.

As a small hint: if you wrote another loop after generating the data line by line:

 int columnTotal = 0; for (int r=0; r < 9; r++) { columnTotal += nums[r][0]; } 

which should allow you to summarize the values โ€‹โ€‹of the first column (column 0).

Hope this helps :-)

0
source

Here is my code to solve the problem ...

 import java.util.Random; public class columnsAndRowsSummatory { static int x = 0; static int y = 0; public static void main (String[] arg) { String spaces = " " ; //There are 7 spaces on the spaces String int[][] nums = new int[9][9]; int random, rowTotal = 0, colTotal = 0; Random randomGenerator = new Random(); String ColTotal = ""; for ( x = 0; x < nums.length -1; x++) { rowTotal = 0; for (y = 0; y < nums.length ; y++) { random = randomGenerator.nextInt(10000); nums[x][y] = random; while( y < nums.length -1 ) { rowTotal = rowTotal + random; break; } if(y < nums.length -1 && x != nums.length -1 && random != 0) { int z = (int)Math.floor(Math.log10(random) ); System.out.print(random + spaces.substring( (z) ) ); //This method calculates how many digits does the number 'random' //Have and takes that amount away from the spaces string to even //Out numbers displayed } else if(random == 0 && x != nums.length -1) { System.out.print(random + spaces); } else { System.out.printf("= %d ", rowTotal); } } System.out.println(); } for(x = 0; x < nums.length -1; x++) { for (y = 0; y < nums.length -1 ; y++) { colTotal = colTotal + nums[y][x]; } int z = (int)Math.floor(Math.log10(colTotal) ); ColTotal = ColTotal + colTotal + spaces.substring(z) ; colTotal = 0; } System.out.print(ColTotal + "<-Totals^"); } } 

Now, this code will display a neat array of the number of rows and columns that you want, and display the column / row totals in the corresponding last row of the array. If you were dealing with numbers over 10 million, then all you have to do is add a space to the space bar, and you're good to go.

For example, if you have

 random = randomGenerator.nextInt(999999); 

It will display something like this:

 711778 119342 62648 8035 652270 344935 101175 265275 174969 = 2440427 414592 798519 453329 988340 180647 540748 572587 675951 903581 = 5528294 747535 955957 47966 742898 773810 34212 914212 625716 209569 = 5051875 492510 998618 622957 517383 704277 397076 970357 280134 842993 = 5826305 190916 57734 590635 944535 991653 366530 145640 402605 456611 = 4146859 117598 612710 967466 67209 660383 247281 304953 141144 260186 = 3378930 635384 660332 768930 340590 800611 758383 41154 289514 189504 = 4484402 173484 740549 226505 921889 135020 555594 554520 65581 433272 = 3806414 796617 601478 77503 161865 891582 715084 14614 189920 568788 = 4017451 4280414 5545239 3817939 4692744 5790253 3959843 3619212 2935840 4039473 <-Totals^ 
0
source

Source: https://habr.com/ru/post/1273209/


All Articles