static void Main(string[] args) { int[] lowerBounds = { 2005, 1 }; int[] lengths = { 5, 4 }; //利用Array类型的静态方法,可以创建下标起始不为0的数组 decimal[,] quarterlyRevenue = (decimal[, ])Array.CreateInstance(typeof(decimal), lengths, lowerBounds); Console.WriteLine("{0,4}{1,9}{2,9}{3,9}{4,9}", "Year", "Q1", "Q2", "Q3", "Q4"); int firstYear = quarterlyRevenue.GetLowerBound(0); int lastYear = quarterlyRevenue.GetUpperBound(0); int firstQ = quarterlyRevenue.GetLowerBound(1); int lastQ = quarterlyRevenue.GetUpperBound(1); for (int year = firstYear; year <= lastYear; year++) { Console.Write(year + " "); for (int quarter = firstQ; quarter <= lastQ; quarter++) { Console.Write("{0, 9:C}", quarterlyRevenue[year, quarter]); } Console.WriteLine(); } Console.ReadKey(); }
public Matrix(decimal[,] members) { row = members.GetUpperBound(0) + 1; column = members.GetUpperBound(1) + 1; data = new decimal[row, column]; Array.Copy(members, data, row * column); }
/// <summary> /// This method finds the smallest value in the given distance array. If the smallest value appears more than once, /// then the lower index is returned. /// </summary> /// <param name="distances">The pre-calculated distance matrix.</param> /// <returns>The first smallest distance index.</returns> public static int[] FirstSmallestDistanceIndex(decimal[,] distances) { if (ParameterValidation.IsDecimalArrayNullOrEmpty(distances)) { return(null); } var indexSmallest = new[] { 0, 0 }; //var indexSmallestA = 0; //var indexSmallestB = 0; for (int indexA = distances.GetLowerBound(0); indexA <= distances.GetUpperBound(0); indexA++) { for (int indexB = distances.GetLowerBound(1); indexB <= distances.GetUpperBound(1); indexB++) { if ((indexA != indexB) && ((indexSmallest[0] == indexSmallest[1]) || (Math.Abs(distances[indexA, indexB]) < Math.Abs(distances[indexSmallest[0], indexSmallest[1]])))) { indexSmallest[0] = indexA; indexSmallest[1] = indexB; } } } return(indexSmallest); // new int[] { indexSmallestA, indexSmallestB }; }
static void Main(string[] args) { //创建二维数组[2004...2009][1...4] int[] lowerBounds = { 2005, 1 }; int[] lengths = { 5, 4 }; decimal[,] quarterlyRevenue = (decimal[, ])Array.CreateInstance(typeof(decimal), lengths, lowerBounds); Console.WriteLine("{0,4} {1,9} {2,9} {3,9} {4,9}", "Year", "Q1", "Q2", "Q3", "Q4"); int firstYear = quarterlyRevenue.GetLowerBound(0); int lastYear = quarterlyRevenue.GetUpperBound(0); int firstQuarter = quarterlyRevenue.GetLowerBound(1); int lastQuarter = quarterlyRevenue.GetUpperBound(1); for (int year = firstYear; year <= lastYear; year++) { Console.Write(year + " "); for (int quarter = firstQuarter; quarter <= lastQuarter; quarter++) { Console.Write("{0,9:C}", quarterlyRevenue[year, quarter]); } Console.WriteLine(); } Console.ReadLine(); }
public NMatrix(decimal[,] data) { Data = data; Rows = Data.GetUpperBound(0) - Data.GetLowerBound(0) + 1; Columns = Data.GetUpperBound(1) - Data.GetLowerBound(1) + 1; }
public static decimal[,] Copy(this decimal[,] arr) { int uBound0 = arr.GetUpperBound(0); int uBound1 = arr.GetUpperBound(1); decimal[,] newArr = new decimal[uBound0, uBound1]; arr.CopyTo(newArr, 0); return(newArr); }
/// <summary> /// This method finds the next smallest value in the array, after the value found in the index specified. If the same /// value exists multiple times, the lower index is returned. /// </summary> /// <param name="distances">The pre-calculated distance matrix.</param> /// <param name="indexLast"></param> /// <returns>The next smallest distance index from the last indexes.</returns> public static int[] NextSmallestDistanceIndex(decimal[,] distances, int[] indexLast) { if (ParameterValidation.IsDecimalArrayNullOrEmpty(distances)) { throw new ArgumentOutOfRangeException(nameof(distances)); } if (ParameterValidation.IsIntArrayNullOrEmpty(indexLast)) { throw new ArgumentOutOfRangeException(nameof(indexLast)); } var indexBest = new[] { -1, -1 }; //var indexBestA = -1; //var indexBestB = -1; if ((indexLast[0] == -1) || (indexLast[1] == -1)) { return(FirstSmallestDistanceIndex(distances)); } for (int indexA = distances.GetLowerBound(0); indexA <= distances.GetUpperBound(0); indexA++) { for (int indexB = distances.GetLowerBound(1); indexB <= distances.GetUpperBound(1); indexB++) { if //// Not the same object twice / no distance measurement ... zero. ((indexA != indexB) && //// Not the same indexes the other way around as last time (at least one value changed). (indexLast[0] != indexB || indexLast[1] != indexA) && //// Not the same indexes the same way around as last time (at least one value changed). (indexLast[0] != indexA || indexLast[1] != indexB) && //// Distance can be the same as the last one - if the index is higher. ((Math.Abs(distances[indexA, indexB]) == Math.Abs(distances[indexLast[0], indexLast[1]]) && (indexA > indexLast[0] || (indexA >= indexLast[0] && indexB > indexLast[1]))) || //// Distance can be more, but only if it is less than the best found so far. (Math.Abs(distances[indexA, indexB]) > Math.Abs(distances[indexLast[0], indexLast[1]]) && (indexBest[0] == -1 || indexBest[1] == -1 || Math.Abs(distances[indexA, indexB]) < Math.Abs(distances[indexBest[0], indexBest[1]]))))) { indexBest[0] = indexA; indexBest[1] = indexB; if (distances[indexA, indexB] == distances[indexLast[0], indexLast[1]]) { // If the distance is the same, then it is impossible to find a lower one, so break out, also, if no break, indexes may be skipped. break; } } } } return(indexBest); //new int[] { indexBestA, indexBestB }; }
///<summary> ///</summary> ///<param name="decimalMatrix"></param> ///<returns></returns> public static double[,] ToDoubleMatrix(decimal[,] decimalMatrix) { var result = new double[decimalMatrix.GetLength(0), decimalMatrix.GetLength(1)]; for (int i = 0; i <= decimalMatrix.GetUpperBound(0); ++i) { for (int j = 0; j <= decimalMatrix.GetUpperBound(1); ++j) { result[i, j] = Convert.ToDouble(decimalMatrix[i, j]); } } return(result); }
public static void FillRandom(this decimal[,] arr, int min, int max) { int uBound0 = arr.GetUpperBound(0); int uBound1 = arr.GetUpperBound(1); for (int i = 0; i < uBound0; i++) { for (int j = 0; j < uBound1; j++) { arr[i, j] = NumberGenerator.Current.NextDecimal(min, max); } } }
protected static double[,] DecimalsToDoubles(decimal[,] array) { int rows = array.GetUpperBound(0) - array.GetLowerBound(0) + 1; int cols = array.GetUpperBound(1) - array.GetLowerBound(1) + 1; double[,] result = new double[rows, cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i, j] = (double)array[i, j]; } } return(result); }
/// <summary> /// Convert a 2D decimal array to a 1D array. /// </summary> /// <param name="array"></param> /// <returns></returns> public static double[] DecimalMatrixToDoubleArray(decimal[,] array) { var rb = array.GetUpperBound(0) + 1; var cb = array.GetUpperBound(1) + 1; var ub = (rb > cb) ? rb : cb; var retval = new double[ub]; for (var row = 0; row < ub; row++) { retval[row] = (rb > cb) ? Convert.ToDouble(array[row, 0], CultureInfo.CurrentCulture) : Convert.ToDouble(array[0, row], CultureInfo.CurrentCulture); } return(retval); }
private decimal GetAverage() { decimal average = 0; for (byte i = 0; i < r; i++) { average += recArray[i, recArray.GetUpperBound(1)]; } average /= r; return(average); }
public void TestMultiDimensionalArray() { NpgsqlCommand command = new NpgsqlCommand("select :i", TheConnection); command.Parameters.Add(":i", (new decimal[, ] { { 0, 1, 2 }, { 3, 4, 5 } })); using (NpgsqlDataReader dr = command.ExecuteReader()) { dr.Read(); Assert.AreEqual(2, (dr[0] as Array).Rank); decimal[,] da = dr[0] as decimal[, ]; Assert.AreEqual(da.GetUpperBound(0), 1); Assert.AreEqual(da.GetUpperBound(1), 2); decimal cmp = 0m; foreach (decimal el in da) { Assert.AreEqual(el, cmp++); } } }
static void DisplayMatrixToConsole(decimal[,] array) { try { // Loop over array and display it. for (int i = 0; i <= array.GetUpperBound(0); i++) { for (int x = 0; x <= array.GetUpperBound(1); x++) { Console.Write(array[i, x]); Console.Write(" "); } Console.WriteLine(); } } catch (IndexOutOfRangeException ex) { LogError(ex.ToString()); } catch (Exception ex) { LogError(ex.ToString()); } }
private void btnTwoDimensionalArray_Click(object sender, EventArgs e) { //declaring wihtout initialization //decimal[,] price = new decimal [5,4]; //price[0, 0] = 450m; //price[0, 1] = 450m; //price[0, 2] = 540m; //... ... ... shows you how to declare with initilization //decimal stores more digits, more precise data //used for money calculations //m used to assign decimal numbers decimal[,] prices = { { 450m, 450m, 450m, 450m }, { 425m, 425m, 425m, 425m }, { 400m, 400m, 400m, 400m }, { 375m, 375m, 375m, 375m }, { 375m, 375m, 375m, 375m }, { 350m, 350m, 350m, 350m } }; rtbDisplay.Text = "traversgin a two-dimensional array, row by row: \n"; for (int x = 0; x <= prices.GetUpperBound(0); x++) { for (int y = 0; y <= prices.GetUpperBound(1); y++) { //rtbDisplay.AppendText(prices[x, y].ToString() + ","); lblDisplay.Text = (prices[x, y].ToString()); } //rtbDisplay.AppendText(Environment.NewLine); lblDisplay.Text.ToString(); } rtbDisplay.AppendText("\ntraversing a two-dimensional array, column by column:\n"); for (int y = 0; y <= prices.GetUpperBound(1); y++) { for (int x = 0; x <= prices.GetUpperBound(0); x++) { rtbDisplay.AppendText(prices[x, y].ToString() + ","); } rtbDisplay.AppendText(Environment.NewLine); } rtbDisplay.AppendText("\nThe sum of all items is: "); decimal sum = 0; for (int y = 0; y <= prices.GetUpperBound(1); y++) { for (int x = 0; x <= prices.GetUpperBound(0); x++) { sum += prices[x, y]; //sum = sum + prices[x, y]; } } rtbDisplay.AppendText(sum.ToString()); }
private string GetBestResult() { int indexOfMinimumProbability = 0; decimal minimumProbability = 1.01m; for (int i = 1; i <= probabilitiesOfFlightAndIndexesPair.GetUpperBound(0); ++i) { if (minimumProbability > probabilitiesOfFlightAndIndexesPair[i, 0]) { indexOfMinimumProbability = i; minimumProbability = probabilitiesOfFlightAndIndexesPair[i, 0]; } } return(probabilitiesOfFlightAndIndexesPair[indexOfMinimumProbability, 0] == probabilitiesOfFlightAndIndexesPair[indexOfMinimumProbability, 1] ? GenerateAnswear(probabilitiesOfFlightAndIndexesPair[indexOfMinimumProbability, 0], 1) : GenerateAnswear(probabilitiesOfFlightAndIndexesPair[indexOfMinimumProbability, 0], 2, (int)probabilitiesOfFlightAndIndexesPair[indexOfMinimumProbability, 1], (int)probabilitiesOfFlightAndIndexesPair[indexOfMinimumProbability, 2] )); }
public static void MainTestArray() { var names = new[] { 1, 2, 0, 5, 4 }; var namesdes = new int[names.Length]; System.Buffer.BlockCopy(names, 0, namesdes, 1, names.Length); var des = new string[namesdes.Length]; foreach (var val in namesdes) { //Console.WriteLine(val); } var arr = new object[10]; var arrs = new object[10, 8]; //Console.WriteLine(namesdes.Aggregate((a, b) => a.ToString() + "," + b.ToString())); //Console.WriteLine(FileAttributes.Archive | FileAttributes.Hidden | FileAttributes.Normal); //Console.WriteLine(FileAttributes.Archive & FileAttributes.Archive); decimal[,] darr = (decimal[, ])Array.CreateInstance(typeof(decimal), new int[] { 5, 4 }, new[] { 2005, 1 }); Console.WriteLine("{0,4},{1,9},{2,9},{3,9},{4,9}", "Year", "Q1", "Q2", "Q3", "Q4"); int firstYear = darr.GetLowerBound(0); int lastYear = darr.GetUpperBound(0); int firstQuarter = darr.GetLowerBound(1); int lastQuarter = darr.GetUpperBound(1); for (int i = firstYear; i <= lastYear; i++) { Console.Write(i + " "); for (int j = firstQuarter; j <= lastQuarter; j++) { Console.Write("{0,1:C} ", darr[i, j]); } Console.WriteLine(); } int[,] a1 = new int[10000, 10000]; int[][] a2 = new int[10000][]; for (int i = 0; i < 10000; i++) { a2[i] = new int[10000]; } Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < 10; i++) { Safe2DimArrayAccess(a1); } Console.WriteLine("{0}:Safe2DimArrayAccess", sw.Elapsed); sw = Stopwatch.StartNew(); for (int i = 0; i < 10; i++) { SafeJaggedArrayAccess(a2); } Console.WriteLine("{0}:SafeJaggedArrayAccess", sw.Elapsed); sw = Stopwatch.StartNew(); for (int i = 0; i < 10; i++) { UnSafe2DimArrayAccess(a1); } Console.WriteLine("{0}:UnSafe2DimArrayAccess", sw.Elapsed); StackallocDemo(); InlineArrayDemo(); Console.Read(); }