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..40e2e20 100644 --- a/Lab4/White.cs +++ b/Lab4/White.cs @@ -1,4 +1,4 @@ -namespace Lab4 +namespace Lab4 { public class White { @@ -7,7 +7,12 @@ public double Task1(int[] vector) double length = 0; // code here - + for (int i = 0; i < vector.Length; i++) + { + length += Math.Pow(vector[i], 2); + } + length = Math.Sqrt(length); + Console.WriteLine(length); // end return length; @@ -17,7 +22,9 @@ public int Task2(int[] array, int P, int Q) int count = 0; // code here - + foreach (int x in array) + if (x > P && x < Q) count++; + return count; // end return count; @@ -26,7 +33,26 @@ public void Task3(int[] array) { // code here - + if (array == null || array.Length == 0) + return; + int maxIndex = 0; + for (int i = 0; i < array.Length; i++) + { + if (array[i] > array[maxIndex]) + { + maxIndex = i; + } + } + if (maxIndex == array.Length - 1) { return; } + + int min = maxIndex + 1; + for (int i = maxIndex + 2; i < array.Length; i++) + { + if (array[i] < array[min]) { min = i; } + } + + (array[maxIndex], array[min]) = (array[min], array[maxIndex]); + Console.WriteLine(string.Join(",", array)); // end } @@ -34,7 +60,16 @@ public void Task4(int[] array) { // code here - + if (array != null && array.Length > 0) + { + 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 +78,18 @@ public int Task5(int[] array, int P) int index = 0; // code here - + index = -1; + if (array != null) + { + for (int i = 0; i < array.Length; i++) + { + if (array[i] == P) + { + index = i; + break; + } + } + } // end return index; @@ -52,7 +98,24 @@ public void Task6(int[] array) { // code here - + int imax = int.MinValue; + int IxMax = -1; + for (int i = 0; i < array.Length; i++) + { + if (imax < array[i]) + { + imax = array[i]; + IxMax = i; + } + } + if (IxMax == 0) + { + return; + } + for (int i = 0; i + 1 < IxMax; i += 2) + { + (array[i], array[i + 1]) = (array[i + 1], array[i]); + } // end } @@ -61,7 +124,29 @@ public int[] Task7(int[] array) int[] answer = null; // code here - + if (array != null) + { + 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]; + } + } + } // end return answer; @@ -70,7 +155,22 @@ public void Task8(int[] array) { // code here - + if (array == null || array.Length < 2) + { + return; + } + for (int i = 1; i < array.Length; i++) + { + int p = array[i]; + int j = i - 1; + while (j >= 0 && array[j] < p) + { + array[j + 1] = array[j]; + j--; + } + + array[j + 1] = p; + } // end } @@ -78,16 +178,36 @@ public void Task9(int[] array) { // code here - + { + int tepm, n = array.Length; + for (int i = 0; i < n / 2; i++) + { + tepm = array[i]; + array[i] = array[n - i - 1]; + array[n - i - 1] = tepm; + + } + } // end } public int[] Task10(int[] A, int[] B) { int[] C = null; - - // code here - +// code here +if (A.Length == 0) + C = new int[A.Length]; +if (B.Length == 0) + C = new int[B.Length]; +C = new int[A.Length + B.Length]; +int ia = 0, ib = 0, ic = 0; +while (ia < A.Length || ib < B.Length) +{ + if (ia < A.Length) + C[ic++] = A[ia++]; + if (ib < B.Length) + C[ic++] = B[ib++]; +} // end return C; @@ -97,7 +217,40 @@ public double[] Task11(double a, double b, int n) double[] array = null; // code here +if (n <= 0) { return null; } + +if (n == 1) +{ + if (a == b) + { + return new double[] { a }; + } + else + { + return null; + } +} + +if (a == b) { return null; } + +array = new double[n]; +if (a < b) +{ + double step = (b - a) / (n - 1); + for (int i = 0; i < n; i++) + { + array[i] = a + i * step; + } +} +else +{ + double step = (a - b) / (n - 1); + for (int i = 0; i < n; i++) + { + array[i] = a - i * step; + } +} // end return array; @@ -108,10 +261,29 @@ public double[] Task12(double[] raw) double[] restored = null; // code here - +if (raw.Length < 3) +{ + restored = null; +} +else +{ + restored = new double[raw.Length]; + for (int i = 0; i < raw.Length; i++) + { + if (raw[i] == -1 && raw[(i - 1 + raw.Length) % raw.Length] != -1 && raw[(i + 1) % raw.Length] != -1) + { + restored[i] = (raw[(i + 1) % raw.Length] + raw[(i - 1 + raw.Length) % raw.Length]) / 2; + } + else + { + restored[i] = raw[i]; + } + } +} // end return restored; } } -} \ No newline at end of file + +}