# noob: two-dimension array

• 03-15-2009, 07:48 PM
bobmasta5
noob: two-dimension array
Create a program that generates a 10x10 table. Your program should fill the table with random integers between 0 and 9. Display the contents of your table on the screen as a 10x10 grid. AFTER the numbers have been generated and displayed on the screen, calculate and display the total of the 100 numbers generated.
-I must use multi-dimensional arrays to create this program
Here is my code as of now and it does not run, are my brackets and loops properly placed? Any help is much appreciated.
Code:

```public static void main (String[] args)     {         c = new Console ();         //create a 10*10 table         int table[] [];         table = new int [10] [10];         //declare a value that will make a random number between 0 and 9         int randomInt = 0;         randomInt = ((int) (Math.random () * 9)) + 1;         //create a loop to put the random values of 0 to 9 in         int i, j;         //two loops, i for the rows         for (i = 0 ; i < 10 ; i++)         {             table [i] [0] = randomInt;         }         //second loop, j for the columns         for (j = 0 ; j < 10 ; j++)         {             table [0] [j] = randomInt;         }         //every element is assigned the random integer         //print the table on the output screen         c.println (table [i] [j] + " ");     } // main method```
• 03-15-2009, 08:11 PM
angryboy
to access a multi-array, you need two loops inside one another. one for rows, another for cols.

and you shouldn't use random numbers to test your code. start by using fixed numbers first.
• 03-15-2009, 10:46 PM
bobmasta5
k so i did a normal multi-dimensional array and figured some more stuff out, here my code now, and ive gotten some help, and now i just need to print every number in the array, do i need 2 more loops to somehow do it?
Code:

```public static void main (String[] args)     {         c = new Console ();         //create a 10*10 table         int table[] [];         table = new int [10] [10];         //declare a value that will make a random number between 0 and 9         int randomInt = 0;         randomInt = ((int) (Math.random () * 9)) + 1;         int i, j;         //m equals 10 because there is 10 rows         int m = 10;         //n equals 10 because there is 10 columns         int n = 10;         //create a loop to put the random values of 0 to 9 in         //two loops, i for the rows         for (i = 0 ; i < m ; i++)         {             //second loop, j for the columns             for (j = 0 ; j < n ; j++)                 //every element is assigned the random integer                 table [i] [j] = randomInt;         }             } // main method```
• 03-16-2009, 12:15 AM
einarb
Quote:

Originally Posted by bobmasta5
k so i did a normal multi-dimensional array and figured some more stuff out, here my code now, and ive gotten some help, and now i just need to print every number in the array, do i need 2 more loops to somehow do it?

Either that or include it in the loops you already got. You will probably want to move the random a bit around too, but you'll see that once you print the numbers;-)
• 03-16-2009, 12:23 AM
bobmasta5
so include it in the loop like this?:
Code:

```for (i = 0 ; i < m ; i++)         {             //second loop, j for the columns             for (j = 0 ; j < n ; j++)                 //every element is assigned the random integer                 table [i] [j] = randomInt;             c.println (table [i] [j]);         }```
when i click run and it compiles in then says error: indexOutOfBoundsException:10
• 03-16-2009, 12:42 AM
angryboy
you forgot braces in second for loop.