Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import java.util.Random;
  2. public class Array2D{
  3. public static void main (String[]args){
  4.  
  5. //initializing variables
  6. final int ROWS = 5;
  7. final int COLS = 4;
  8. int[][] arr2D = new int [ROWS][COLS];
  9. Random rand = new Random();
  10.  
  11. //fill the array with random data
  12.  
  13. for (int y = 0; y < ROWS; y++) {
  14. for (int x = 0; x < COLS; x++) {
  15. arr2D[y][x] = rand.nextInt(10);
  16.  
  17. }
  18.  
  19. }
  20.  
  21. //print the data
  22. for (int y = 0; y < ROWS; y++) {
  23. for (int x = 0; x < COLS; x++) {
  24. System.out.print(arr2D[y][x] + "\t");
  25. }
  26. System.out.println();
  27. }
  28.  
  29. //print the sum of every row
  30. int rowSum = 0;
  31. for (int y = 0; y < ROWS; y++) {
  32. for(int x = 0; x < COLS; x++) {
  33. rowSum += arr2D[y][x];
  34. }
  35. System.out.println("Sum at row " + y + " : " + rowSum);
  36. rowSum = 0;
  37. }
  38.  
  39. //print the sum of every column
  40. int colSum = 0;
  41. for (int y = 0; y < COLS; y++) {
  42. for(int x = 0; x < ROWS; x++) {
  43. colSum += arr2D[y][x];
  44. }
  45. System.out.println("Sum at column " + y + " : " + colSum);
  46. colSum = 0;
  47.  
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement