Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners how to do this?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


leon k leonardo
how to do this?
1 Answer(s)      2 years and 6 months 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]+" ");
            }
        }
     }
  }


Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com