Results 1 to 6 of 6
  1. #1
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Post 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. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Default

    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. #4
    einarb is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by bobmasta5 View Post
    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. #5
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Default

    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. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you forgot braces in second for loop.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Help im a noob.. a super noob on java..
    By critdevil in forum New To Java
    Replies: 12
    Last Post: 03-07-2009, 03:17 AM
  2. Noob
    By nokomis in forum Introductions
    Replies: 2
    Last Post: 03-06-2009, 05:10 PM
  3. JPanel in agreement with dimension of pictures
    By KamilR in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2008, 07:38 AM
  4. Replies: 2
    Last Post: 04-27-2008, 07:51 PM
  5. Arraylist to a 2- dimension array conversion
    By mars123 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 11:24 AM

Posting Permissions

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