• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random numbers in a 2d array

 
Ranch Hand
Posts: 90
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
  }

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For 10 (inclusive) to 100 (exclusive)

For 10 (inclusive) to 100 (INclusive)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic