static NumberOfCoinsidingNumbersAndItsLine CompareTheLines(int[] firstLine, int[] currentLine, int index) { int numberOfCounsidingNumbers = 0; for (int i = 0; i < firstLine.Length; i++) { if (firstLine[i] == currentLine[i]) { numberOfCounsidingNumbers++; } } NumberOfCoinsidingNumbersAndItsLine numberOfCoinsidingNumbersAndItsLine = new NumberOfCoinsidingNumbersAndItsLine(numberOfCounsidingNumbers, index); return(numberOfCoinsidingNumbersAndItsLine); }
static NumberOfCoinsidingNumbersAndItsLine[] RewriteNumberOfCoinsidingNumbersAndItsLine(NumberOfCoinsidingNumbersAndItsLine[] array, NumberOfCoinsidingNumbersAndItsLine newElement) { int arrayLength = 0; if (array != null) { arrayLength = array.Length; } NumberOfCoinsidingNumbersAndItsLine[] result = new NumberOfCoinsidingNumbersAndItsLine[arrayLength + 1]; for (int j = 0; j < arrayLength; j++) { result[j] = array[j]; } result[result.Length - 1] = newElement; return(result); }
static NumberOfCoinsidingNumbersAndItsLine[] SortNumberOfCoinsidingNumbersAndItsLines(NumberOfCoinsidingNumbersAndItsLine[] array) { for (int i = 0; i < array.Length; i++) { for (int j = 0; j < array.Length - 1; j++) { NumberOfCoinsidingNumbersAndItsLine firstNumb = array[j]; int firstNumber = firstNumb.NumberOfCoinsidingNumbers; NumberOfCoinsidingNumbersAndItsLine secNumb = array[j + 1]; int secondNumber = secNumb.NumberOfCoinsidingNumbers; if (secondNumber < firstNumber) { array[j + 1] = firstNumb; array[j] = secNumb; } } } return(array); }
static NumberOfCoinsidingNumbersAndItsLine[] FindMinimal(NumberOfCoinsidingNumbersAndItsLine[] arrayOfCoinsidingNumbersAndItsLines) { NumberOfCoinsidingNumbersAndItsLine[] newArrayOfNumberOfCoinsidingNumbersAndItsLines = new NumberOfCoinsidingNumbersAndItsLine[0]; NumberOfCoinsidingNumbersAndItsLine firstElement = newArrayOfNumberOfCoinsidingNumbersAndItsLines[0]; int firstElementOfArray = newArrayOfNumberOfCoinsidingNumbersAndItsLines[0].NumberOfCoinsidingNumbers; newArrayOfNumberOfCoinsidingNumbersAndItsLines = RewriteNumberOfCoinsidingNumbersAndItsLine(newArrayOfNumberOfCoinsidingNumbersAndItsLines, firstElement); for (int i = 1; i < arrayOfCoinsidingNumbersAndItsLines.Length; i++) { NumberOfCoinsidingNumbersAndItsLine currentElement = arrayOfCoinsidingNumbersAndItsLines[i]; int currentElementOfArray = currentElement.NumberOfCoinsidingNumbers; if (currentElementOfArray == firstElementOfArray) { newArrayOfNumberOfCoinsidingNumbersAndItsLines = RewriteNumberOfCoinsidingNumbersAndItsLine(newArrayOfNumberOfCoinsidingNumbersAndItsLines, currentElement); } else { break; } } return(newArrayOfNumberOfCoinsidingNumbersAndItsLines); }
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[] arrayOfNumbersOfFirstLine = new int[numberOfColumns]; NumberOfCoinsidingNumbersAndItsLine[] numberOfCoinsidingNumbersAndItsLine = new NumberOfCoinsidingNumbersAndItsLine[numberOfRows - 1]; int indexOfTheLine = 0; arrayOfNumbersOfFirstLine = GetCurrentSortedArray(indexOfTheLine, matrix, numberOfColumns); int[] arrayOfCurrentNumbersOfCurrentLine = new int[numberOfColumns]; for (indexOfTheLine = 1; indexOfTheLine < numberOfRows; indexOfTheLine++) { arrayOfCurrentNumbersOfCurrentLine = GetCurrentSortedArray(indexOfTheLine, matrix, numberOfColumns); NumberOfCoinsidingNumbersAndItsLine coinsidingNumbersAndItsLine = CompareTheLines(arrayOfNumbersOfFirstLine, arrayOfCurrentNumbersOfCurrentLine, indexOfTheLine); numberOfCoinsidingNumbersAndItsLine[indexOfTheLine - 1] = coinsidingNumbersAndItsLine; } numberOfCoinsidingNumbersAndItsLine = SortNumberOfCoinsidingNumbersAndItsLines(numberOfCoinsidingNumbersAndItsLine); Console.ReadKey(); }