0

I have to write a JAVA program where a user set the number of columns and rows of a 2d array. Then he should choose a minimum and a maximum. After that, the array is filled randomly.

I writed this code:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class array2d
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int rows, col, min, max;

        Scanner scan1 = new Scanner(System.in);

        System.out.println("Enter number of rows and columns:");
        rows = scan1.nextInt();
        col = scan1.nextInt();
        int[][] a = new int[rows][col];
        System.out.println("Enter min and max:");
        min = scan1.nextInt();
        max = scan1.nextInt();

        for(int i = 0; i<rows; i++)
        {
            for(int j = 0; j<col; j++)
            {
                a[i][j] = min - (int)Math.random()*(max-min+1);
            }
        }

        //Display on the screen
        for(int i = 0; i<rows; i++)
        {
            for(int j = 0; j<col; j++)
            {
                System.out.print(a[i][j]+ " ");
            }
        }
        ...
     }
}

Then we should run a program to see if the first number of each row is a divisor of all the row:

for(int i = 0; i<rows; i++)
        {
            for(int j = 0; j<col; j++)
            {
                if(a[i][0]%a[i][j]==0)
                {
                    System.out.println(a[i][j]);
                }
                else
                    System.out.println("None");
            }
        }

That's working properly, but the generated array on my computer is displayed like that (CMD output):

enter image description here

And as you see in the picture, all randomly filled random are equal to the minimum specified.

So how can I display this array as like matrix and why the random numbers are showing like this.

3 Answers3

4

Math#random return a number between 0.0-1.0, but less then 1.0. This does in fact make your calculation look like the following min-0, since you are parsing it as int. Due to this you are allways saying 0*(max-min+1).

In order to achive what you want you need to add braces and add to min.

For your representation problem, add a System.out.println after you finished the inner loop.

for(int i = 0; i<rows; i++)  {
   for(int j = 0; j<col; j++)  {
       a[i][j] = min + (int)(Math.random()*(max-min+1)); // You need brackets and add it to min
   }
}
//Display on the screen
for(int i = 0; i<rows; i++) {
   for(int j = 0; j<col; j++){
       System.out.print(a[i][j]+ " ");
   }
   System.out.println(); // You need a linebreak
}
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
3

All slots of your array get filled wit the 10 value in your example, min in general :

a[i][j] = min - (int)Math.random()*(max-min+1);

yields

a[i][j] = 10 - (int)Math.random()*(10);

Math.random() returns a double greater than or equal to 0.0 and less than 1.0 , so rounded to an int, it is 0 .

so

a[i][j] = 10 - (0*(10)); // this yields 10, the value of "min"
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • @Kevin Esche just posted the answer to the display part , and the other answer by khelwood, tells you how to populate your array correctly. – Arnaud Feb 02 '16 at 14:09
3

Where you have:

a[i][j] = min - (int)Math.random()*(max-min+1);

You actually mean:

a[i][j] = min + (int) (Math.random()*(max-min+1));

The first one casts Math.random() to an int (giving zero), then multiplies it by (max-min+1) (giving zero), then subtracts it from min (giving min).

The second one multiples Math.random() by an int (giving a random double in the range [0,max-min+1) ), then casts it to an int (giving a random int in the range [0,max-min] ), and then adds min (giving a random int in the range [min,max] ).

khelwood
  • 55,782
  • 14
  • 81
  • 108