leon k leonardo
how to do this?
1 Answer(s)      3 years ago
Posted in : Java Beginners
  1. Given any integer 2D array, design then implement a Java program that will add to each element in the array the corresponding column number and the corresponding row number. Then, it prints the array before and after modification. For example, if the program fed with following 2D array: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    The output should be as follows:
    

original 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

new 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6

Advertisement
View Answers

April 19, 2012 at 1:24 PM


class TwoDArray{
    public static void main(String[]args){
    int[][] A = {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
    int[][] B = new int[3][5];

    System.out.println("Original Array: ");
        for(int i=0 ; i < A.length ; i++){
            System.out.println();
            for(int j=0 ; j < A[i].length ; j++){
                System.out.print(A[i][j]+" ");
            }
        }
    System.out.println();
    System.out.println();
        for (int i=0 ; i < B.length ; i++){
            for  (int j=0 ; j < B[i].length ; j++){
                B[i][j] = i+j;
            }
        }
        System.out.println("New Array: ");
        for(int i=0 ; i < B.length ; i++){
            System.out.println();
            for(int j=0 ; j < B[i].length ; j++){
                System.out.print(B[i][j]+" ");
            }
        }
     }
  }