Search code examples
javamultidimensional-arraydivisionmultiplication

Two dimensional array (multiplication/division)


I currently have this class created called Array Math which is loading into the 10x10 array the multiplication by location as shown in the image displayed under the code, however what I want to do is divide each location by 2 after the multiplication. So in other words (row * column) / 2 at the current moment i'm just loading into the array these numbers. I'm not sure how to approach this logically since i'm using two for loops to produce the multiplication between rows and columns.

class ArrayMath
{
        private static final int tableSize = 10;
        public static void main(String[] args)
        {
                int table[][] = new int [tableSize][tableSize];
                for (int row = 1; row <= 10; row++)
                {
                        for (int col = 1; col <= 10; col++)
                        {
                                System.out.printf(row * col +"\t");
                        }
                        System.out.println();
                }
        }
}

Output of array


Solution

  • All your need to do is add /2.0. Order of operations takes care of the rest

    System.out.printf( ( row * col / 2.0 ) + "\t" );
    

    The reason you need to use 2.0 it because otherwise Java will round the result.