Need to put the frequency of each number occurrence into a 1D array and output the result. The 2D to 1D part is throwing me off, I’m not terribly comfortable with arrays yet.
public static void main( String[] args ){
int matrix [][]=new int[20][2];
for (int row=0;row<matrix.length;row++){
for (int column=0;column<matrix[row].length;column++)
matrix[row][column]=(int)(Math.random()*12+1);
}
frequency(matrix);
public static int frequency(int [][] matrix){
int [] nums =[12];
int count =0;
for (int i=0;i<matrix.length;i++){
for (int j=0; j<matrix[i].length;j++)
(???)
}
return (?);
Something like this I would guess :
This is a bit of a contrived example, as normally the values to be collected do not nicely fall on an array index, so a hashmap would be a more typical solution.
Actually it would be more fun to multiply 2 random numbers (Like Math.random()*Math.random() + 1 ) to fill the matrix, then you get a nice bell curve instead of boring white noise in your final frequency distribution.