+ Reply to Thread
Page 1 of 3
1 2 3 LastLast
Results 1 to 10 of 23

Thread: Multidimensional Arrays

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,035

    Multidimensional Arrays

    Multi-dimensional Arrays

    This is a tutorial about multi-dimensional arrays, however a quick review of 1-dimensional arrays. The understanding of a 1-dimensional array is crucial to understanding 2 and 3-dimensional arrays.

    When you first learn about arrays in a programming language, you think of a bunch of blocks. That are containers for similar variables. This is a 1 dimensional array.



    That is a representation of a 1-dimensional array that can hold 6 elements. With arrays you can access items by position not content. Thus the array is the basic data structure that is used in the implementation of more complicated data structures like linked structures and hash tables.

    In Java and other languages this array is declared like this:

    Code:
    int[] arnNums = new int[6]; // an array that can hold 6 elements
    If you are used to C++ you might see this written as:

    Code:
    int arnNums[] = new int[6];
    Both of these forms are acceptable, use whichever you would like.

    Populating the array

    Code:
    int[] arnNums = {0,1,2,3,4,5}; // declaring and initializing an array to hold 6 elements
    You can also use a loop to populate the array.

    Example:

    Code:
    for (int i=0;i<6;i++) {
    	arnNums[i] = i;
    }
    You can access how many elements are in the array by using the length property.

    Code:
    arnNums.length; // returns 6 since there are 6 elements in the array.
    Before you continue try this problem to make sure you have this concept down.


    1. Have the user input 10 numbers. Positive and negative numbers are allowed. The numbers will all be real numbers. With this set of 10 numbers you want to calculate:
    a) Integer Mean (Don't use reals)
    b) Median
    c) Largest number
    d) Smallest number
    e) Number of even numbers
    f) Number of odd numbers
    Two-dimensional Array

    A two dimensional array is really just an array of an array. So each element in the array is an 1-dimensional array. Think of a 2-dimensional array as a grid like a chessboard.

    Example:



    This diagram is showing how each element has 2 indexes not 1. This is what makes it a 2-dimensional array.

    Remember that is really just an array of arrays. The first number says what sub array the element is in, and the second index tells the exact number in the array.

    You can populate a 2d array using the brace bracket notation in the same manner as a 1 dimensional array.



    This diagram shows what value is located at each position in the array. So there is a 5 at [0,0], [0,1], [0,2], etc.

    The above array can be constructed like this:

    Code:
    int[][] arnGrid = { 
    {5,5,5,5,5,5},
    {5,5,5,5,5,5},
    {4,3,3,2,1,-5},
    {2,2,2,5,2,3}
    };
    Since each element of the array is really just another array. We can get the number of elements in each individual sub array by using:
    Code:
    arnGrid[0].length
    We can also get the number of sub arrays by using:

    Code:
    arnGrid.length
    This allows us to perform a loop through the entire array printing the grid.

    Code:
    for (int i=0;i<arnGrid.length;i++) {
                for (int x=0;x<arnGrid[i].length;x++) {
                    System.out.print(arnGrid[i][x] + " ");
                }
                System.out.println();
    }
    So the outer loop executes once for each sub array in the 2-dimensional array. Then the inner loop executes once for each element in the sub array. The output is:

    5 5 5 5 5 5
    5 5 5 5 5 5
    4 3 3 2 1 -5
    2 2 2 5 2 3
    So can you get the user to input numbers and store them in the array?


    Note that you can also declare a 2-dimensional array like this:

    Code:
    int[][] arnNums = new int[5][6];
    This means that there will be 5 subarrays which will each hold 6 elements.

    This can also be written in C++ style as:

    Code:
    int arnNums[][] = new int[5][6];
    Before we move on any farther let's try a few challenges:

    1. Create a multiplication table. Have the user enter two numbers 5 <= x <= 7 and 7 <= y <= 9
    The program will create a multiplication table from 0*x-1 to x-1*y-1. Make sure you get the alignments correct.

    For example:

    x = 3
    y = 5

    I want to create a table from 0*3 to 2*4:

    0 0 0 0 0
    0 1 2 3 4
    0 2 4 6 8

    Assume that you are required to store the multiplication table for later use.

    2. Have the user enter a 2-dimensional array of numbers and determine if it's a magic square. The user will enter 2 numbers x and y which can be at maximum 50,000. A magic square is a grid where all the rows, columns and diagonals add to the same number.
    Navigating 2-Dimensional Arrays

    I like to keep two arrays one array that holds information on how I can move left or right in the grid, and another array that holds information on how to move up or down. These are two integer arrays that can only hold 1, 0, or -1. 0 means that I am not moving in that direction. 1 means that I am moving either left or up. -1 means that I am moving down or right.

    Example:

    Code:
    int[] x = {0,1,0,-1};
    int[] y = {1,0,-1,0};
    This covers the four main directions: "north", "east", "south" and "west". To move north 1 in the array I add 1to the "y" coordinate and x doesn't change. To move east I add 1 to the x coordinate and y does not change. This covers moving in the four main directions. Now I can easily move in north-east and other directions similar to that.

    A for loop allows me to loop around a current square in all directions.

    Code:
    for (nDir=0;nDir<4;nDir++) {
    	dx = x; // current x coordinate
    	dy = y; // current y coordinate
    	dx += x[nDir]; // changing x coordinate to one of the squares around [dx][dy]
    	dy += y[nDir]; // changing y coordinate to one of the squares around [dx][dy]
    
    	// now it is very important to make sure we have not gone out of bounds
    	if (dx < 0 || dx > 5 || dy < 0 || dy > 6) {
    		// this is out of bounds so we don't go any further
    		continue;
    	}
    	// in bounds so continue processing here
    }
    Note that it is very important to reset the coordinates of where you are every time. Also bounds checking is important. We don't want to check if a square is a mine if it is not in the grid.

    A quick note in Java, a string is an array of characters so an array of strings is a 2d array.

    The 2-Dimensional Array comes in handy when implementing graphs.

    Now have some fun with 2-dimensional array problems:

    http://dwite.ca/old/Problem2Jan2005.pdf
    http://dwite.ca/old/Problem3Nov2002.pdf
    http://dwite.ca/old/Problem4Oct2002.pdf
    http://dwite.ca/old/Problem4Oct2005.pdf

    It might be fun to talk about my various challenges and the DWITE questions.

    James

    Some resources:

    Arrays (Java 2 Platform SE v1.4.2)
    Multi-Dimensional Arrays
    Javanotes 5.0, Section 7.5 -- Multi-dimensional Arrays
    Last edited by chili5; 03-17-2009 at 12:07 PM.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    12,912
    Blog Entries
    57

    Re: Multidimensional Arrays

    +rep for you! Come back, one day!
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,751
    Blog Entries
    97

    Re: Multidimensional Arrays

    Very informative tutorial! +rep.

  4. #4
    Programmer fread is on a distinguished road fread's Avatar
    Join Date
    Nov 2008
    Location
    Caribbean
    Age
    25
    Posts
    183

    Re: Multidimensional Arrays

    Sweet
    Perfection of means and confusion of ends seem to characterize our age. Albert Einstein

  5. #5
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,035

    Re: Multidimensional Arrays

    Great thanks everyone!

    What do you mean "Come back, one day!"?
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  6. #6
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    12,912
    Blog Entries
    57

    Re: Multidimensional Arrays

    It's a reference to the "Soup Nazi" Seinfield episode. Basically: I expect a new tutorial tomorrow.
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #7
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,035

    Re: Multidimensional Arrays

    Okay sure! Any ideas for a topic?

  8. #8
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    12,912
    Blog Entries
    57

    Re: Multidimensional Arrays

    Not off hand... 4 dimensional arrays?
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  9. #9
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,035

    Re: Multidimensional Arrays

    Sure sounds good. It would be good to write about something I don't already know about.

  10. #10
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    17
    Posts
    4,839
    Blog Entries
    10

    Re: Multidimensional Arrays

    Yet another excellent tutorial of yours mate. They are always so highly detailed and understandable even thought I don't know Java.

    Thank-you. +rep
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

+ Reply to Thread
Page 1 of 3
1 2 3 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Multi-dimensional arrays
    By WingedPanther in forum C Tutorials
    Replies: 5
    Last Post: 11-15-2009, 06:40 PM
  2. Char arrays in C
    By outsid3r in forum C Tutorials
    Replies: 4
    Last Post: 07-28-2009, 12:38 PM
  3. Initializating multidimensional arrays
    By ThemePark in forum C and C++
    Replies: 3
    Last Post: 12-20-2008, 07:03 AM
  4. Returning multidimensional arrays from function
    By ThemePark in forum C and C++
    Replies: 11
    Last Post: 12-01-2008, 07:04 AM
  5. Replies: 0
    Last Post: 05-07-2008, 04:36 AM