Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 30th, 2013, 12:32 AM
 Soccerscode
Registered User

Join Date: Jun 2013
Posts: 9
Time spent in forums: 3 h 39 m 22 sec
Reputation Power: 0
JAVA 2d Array help

Having trouble to get the array to print to the screen and correctly at that. It is supposed to spit out 10 rows and 10 columns of random numbers between 1-1000. The way it is set up it should spit out 10 rows/columns of 0's. But cannot figure out how to get it to show that. Here is what the program is supposed to do.

Your program must do the following:
1. Create a two-dimensional array and use 10 rows of 10 columns. Each row in this array will be treated as an individual one-dimensional array.
2. Fill each array individually with random numbers between 1 and 1000. To do this, create and use a method that only takes a one-dimensional array as a parameter.
3. Sort each array individually using any sorting algorithm you choose. Again, create and use a method that only takes a one-dimensional array as a parameter.

I think I have number 1 fairly covered. Any help setting up number 2 would be appreciated.

*Edit*
Got it spitting out random numbers and the method is being called correctly. However, i think there is a problem with my method because it is using a 2d array as a parameter instead of a 1d array. Any ideas how I could do this and still fill the whole 10 rows and columns in?

Code:
```package program7;

import java.util.Random;

public class Program7 {

private static final int ROW_SIZE = 10;
private static final int COL_SIZE=10;
private static final Random rand = new Random();

public static void main(String[] args) {
int[][] arrayGrid = new int[ROW_SIZE][COL_SIZE];
Random rand = new Random();
initGrid(arrayGrid);

}
public static void initGrid(int[][] grid) {
for (int i = 0; i < ROW_SIZE; i++) {
for (int j = 0; j < COL_SIZE; j++) {
grid[i][j] = rand.nextInt(1000);
System.out.print(grid[i][j]+" ");
}
System.out.println("");
}
}

}
```

 Viewing: Dev Shed Forums > Programming Languages > Java Help > JAVA 2d Array help