the use of a three-dimensional rectangular array : Array Dimension « Collections Data Structure « C# / C Sharp






the use of a three-dimensional rectangular array

the use of a three-dimensional rectangular array
 
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example10_8.cs illustrates the use of
  a three-dimensional rectangular array
*/

using System;

public class Example10_8
{

  public static void Main()
  {

    // create the galaxy array
    int[,,] galaxy = new int [10, 5, 3];

    // set two galaxy array elements to the star's brightness
    galaxy[1, 3, 2] = 3;
    galaxy[4, 1, 2] = 9;

    // display the Rank and Length properties of the galaxy array
    Console.WriteLine("galaxy.Rank (number of dimensions) = " + galaxy.Rank);
    Console.WriteLine("galaxy.Length (number of elements) = " + galaxy.Length);

    // display the galaxy array elements, but only display elements that
    // have actually been set to a value (or "contain stars")
    for (int x = 0; x < galaxy.GetLength(0); x++)
    {
      for (int y = 0; y < galaxy.GetLength(1); y++)
      {
        for (int z = 0; z < galaxy.GetLength(2); z++)
        {
          if (galaxy[x, y, z] != 0)
          {
            Console.WriteLine("galaxy[" + x + ", " + y + ", " + z +"] = " +
              galaxy[x, y, z]);
          }
        }
      }
    }

  }

}

           
         
  








Related examples in the same category

1.Demonstrate a two-dimensional arrayDemonstrate a two-dimensional array
2.Sum the values on a diagonal of a 3x3x3 matrixSum the values on a diagonal of a 3x3x3 matrix
3.Initialize a two-dimensional arrayInitialize a two-dimensional array
4.Demonstrate jagged arraysDemonstrate jagged arrays
5.Use the Length array property on a 3-D arrayUse the Length array property on a 3-D array
6.Call GetLength for two dimenional array
7.Demonstrate Length with jagged arraysDemonstrate Length with jagged arrays
8.illustrates the use of a two-dimensional rectangular arrayillustrates the use of a two-dimensional rectangular array
9.initialize a two-dimensional rectangular array, and use the array properties and methodsinitialize a two-dimensional rectangular array, and use the array properties and methods
10.the use of a jagged arraythe use of a jagged array
11.Uses a two-dimensional array to store grades for studentsUses a two-dimensional array to store grades for students
12.Uses a jagged array to store sales figuresUses a jagged array to store sales figures
13.Multidimensional and Jagged Arrays:Jagged ArraysMultidimensional and Jagged Arrays:Jagged Arrays
14.Multi dimensional Arrays 1Multi dimensional Arrays 1
15.Catch OutOfMemoryException
16.Catch IndexOutOfRangeException Exception
17.Defines a 2D Array