FunctionX Logo

C# Examples: Jagged Arrays

 

Introduction

This example uses various variables that each represents an array of arrays, also called jagged arrays:

using System;

class DepartmentStore
{
	static int Main()
	{
		// Each of the following variable arrays is structured as [2][2][4].
		// Each variable represents:
		// A/ Two major lists: The first major list represents women items,
		//				       the second major list represents men items,
		// B/ Two minor lists.
		//    Each of the major lists contains two minor lists:
		//	  a/ The first minor list of the first major list contains adult women items
		//		 The second minor list of the first major list contains girls items
		//	  b/ The first minor list of the second major list contains adult men items
		//		 The second minor list of the second major list contains boys items
		// C/ Each minor list contains four items

		long[][][] ItemNumber = 
			new long[][][]{
				new long[][]{
					new long[]{947783, 934687, 973947, 987598, 974937},
					new long[]{743765, 747635, 765473, 754026, 730302}},
				new long[][]{
				         new long[]{209579, 267583, 248937, 276057, 267945},
					new long[]{ 409579, 467583, 448937, 476057, 467945}} };
		string[][][] ItemName =
			new string[][][]{
				new string[][]{
				         new string[]{ "Double-faced wool coat",
				                       "Floral Silk Tank Blouse",
						     "Push Up Bra",
						     "Chiffon Blouse",
						     "Bow Belt Skirtsuit" },
					new string[]{ "Cable-knit Sweater",
					              "Jeans with Heart Belt",
					              "Fashionable mini skirt",
                                                            "Double Dry Pants",
                                                            "Romantic Flower Dress" } },
				new string[][]{
					new string[]{ "Cotton Polo Shirt",
				                       "Pure Wool Cap",
                                                            "Striped Cotton Shirt",
                                                            "Two-Toned Ribbed Crewneck",
                                                            "Chestnut Italian Shoes" },
					new string[]{ "Under Collar and Placket Jacket",
		                                         "Country Coat Rugged Wear",
                                                           "Carpenter Jeans",
		                                         "Double-Cushion Tennis Shoes",
                                                           "Stitched Center-Bar Belt" }} };
		decimal[][][] UnitPrice =
			new decimal[2][][]{
				new decimal[][]{
					new decimal[]{ 275.25M, 180.00M, 50.00M, 265.00M, 245.55M },
					new decimal[]{ 45.55M, 25.65M,	34.55M, 28.55M, 24.95M }},
				new decimal[][]{
					new decimal[]{ 45.75M, 25.00M, 65.55M, 9.75M, 165.75M },
					new decimal[]{ 265.15M, 35.55M, 24.95M, 48.75M, 32.50M }} };

		long    ItemID = 0;
		string  Description = "Unknown";
		decimal Price = 0.00M;
		string Category = "Category";

		// Order Processing
		try 
		{
			Console.Write("Enter Item Number: ");
			ItemID = long.Parse(Console.ReadLine());
		}
		catch(FormatException)
		{
			Console.WriteLine("Invalid Number - The program will terminate\n");
		}

		for(int i = 0; i < 2; i++)
		{
			for(int j = 0; j < 2; j++)
			{
				for(int k = 0; k < 5; k++)
				{
					if( ItemID == ItemNumber[i][j][k] )
					{
						Description = ItemName[i][j][k];
						Price = UnitPrice[i][j][k];

						if( ItemID >= 900000 )
							Category = "Women";
						else if( ItemID >= 700000 )
							Category = "Girls";
						else if( ItemID >= 400000 )
							Category = "Boys";
						else
							Category = "Men";
					}
				}
			}
		}

		Console.WriteLine("Receipt");
		Console.WriteLine("Item Number: {0}", ItemID);
		Console.WriteLine("Description: {0} - {1}", Category, Description);
		Console.WriteLine("Unit Price:  {0:C}\n", Price);

		return 0;
	}
}
 

Home Copyright © 2004-2010 FunctionX, Inc.