diff --git a/Lab4/Program.cs b/Lab4/Program.cs index 59898f2..8ceb9f4 100644 --- a/Lab4/Program.cs +++ b/Lab4/Program.cs @@ -4,6 +4,8 @@ public class Program { public static void Main() { + } } } + diff --git a/Lab4/Purple.cs b/Lab4/Purple.cs index cb4bd6a..0909a8e 100644 --- a/Lab4/Purple.cs +++ b/Lab4/Purple.cs @@ -117,4 +117,4 @@ public void Task10(int[] array, int N) return (bright, normal, dim); } } -} \ No newline at end of file + } diff --git a/Lab4/White.cs b/Lab4/White.cs index d39815d..c45da83 100644 --- a/Lab4/White.cs +++ b/Lab4/White.cs @@ -1,4 +1,6 @@ -namespace Lab4 +using System; + +namespace Lab4 { public class White { @@ -7,7 +9,16 @@ public double Task1(int[] vector) double length = 0; // code here + if (vector == null || vector.Length == 0) + return 0; + + double sumSquares = 0; + for (int i = 0; i < vector.Length; i++) + { + sumSquares += vector[i] * vector[i]; + } + length = Math.Sqrt(sumSquares); // end return length; @@ -17,7 +28,19 @@ public int Task2(int[] array, int P, int Q) int count = 0; // code here + if (array == null || array.Length == 0) + return 0; + + + int minValue = Math.Min(P, Q); + int maxValue = Math.Max(P, Q); + + for (int i = 0; i < array.Length; i++) + { + if (array[i] > minValue && array[i] < maxValue) + count++; + } // end return count; @@ -26,7 +49,51 @@ public void Task3(int[] array) { // code here - + if (array == null || array.Length == 0) + return; + + + int maxIndex = 0; + for (int i = 1; i < array.Length; i++) + { + if (array[i] > array[maxIndex]) + { + maxIndex = i; + } + } + + + if (maxIndex == array.Length - 1) + return; + + + int minAfter = maxIndex + 1; + for (int i = maxIndex + 2; i < array.Length; i++) + { + if (array[i] < array[minAfter]) + { + minAfter = i; + } + } + + + bool allEqual = true; + for (int i = maxIndex + 1; i < array.Length; i++) + { + if (array[i] != array[minAfter]) + { + allEqual = false; + break; + } + } + + + if (!allEqual) + { + int temp = array[maxIndex]; + array[maxIndex] = array[minAfter]; + array[minAfter] = temp; + } // end } @@ -34,7 +101,21 @@ public void Task4(int[] array) { // code here + if (array == null || array.Length == 0) + return; + + int maxIndex = 0; + for (int i = 2; i < array.Length; i += 2) + { + if (array[i] > array[maxIndex]) + { + maxIndex = i; + } + } + + + array[maxIndex] = maxIndex; // end } @@ -43,7 +124,21 @@ public int Task5(int[] array, int P) int index = 0; // code here + index = -1; + + if (array == null) + return index; + + + for (int i = 0; i < array.Length; i++) + { + if (array[i] == P) + { + index = i; + break; + } + } // end return index; @@ -52,7 +147,26 @@ public void Task6(int[] array) { // code here - + if (array == null || array.Length == 0) + return; + + + int maxIndex = 0; + for (int i = 1; i < array.Length; i++) + { + if (array[i] > array[maxIndex]) + { + maxIndex = i; + } + } + + + for (int i = 0; i < maxIndex - 1; i += 2) + { + int temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + } // end } @@ -61,7 +175,30 @@ public int[] Task7(int[] array) int[] answer = null; // code here + if (array == null) + return answer; + + int count = 0; + for (int i = 0; i < array.Length; i++) + { + if (array[i] >= 0) + { + count++; + } + } + + + answer = new int[count]; + int index = 0; + for (int i = 0; i < array.Length; i++) + { + if (array[i] >= 0) + { + answer[index] = array[i]; + index++; + } + } // end return answer; @@ -70,7 +207,22 @@ public void Task8(int[] array) { // code here - + if (array == null || array.Length == 0) + return; + + + for (int i = 0; i < array.Length - 1; i++) + { + for (int j = 0; j < array.Length - 1 - i; j++) + { + if (array[j] < array[j + 1]) + { + int temp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temp; + } + } + } // end } @@ -78,7 +230,16 @@ public void Task9(int[] array) { // code here + if (array == null || array.Length == 0) + return; + + for (int i = 0; i < array.Length / 2; i++) + { + int temp = array[i]; + array[i] = array[array.Length - 1 - i]; + array[array.Length - 1 - i] = temp; + } // end } @@ -87,7 +248,62 @@ public int[] Task10(int[] A, int[] B) int[] C = null; // code here + if (A == null && B == null) + { + C = new int[0]; + return C; + } + + + if (A == null || A.Length == 0) + { + C = (int[])B.Clone(); + return C; + } + + + if (B == null || B.Length == 0) + { + C = (int[])A.Clone(); + return C; + } + + C = new int[A.Length + B.Length]; + + + int indexA = 0; + int indexB = 0; + int indexC = 0; + + + while (indexA < A.Length && indexB < B.Length) + { + C[indexC] = A[indexA]; + indexC++; + indexA++; + + + C[indexC] = B[indexB]; + indexC++; + indexB++; + } + + + while (indexA < A.Length) + { + C[indexC] = A[indexA]; + indexC++; + indexA++; + } + + + while (indexB < B.Length) + { + C[indexC] = B[indexB]; + indexC++; + indexB++; + } // end return C; @@ -97,7 +313,30 @@ public double[] Task11(double a, double b, int n) double[] array = null; // code here + if (n <= 0) return null; + + + if (Math.Abs(a - b) < double.Epsilon) + { + if (n == 1) + array = new double[] { a }; + else + return null; + } + else + { + + if (n < 2) return null; + + + array = new double[n]; + double step = (b - a) / (n - 1); + + + for (int i = 0; i < n; i++) + array[i] = a + step * i; + } // end return array; @@ -108,7 +347,31 @@ public double[] Task12(double[] raw) double[] restored = null; // code here + if (raw == null || raw.Length < 3) + return null; + + + restored = (double[])raw.Clone(); + + + for (int i = 0; i < restored.Length; i++) + { + if (restored[i] == -1) + { + int prevIndex = i == 0 ? restored.Length - 1 : i - 1; + int nextIndex = i == restored.Length - 1 ? 0 : i + 1; + + + double prevValue = restored[prevIndex]; + double nextValue = restored[nextIndex]; + + if (prevValue != -1 && nextValue != -1) + { + restored[i] = (prevValue + nextValue) / 2.0; + } + } + } // end return restored; diff --git a/Lab4test/PurpleTest.cs b/Lab4test/PurpleTest.cs index 830f4f4..091fd23 100644 --- a/Lab4test/PurpleTest.cs +++ b/Lab4test/PurpleTest.cs @@ -382,20 +382,20 @@ public void Test07() new int[] { 5 } }; var answer = new double[][] { - (new double[] { -0.25, -0.125, 0, 0.125, 0.25, 0.375, 0.375, 1, 0.625, 0.375, 0.125, 0.125, 0.125, 0.25, -0.875, 1, 1, -0.125, 0, 0, 0.625, -0.625, -1, 0.25, 0.375 }), - (new double[] { -0.5, -0.75, -1, -0.75, -0.5, -0.25, -0.25, 1, 0.25, -0.25, -0.75, -0.75, -0.75, -0.5, 0.75, 1, 1, -0.75, -1, -1, 0.25, 0.25, 1, -0.5, -0.25 }), - (new double[] { -0.7142857142857143, -1, -1, -1, -0.7142857142857143, -0.4285714285714286, -0.4285714285714286, 1, 0.1428571428571428, -0.4285714285714286, -1, -1, -1, -0.7142857142857143, 0.7142857142857142, 1, 1, -1, -0.1428571428571429, -0.1428571428571429, 0.1428571428571428, 0.1428571428571428, 1, -0.7142857142857143, -0.4285714285714286 }), - (new double[] { 0.7142857142857142, 1, 1, 1, 0.7142857142857142, 0.4285714285714286, 0.4285714285714286, -1, -0.1428571428571429, 0.4285714285714286, 1, 1, 1, 0.7142857142857142, -0.7142857142857143, -1, -1, 1, 0.1428571428571428, 0.1428571428571428, -0.1428571428571429, -0.1428571428571429, -1, 0.7142857142857142, 0.4285714285714286 }), - (new double[] { 0.5, 0.75, 1, 0.75, 0.5, 0.25, 0.25, -1, -0.25, 0.25, 0.75, 0.75, 0.75, 0.5, -0.75, -1, -1, 0.75, 1, 1, -0.25, -0.25, -1, 0.5, 0.25 }), + new double[] { -0.25, -0.125, 0, 0.125, 0.25, 0.375, 0.375, 1, 0.625, 0.375, 0.125, 0.125, 0.125, 0.25, -0.875, 1, 1, -0.125, 0, 0, 0.625, -0.625, -1, 0.25, 0.375 }, + new double[] { -0.5, -0.75, -1, -0.75, -0.5, -0.25, -0.25, 1, 0.25, -0.25, -0.75, -0.75, -0.75, -0.5, 0.75, 1, 1, -0.75, -1, -1, 0.25, 0.25, 1, -0.5, -0.25 }, + new double[] { -0.7142857142857143, -1, -1, -1, -0.7142857142857143, -0.4285714285714286, -0.4285714285714286, 1, 0.1428571428571428, -0.4285714285714286, -1, -1, -1, -0.7142857142857143, 0.7142857142857142, 1, 1, -1, -0.1428571428571429, -0.1428571428571429, 0.1428571428571428, 0.1428571428571428, 1, -0.7142857142857143, -0.4285714285714286 }, + new double[] { 0.7142857142857142, 1, 1, 1, 0.7142857142857142, 0.4285714285714286, 0.4285714285714286, -1, -0.1428571428571429, 0.4285714285714286, 1, 1, 1, 0.7142857142857142, -0.7142857142857143, -1, -1, 1, 0.1428571428571428, 0.1428571428571428, -0.1428571428571429, -0.1428571428571429, -1, 0.7142857142857142, 0.4285714285714286 }, + new double[] { 0.5, 0.75, 1, 0.75, 0.5, 0.25, 0.25, -1, -0.25, 0.25, 0.75, 0.75, 0.75, 0.5, -0.75, -1, -1, 0.75, 1, 1, -0.25, -0.25, -1, 0.5, 0.25 }, null, null, - (new double[] { -1, 1, -1, 1 }), - (new double[] { 1, -1, 1, -1 }), - (new double[] { 1, -0.6666666666666667, -1, 0.6666666666666667 }), - (new double[] { -0.487603305785124, 1, -0.371900826446281, -0.4049586776859504, -0.4380165289256198, -0.4380165289256198, -0.20661157024793386, -0.5867768595041323, -0.42148760330578516, -1 }), - (new double[] { -1, 0, 1, -1, 0, 1, -1, -1, -1, 0, 1, 1, 1 }), - (new double[] { 0.33333333333333326, -0.6666666666666667, -1, -0.33333333333333337, 0.6666666666666667, 1, 0.33333333333333326, 0.33333333333333326, -0.33333333333333337, -0.6666666666666667, -1, 1, 1 }), - (new double[] { 0, 0.5333333333333334, 1, 1, 1, -1, -1, -0.5333333333333333, 0.1333333333333333, -0.19999999999999996 }), + new double[] { -1, 1, -1, 1 }, + new double[] { 1, -1, 1, -1 }, + new double[] { 1, -0.6666666666666667, -1, 0.6666666666666667 }, + new double[] { -0.487603305785124, 1, -0.371900826446281, -0.4049586776859504, -0.4380165289256198, -0.4380165289256198, -0.20661157024793386, -0.5867768595041323, -0.42148760330578516, -1 }, + new double[] { -1, 0, 1, -1, 0, 1, -1, -1, -1, 0, 1, 1, 1 }, + new double[] { 0.33333333333333326, -0.6666666666666667, -1, -0.33333333333333337, 0.6666666666666667, 1, 0.33333333333333326, 0.33333333333333326, -0.33333333333333337, -0.6666666666666667, -1, 1, 1 }, + new double[] { 0, 0.5333333333333334, 1, 1, 1, -1, -1, -0.5333333333333333, 0.1333333333333333, -0.19999999999999996 }, null }; var test = new double[answer.Length][]; @@ -415,7 +415,7 @@ public void Test07() Assert.AreEqual(answer[i].Length, test[i].Length); for (int j = 0; j < answer[i].Length; j++) { - Assert.AreEqual(answer[i][j], test[i][j], $"Test {i} failed (index {j})"); + Assert.AreEqual(answer[i][j], test[i][j], E, $"Test {i} failed (index {j})"); } } } @@ -698,4 +698,5 @@ public void Test12() } } } -} \ No newline at end of file + +}