/******************************************************************************
 * Example of 2 dimensional arrays
 ******************************************************************************/
public class array2
{
    public static void main (String[] args) 
    {

    //------------ declare, then allocate space ---------------------------

        float[ ][ ] numArray;                   //declare a 2 dim array

        numArray = new float[3][5];              //allocate space for it
    
        numArray[0][0] = 0.0f;                  //store in 1st array 1st elmt    
        numArray[0][1] = 0.1f;                
        numArray[0][2] = 0.2f;                
        numArray[0][3] = 0.3f;                
        numArray[0][4] = 0.4f;                

        numArray[1][0] = 1.0f;                  //store in 2nd array 1st elmt    
        numArray[1][1] = 1.1f;                
        numArray[1][2] = 1.2f;                
        numArray[1][3] = 1.3f;                
        numArray[1][4] = 1.4f;                

        numArray[2][0] = 2.0f;                  //store in 3rd array 1st elmt    
        numArray[2][1] = 2.1f;                
        numArray[2][2] = 2.2f;                
        numArray[2][3] = 2.3f;                
        numArray[2][4] = 2.4f;                

        float element = numArray[2][1];         //access 3rd row, 2nd element
        int   rowSize = numArray.length;
        int   colSize = numArray[0].length;

        System.out.println("Element row3, col2 is:  " + element);
        System.out.println("Size of the array rows: " + rowSize);
        System.out.println("Size of the array cols: " + colSize);

        System.out.println("\n Printing rows then cols (As normal):");

        for (int row=0; row < numArray.length; row++)           //loop thru row dim
        {
            for (int col=0; col < numArray[row].length; col++)  //loop thru col dim
            {
                System.out.print(numArray[row][col] + " ");     //print element value
            }
            System.out.println();             
        }
    
        System.out.println("\n Printing rows then cols (Enhanced 'for' loop):");

        for (float[] row : numArray)                            //loop thru row dim
        {
            for (float element_value : row)                     //loop thru col dim
            {
                System.out.print(element_value + " ");          //print element value
            }
            System.out.println();             
        }

   //------------ Transposing the output -----------------------

        System.out.println("\n Printing cols then rows (Transposed):");

        for(int row=0; row<numArray[0].length; row++)           //loop thru length of row 1
        {
            for(int col=0; col < numArray.length; col++)        //loop thru number of rows
            {
                System.out.print(numArray[col][row] + " ");     //print element value
            }
            System.out.println();             
        }

        System.out.println();                 //print empty line



    //------------ declare, allocate and initialize -----------------------

        char[ ][ ] letters = { {'A','E','I','O','U'},       //vowels        
                   {'B','C','D','F','G','H','J',            //consonants
                    'K','L','M','N','P','Q','R',
                    'S','T','V','W','X','Y','Z'} };   
    
        for (int i=0; i < 2; i++)                           //loop thru 1st dim
        {
            for (int j=0; j < letters[i].length; j++)       //loop thru 2nd dim
            {
                System.out.print(letters[i][j] + " ");      //print element value
            }
            System.out.println();             
        }
    
        System.out.println();                 //print empty line
    


    //------ Multiplication table with random rows and columns --------------

        int[ ][ ] mult;                                 //declare a 2 dim array

        int rows = (int) (Math.random()*10 +1);         //random rows 1-10
    
        mult = new int[rows][];                         //allocate row dimension
    
        for (int i=0; i < mult.length; i++)             //loop thru rows
        {
            int cols = (int) (Math.random()*10 +1);     //random cols 1-10

            mult[i] = new int[cols];                    //allocate col dimension

            for (int j=0; j < mult[i].length; j++)      //loop thru cols
            {
                mult[i][j] = (i+1) * (j+1);             //multiply row+1 * col+1
                System.out.print(mult[i][j] + " ");     //print it
            }
            System.out.println();             
        }
    }
}