Core C#

Multidimensional Arrays

A multidimensional array is an array of arrays, possibly with more levels. Generally, we call an array of arrays a two-dimensional array. Below, we give an example of a two dimensional array that contains the number of miles traveled on each of four days for each of three evangelists, The two dimensions of the array are the evangelist and the day. The first index indicates the evangelist, while the second index indicates the day.

Program.cs

using System;

namespace XoaX {
    class Program {
        static void Main(string[] args) {
            // ***** Rectangular Array Example *****
            // The miles traveled each day by each of the three evangelists.
            double[,] daaMilesTraveled = new double[3, 5] {
                {6.11, 5.00, .88, 3.82, 3.9},
                {4.2, 6.9, 9.4, 0.8, 5.5},
                {1.9, 2.1, 1.4, .57, .29}
            };

            Console.WriteLine("Miles Traveled");
            Console.WriteLine("--------------");
            for (int iEvangelist = 0; iEvangelist < daaMilesTraveled.GetLength(0); ++iEvangelist) {
                Console.Write("Evangelist #" + iEvangelist + ":  ");
                for (int iDay = 0; iDay < daaMilesTraveled.GetLength(1); ++iDay) {
                    Console.Write(daaMilesTraveled[iEvangelist, iDay] + "  ");
                }
                Console.WriteLine("");
            }
            Console.WriteLine("");
        }
    }
}
 

Output

Miles Traveled
--------------
Evangelist #0:  6.11  5  0.88  3.82  3.9
Evangelist #1:  4.2  6.9  9.4  0.8  5.5
Evangelist #2:  1.9  2.1  1.4  0.57  0.29

Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.