• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random numbers in a 2d array  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to create a 2d array with random numbers between 10 to 100, but the compiler tells me that the i have incompatible types when it comes to my random number generator.
any clues?
// create a matrix of size m x n, filled with random numbers between 10 and 100
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The size variable is declared as an array of array of int elements. So, if you dereference it once, you get an array of int variable.

You are not allowed to assign an int value to an array of int variable.

Henry
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i would need to assing the random to a 2d array. like this?

public static int[][] newRandom(int m, int n) {
     
       int[][] size = new int[m][n];
       for(int i = 0; i < m; i++){
           for(int j = 0; j < n ; j++){
            size[i][j] = rand.nextInt(10)*10+1;
             
          }
      }
      return size;
   }

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For 10 (inclusive) to 100 (exclusive)

For 10 (inclusive) to 100 (INclusive)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!