# Thread: noob: two-dimension array

1. Member
Join Date
Dec 2008
Location
North America
Posts
29
Rep Power
0

## 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.
Java 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```

2. 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.

3. Member
Join Date
Dec 2008
Location
North America
Posts
29
Rep Power
0
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?
Java 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```

4. Member
Join Date
Mar 2009
Posts
22
Rep Power
0
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;-)

5. Member
Join Date
Dec 2008
Location
North America
Posts
29
Rep Power
0
so include it in the loop like this?:
Java 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

6. you forgot braces in second for loop.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•