Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Array2D{
- public static void main (String[]args){
- //initializing variables
- final int ROWS = 5;
- final int COLS = 4;
- int[][] arr2D = new int [ROWS][COLS];
- Random rand = new Random();
- //fill the array with random data
- for (int y = 0; y < ROWS; y++) {
- for (int x = 0; x < COLS; x++) {
- arr2D[y][x] = rand.nextInt(10);
- }
- }
- //print the data
- for (int y = 0; y < ROWS; y++) {
- for (int x = 0; x < COLS; x++) {
- System.out.print(arr2D[y][x] + "\t");
- }
- System.out.println();
- }
- //print the sum of every row
- int rowSum = 0;
- for (int y = 0; y < ROWS; y++) {
- for(int x = 0; x < COLS; x++) {
- rowSum += arr2D[y][x];
- }
- System.out.println("Sum at row " + y + " : " + rowSum);
- rowSum = 0;
- }
- //print the sum of every column
- int colSum = 0;
- for (int y = 0; y < COLS; y++) {
- for(int x = 0; x < ROWS; x++) {
- colSum += arr2D[y][x];
- }
- System.out.println("Sum at column " + y + " : " + colSum);
- colSum = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement