static void Main(string[] args) { int numberOfRows = ReadInt($"number of rows is: "); int numberOfColumns = ReadInt($"number of columns is: "); int[,] matrix = CreateRandomMatrix(numberOfRows, numberOfColumns); int ariphmeticMean = AriphmeticMean(matrix, numberOfColumns, numberOfRows); Console.WriteLine($"ariph mean is:{ariphmeticMean}"); ElementAndItsCoordinatesAndDifference[] elementAndItsCoordinatesAndDifferences = new ElementAndItsCoordinatesAndDifference[0]; for (int i = 0; i < numberOfRows; i++) { for (int j = 0; j < numberOfColumns; j++) { ElementAndItsCoordinates elementAndItsCoordinates = new ElementAndItsCoordinates(matrix[j, i], i, j); int difference = Math.Abs(matrix[j, i] - ariphmeticMean); ElementAndItsCoordinatesAndDifference elementAndItsCoordinatesAndDifference = new ElementAndItsCoordinatesAndDifference(elementAndItsCoordinates, difference); elementAndItsCoordinatesAndDifferences = RewriteElementAndItsCoordinate(elementAndItsCoordinatesAndDifferences, elementAndItsCoordinatesAndDifference); } } elementAndItsCoordinatesAndDifferences = SortArray(elementAndItsCoordinatesAndDifferences); elementAndItsCoordinatesAndDifferences = FindMinimalDifferences(elementAndItsCoordinatesAndDifferences); for (int i = 0; i < elementAndItsCoordinatesAndDifferences.Length; i++) { Console.WriteLine($"coordinate of element is: i = {elementAndItsCoordinatesAndDifferences[i].ElementAndItsCoordinates.CoordinateI} " + $"and j = {elementAndItsCoordinatesAndDifferences[i].ElementAndItsCoordinates.CoordinateJ}; " + $"value {elementAndItsCoordinatesAndDifferences[i].ElementAndItsCoordinates.Element}"); } Console.ReadKey(); }
static ElementAndItsCoordinatesAndDifference[] RewriteElementAndItsCoordinate(ElementAndItsCoordinatesAndDifference[] array, ElementAndItsCoordinatesAndDifference elementAndItsCoordinates) { ElementAndItsCoordinatesAndDifference[] resultOfElementAndItsCoordinates = new ElementAndItsCoordinatesAndDifference[array.Length + 1]; for (int i = 0; i < array.Length; i++) { resultOfElementAndItsCoordinates[i] = array[i]; } resultOfElementAndItsCoordinates[resultOfElementAndItsCoordinates.Length - 1] = elementAndItsCoordinates; return resultOfElementAndItsCoordinates; }
static ElementAndItsCoordinatesAndDifference[] SortArray(ElementAndItsCoordinatesAndDifference[] array) { for (int i = 0; i < array.Length; i++) { for (int j = 0; j < array.Length - 1; j++) { ElementAndItsCoordinatesAndDifference firstObject = array[j]; int firstDif = firstObject.Difference; ElementAndItsCoordinatesAndDifference secObject = array[j + 1]; int secDif = secObject.Difference; if (secDif < firstDif) { array[j + 1] = firstObject; array[j] = secObject; } } } return array; }
static ElementAndItsCoordinatesAndDifference[] FindMinimalDifferences(ElementAndItsCoordinatesAndDifference[] array) { ElementAndItsCoordinatesAndDifference[] newArrayOfElementsAndDifference = new ElementAndItsCoordinatesAndDifference[0]; ElementAndItsCoordinatesAndDifference firstElementOfArray = array[0]; int firstDif = firstElementOfArray.Difference; newArrayOfElementsAndDifference = RewriteElementAndItsCoordinate(newArrayOfElementsAndDifference,firstElementOfArray); for (int i = 1; i < array.Length; i++) { ElementAndItsCoordinatesAndDifference currentElement = array[i]; int currentDif = currentElement.Difference; if (currentDif == firstDif) { newArrayOfElementsAndDifference = RewriteElementAndItsCoordinate(newArrayOfElementsAndDifference, currentElement); } else { break; } } return newArrayOfElementsAndDifference; }