Saturday, February 4, 2012

4.2. Two-dimensional arrays

We can declare an array with two or more dimensions. For example, if you want we save data from two groups of students, each of which has 20 students, we have two options:

Ø     We can use int studentData[40] but then we must remember that the first 20 data are actually a group of students and a the other 20 belong to another group. It is "too traditional", so we will not see more details on it.
Ø     We can use  int studentData[2,20] and then we know that the data which look like studenData[0, i] belong to the first group, and studentData[1, i] are part of the second group.
Ø     An alternative, which may sound more familiar to one who has already programmed in C is  int studentData[2][20] but in C# this is not exactly the same meaning as [2,20], because there are two arrays , whose elements are arrays of 20 elements. Indeed, they may even be two arrays of different sizes, as we will see in a later example.

If we specify initial values, we will uses braces, as if it were a one-dimension array.

Let's see a first example of arrays with the form [2,20], which might be called the "Pascal-style". We will use both arrays with default values, and arrays in which we allocate space with the "new" command and we assign values later:

/*---------------------------*/
/*  C# Example #37:          */
/*  example37.cs             */
/*                           */
/*  Two-dimensional arrays   */
/*  Pascal style             */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example37
{
  public static void Main()
  {

    int[,] marks1 = new int[2,2]; // 2 blocks of 2 numbers
    marks1[0,0] = 1;
    marks1[0,1] = 2;
    marks1[1,0] = 3;
    marks1[1,1] = 4;
 
   int[,] marks2 = // 2 blocks of 10 numbers
    {
      {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
      {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
    };

    Console.WriteLine("The mark1 for the second student in group 1 is {0}",
      marks1[0,1]);
    Console.WriteLine(" The mark2 for the third student in group 1 is {0}",
      marks2[0,2]); 
  }
}


Such tables of several dimensions are also used to store matrices, when it comes to more complex math problems that we've seen so far.

The other way to have multi-dimensional arrays are "arrays of arrays" (also called "jagged arrays", compared to the "rectangular arrays"w). Which, as mentioned, may have elements of different sizes. If we need to know their length, we can use "a.Length":

/*---------------------------*/
/*  C# Example #38:          */
/*  example38.cs             */
/*                           */
/*  Two-dimensional arrays   */
/*  C-like style             */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example38
{
  public static void Main()
  {

    int[][] marks;         // Two-dimensional array
    marks = new int[3][];  // 3 blocks of data
    marks[0] = new int[10];  // 10 marks in one group
    marks[1] = new int[15];  // 15 marks in another group
    marks[2] = new int[12];  // 12 marks in the last group

    // We assign example values
    for (int i=0;i<marks.Length;i++)
    {
      for (int j=0;j<marks[i].Length;j++)
      {
        marks[i][j] = i + j;
      }
    }
   
    // And we show these values
    for (int i=0;i<marks.Length;i++)
    {
      for (int j=0;j<marks[i].Length;j++)
      {
        Console.Write(" {0}", marks[i][j]);
      }
      Console.WriteLine();
    }

  }
}

The output of this program would be

0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 3 4 5 6 7 8 9 10 11 12 13


Suggested exercises:
·       A program that asks the user for two blocks of 10 integers (using a rectangular array). Then it will show the greatest number that has been entered in each block.
·       A program that asks the user for two blocks of 6 strings. Then it will ask the user for a new string and check if it appears in any of the previous two sets of information.
·       If you have studied matrix algebra:
·       A program that computes the determinant of a 2x2 matrix.
·       A program that computes the determinant of a 3x3 matrix.
·       A program that calculates whether the rows of a matrix are linearly dependent.
·       A program that uses arrays to solve a system of linear equations using the Gauss method.


No comments:

Post a Comment