From d840d6b32c377e48234d83b3a3ed11b80f91d599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=88=D1=82=D0=B0=D0=BA=20=D0=98=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D1=8C?= Date: Fri, 24 Oct 2025 22:04:19 +0300 Subject: [PATCH 01/11] Add files via upload --- Program.cs | 144 ++++++++++++++++ Purple.cs | 499 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 643 insertions(+) create mode 100644 Program.cs create mode 100644 Purple.cs diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..b4b8df7 --- /dev/null +++ b/Program.cs @@ -0,0 +1,144 @@ +namespace Lab4 +{ + public class Program + { + public static void Main() + { + Purple purple = new Purple(); + + // Task 3 + + Console.WriteLine("Task 3\n"); + + var sizes = new int [] { 4, 8, 12, 10, 15, 22 }; + for (int i = 0; i < 6; i++) + { + int n = sizes[i]; + var a = new int[n]; + Random k = new Random(); + int sum = 0; + for (int j = 0; j < n; j++) + { + a[j] = k.Next(11); + sum += a[j]; + } + Console.WriteLine(string.Join(" ", a)); + double mid = (double)sum / n; + Console.WriteLine($"mid: {mid}"); + int p = (int)(mid + k.Next(-3, 4)); + Console.WriteLine($"p: {p}"); + var ans_a = purple.Task3(a, p); + Console.WriteLine(string.Join(" ", ans_a)); + Console.WriteLine(); + } + + // Task 4 + + Console.WriteLine("Task 4\n"); + + var a4 = new int[] { 3, -6, -2, 2, 4, 62, -32, 1, 0, 2}; + Console.WriteLine(string.Join(" ", a4)); + purple.Task4(a4); + Console.WriteLine($"{string.Join(" ", a4)}\n"); + + // Task 5 + + Console.WriteLine("Task 5\n"); + + Random gen = new Random(); + + var a5 = new int[] { 334, -61, -243, 23, 41, 62, -32, 10, 54, 24 }; + var b5 = new int[] { 0, 1, 2, 3 }; + Console.WriteLine($"a: {string.Join(" ", b5)}\n"); + Console.WriteLine($"b: {string.Join(" ", a5)}\n"); + + var k_ind = new int[] {0, gen.Next(4, 7), a5.Length - 1, 50}; + + foreach (int k_ind_i in k_ind) + { + Console.WriteLine($"k: {k_ind_i}"); + var ans5 = purple.Task5(a5, b5, k_ind_i); + if (ans5 != null) + { + Console.WriteLine($"{string.Join(" ", ans5)}\n"); + } + } + + // Task 7 + + Console.WriteLine("Task 7\n"); + + var a7 = new int[] { 0, 1, 4, 8, 6, 7, 10 }; + var ans7 = purple.Task7(a7); + Console.WriteLine(string.Join(" ", a7)); + Console.WriteLine($"{string.Join(" ", ans7)}\n"); + + // Task 8 + + Console.WriteLine("Task 8\n"); + + var a8 = new int[] { 3, -4, 2, 1 }; + var b8 = new int[] { 9, -2, 5, -2, 4, 2, 2 }; + Console.WriteLine(string.Join(" ", a8)); + Console.WriteLine(string.Join(" ", b8)); + var ans8 = purple.Task8(a8, b8); + Console.WriteLine(string.Join(" ", a8)); + Console.WriteLine(string.Join(" ", b8)); + Console.WriteLine($"{string.Join(" ", ans8)}\n"); + + // Task 9 + + Console.WriteLine("Task 9\n"); + + var a9 = new int[] { -2, -5, 3, -4, 2, 1, 2, 3, 3, 0, -6 }; + Console.WriteLine(string.Join(" ", a9)); + purple.Task9(a9); + Console.WriteLine($"{string.Join(" ", a9)}\n"); + + // Task 10 + + Console.WriteLine("Task 10\n"); + + var a10 = new int[] { 3, 1, 8, 4, 9, 5, 7, 0, -2, 31, 4 }; + int N10 = 3; + Console.WriteLine(string.Join(" ", a10)); + purple.Task10(a10, N10); + Console.WriteLine($"{string.Join(" ", a10)}\n"); + + N10 = 8; + Console.WriteLine(string.Join(" ", a10)); + purple.Task10(a10, N10); + Console.WriteLine($"{string.Join(" ", a10)}\n"); + + // Task 11 + + Console.WriteLine("Task 11\n"); + + double[] a11 = { 7, 7, 4, 10, 10, 10, 10, 10 }; + double[] b11 = { 7, 7, 0, 19, 19, 19, 19, 19 }; + int[] n11 = { 1, 5, 3, 0, 1, 2, 3, 6 }; + double[] Xext, Yext; + + for (int i = 0; i < n11.Length; i++) + { + Console.WriteLine($"a = {a11[i]} b = {b11[i]} n = {n11[i]}"); + (Xext, Yext) = purple.Task11(a11[i], b11[i], n11[i]); + if (Xext != null) + { + Console.WriteLine(Xext.Length); + Console.WriteLine($"Xext: {string.Join(" ", Xext)}"); + Console.WriteLine($"Yext: {string.Join(" ", Yext)}"); + } + Console.WriteLine(); + } + + // Task 12 + + Console.WriteLine("Task 12\n"); + + double[] raw12 = { 7, 10, 3, 8, 4, -1, -3, 1, -2, 4, 6, -3, 6, 0, -3 }; + Console.WriteLine($"raw: {string.Join(" ", raw12)}"); + purple.Task12(raw12); + } + } +} diff --git a/Purple.cs b/Purple.cs new file mode 100644 index 0000000..1e24694 --- /dev/null +++ b/Purple.cs @@ -0,0 +1,499 @@ +using System.Security.Cryptography; + +namespace Lab4 +{ + public class Purple + { + public void Task1(double[] array) + { + + // code here + + int n = array.Length; + double sum_elem = 0; + double max_val = array[0]; + int max_ind = 0; + for (int i = 0; i < n; i++) + { + if (max_val < array[i]) + { + max_val = array[i]; + max_ind = i; + } + sum_elem += array[i]; + } + double mid = sum_elem / n; + + for (int i = max_ind + 1; i < n; i++) + { + array[i] = mid; + } + + // end + + } + public (int[] even, int[] odd) Task2(int[] array) + { + int[] even = null, odd = null; + + // code here + + int n = array.Length; + odd = new int[n / 2]; + even = new int[n - odd.Length]; + for (int i = 0; i < n; i++) + { + if (i % 2 == 0) + { + even[i / 2] = array[i]; + } + else + { + odd[i / 2] = array[i]; + } + } + + // end + + return (even, odd); + } + public int[] Task3(int[] array, int P) + { + int[] answer = null; + + // code here + + int n = array.Length; + int sum_elem = 0; + foreach (int elem in array) + { + sum_elem += elem; + } + double mid = (double)sum_elem / n; + + int closest_ind = 0; + double closest_dif = Math.Abs(array[0] - mid); + for (int i = 0; i < n; i++) + { + double new_dif = Math.Abs(array[i] - mid); + if (new_dif < closest_dif) + { + closest_dif = new_dif; + closest_ind = i; + } + } + answer = new int[n + 1]; + for (int i = 0; i <= closest_ind; i++) + { + answer[i] = array[i]; + } + answer[closest_ind + 1] = P; + for (int i = closest_ind + 1; i < n; i++) + { + answer[i + 1] = array[i]; + } + + // end + + return answer; + } + public void Task4(int[] array) + { + + // code here + + int n = array.Length; + int neg_size = 0; + for (int i = 0; i < n; i++) + { + if (array[i] < 0) + { + neg_size++; + } + } + int pos_size = n - neg_size; + + var pos = new int[pos_size]; + var neg = new int[neg_size]; + + int pos_ind = 0, neg_ind = 0; + for (int i = 0; i < n; i++) + { + if (array[i] < 0) + { + neg[neg_ind++] = array[i]; + } + else + { + pos[pos_ind++] = array[i]; + } + } + for (int i = 0; i < pos_size; i++) + { + array[i] = pos[i]; + } + for (int i = pos_size; i < n; i++) + { + array[i] = neg[i - pos_size]; + } + + // end + + } + public int[] Task5(int[] A, int[] B, int k) + { + int[] answer = null; + + // code here + + if (k < A.Length) + { + answer = new int[A.Length + B.Length]; + for (int i = 0; i < k; i++) + { + answer[i] = A[i]; + } + for (int i = 0; i < B.Length; i++) + { + answer[i + k] = B[i]; + } + for (int i = k; i < A.Length; i++) + { + answer[B.Length + i] = A[i]; + } + } + if (answer == null) + { + answer = A; + } + + // end + + return answer; + } + public (int[] sum, int[] dif) Task6(int[] A, int[] B) + { + int[] sum = null, dif = null; + + // code here + + if (A.Length == B.Length) + { + int n = A.Length; + sum = new int[n]; + dif = new int[n]; + + for (int i = 0; i < A.Length; i++) + { + sum[i] = A[i] + B[i]; + dif[i] = A[i] - B[i]; + } + } + + // end + + return (sum, dif); + } + public double[] Task7(int[] array) + { + double[] normalized = null; + + // code here + + int n = array.Length; + int MinVal = array[0], MaxVal = array[0]; + for (int i = 0; i < n; i++) + { + MinVal = Math.Min(MinVal, array[i]); + MaxVal = Math.Max(MaxVal, array[i]); + } + if (MinVal != MaxVal) + { + normalized = new double[n]; + for (int i = 0; i < n; i++) + { + normalized[i] = ((double)(array[i] - MinVal)) / (MaxVal - MinVal) * 2 - 1; + } + } + + // end + + return normalized; + } + public int[] Task8(int[] A, int[] B) + { + int[] C = null; + + // code here + + int n = A.Length; + for (int i = 0; i < n - 1; i++) + { + int MaxInd = i; + int MaxVal = A[i]; + for (int j = i + 1; j < n; j++) + { + if (A[j] > MaxVal) + { + MaxInd = j; + MaxVal = A[j]; + } + } + (A[i], A[MaxInd]) = (A[MaxInd], A[i]); + } + + int m = B.Length; + for (int i = 0; i < m - 1; i++) + { + int MaxInd = i; + int MaxVal = B[i]; + for (int j = i + 1; j < m; j++) + { + if (B[j] > MaxVal) + { + MaxInd = j; + MaxVal = B[j]; + } + } + (B[i], B[MaxInd]) = (B[MaxInd], B[i]); + } + + C = new int[n + m]; + int i_A = 0, i_B = 0, i_C = 0; + while (i_A < n || i_B < m) + { + if (i_A < n && i_B < m) + { + if (A[i_A] > B[i_B]) + { + C[i_C++] = A[i_A++]; + } + else + { + C[i_C++] = B[i_B++]; + } + } + else if (i_A < n) + { + C[i_C++] = A[i_A++]; + } + else + { + C[i_C++] = B[i_B++]; + } + } + + // end + + return C; + } + public void Task9(int[] array) + { + + // code here + + int n = array.Length; + int MaxInd = 0; + int MaxVal = array[0]; + for (int i = 0; i < n; i++) + { + if (array[i] > MaxVal) + { + MaxVal = array[i]; + MaxInd = i; + } + } + + var moved = new int[n]; + for (int i = 0; i < n; i++) + { + moved[(i + MaxInd) % n] = array[i]; + } + // // Console.WriteLine(string.Join(" ", moved)); + for (int i = 0; i < n; i++) + { + array[i] = moved[i]; + } + + // end + + } + public void Task10(int[] array, int N) + { + + // code here + + N--; + for (int move = 1; move <= N && N + move < array.Length; move++) + { + array[N + move] = array[N - move]; + } + + // end + + } + public (double[], double[]) Task11(double a, double b, int n) + { + double[] X = null, Y = null; + double[] Xext = null, Yext = null; + + // code here + + X = new double[n]; + Y = new double[n]; + + if (n == 1) + { + X[0] = (a + b) / 2; + } + else if (n >= 2) + { + double dif = (b - a) / (n - 1); + for (int i = 0; i < n; i++) + { + X[i] = a + dif * i; + } + } + for (int i = 0; i < n; i++) + { + Y[i] = Math.Cos(X[i]) + X[i] * Math.Sin(X[i]); + } + + //// Console.WriteLine($"X: {string.Join(" ", X)}"); + //// Console.WriteLine($"Y: {string.Join(" ", Y)}"); + + if (a == b && n == 1 || a < b) + { + int cntExt = 0; + for (int i = 1; i < n - 1; i++) + { + if (Y[i - 1] < Y[i] && Y[i] > Y[i + 1] || + Y[i - 1] > Y[i] && Y[i] < Y[i + 1]) + { + cntExt++; + } + } + + Xext = new double[cntExt]; + Yext = new double[cntExt]; + int iExt = 0; + for (int i = 1; i < n - 1; i++) + { + if (Y[i - 1] < Y[i] && Y[i] > Y[i + 1] || + Y[i - 1] > Y[i] && Y[i] < Y[i + 1]) + { + Xext[iExt] = X[i]; + Yext[iExt++] = Y[i]; + } + } + } + + // end + + return (Xext, Yext); + } + + public (double[] bright, double[] normal, double[] dim) Task12(double[] raw) + { + double[] bright = null, normal = null, dim = null; + + // code here + + // a + + int n = raw.Length; + int bright_len = 0, dim_len = 0; + + double sum = 0; + foreach (double x in raw) + { + sum += x; + } + double mid = sum / n; + // Console.WriteLine($"mid: {mid}"); + + foreach (double x in raw) + { + if (x > mid * 2) + { + bright_len++; + } + if (x * 2 < mid) + { + dim_len++; + } + } + + bright = new double[bright_len]; + dim = new double[dim_len]; + int i_bright = 0, i_dim = 0; + + for (int i = 0; i < n; i++) + { + if (raw[i] > mid * 2) + { + bright[i_bright++] = raw[i]; + } + if (raw[i] * 2 < mid) + { + dim[i_dim++] = raw[i]; + } + } + // Console.WriteLine($"bright: {string.Join(" ", bright)}"); + // Console.WriteLine($"dim: {string.Join(" ", dim)}"); + + // b + + int good_cnt = n - bright_len - dim_len; + normal = new double[n]; + double good_sum = 0; + + for (int i = 0; i < n; i++) + { + if (!(raw[i] > mid * 2 || raw[i] * 2 < mid)) + { + normal[i] = raw[i]; + good_sum += normal[i]; + } + } + // Console.WriteLine($"normal: {string.Join(" ", normal)}"); + double good_mid = good_sum / good_cnt; + // Console.WriteLine($"good_mid: {good_mid}"); + + // c + + for (int i = 0; i < n; i++) + { + if (raw[i] > mid * 2 || raw[i] * 2 < mid) + { + normal[i] = (good_mid + raw[i]) / 2; + } + } + // Console.WriteLine($"normal: {string.Join(" ", normal)}"); + + // d + + for (int i = 0; i < n; i++) + { + bool changed = false; + for (int j = 1; j < n - i; j++) + { + if (normal[j - 1] < normal[j]) + { + (normal[j - 1], normal[j]) = (normal[j], normal[j - 1]); + changed = true; + } + } + if (!changed) + { + break; + } + } + // Console.WriteLine($"normal: {string.Join(" ", normal)}"); + + // end + + return (bright, normal, dim); + } + } +} \ No newline at end of file From fcd13a1a5674569e600702419e07a9df74802367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=88=D1=82=D0=B0=D0=BA=20=D0=98=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D1=8C?= Date: Fri, 24 Oct 2025 22:32:42 +0300 Subject: [PATCH 02/11] Add files via upload From 362d40cf8f4905a00c478e9d8ab53c0cfa1dcdfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=88=D1=82=D0=B0=D0=BA=20=D0=98=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D1=8C?= Date: Fri, 24 Oct 2025 22:51:33 +0300 Subject: [PATCH 03/11] Delete Purple.cs --- Purple.cs | 499 ------------------------------------------------------ 1 file changed, 499 deletions(-) delete mode 100644 Purple.cs diff --git a/Purple.cs b/Purple.cs deleted file mode 100644 index 1e24694..0000000 --- a/Purple.cs +++ /dev/null @@ -1,499 +0,0 @@ -using System.Security.Cryptography; - -namespace Lab4 -{ - public class Purple - { - public void Task1(double[] array) - { - - // code here - - int n = array.Length; - double sum_elem = 0; - double max_val = array[0]; - int max_ind = 0; - for (int i = 0; i < n; i++) - { - if (max_val < array[i]) - { - max_val = array[i]; - max_ind = i; - } - sum_elem += array[i]; - } - double mid = sum_elem / n; - - for (int i = max_ind + 1; i < n; i++) - { - array[i] = mid; - } - - // end - - } - public (int[] even, int[] odd) Task2(int[] array) - { - int[] even = null, odd = null; - - // code here - - int n = array.Length; - odd = new int[n / 2]; - even = new int[n - odd.Length]; - for (int i = 0; i < n; i++) - { - if (i % 2 == 0) - { - even[i / 2] = array[i]; - } - else - { - odd[i / 2] = array[i]; - } - } - - // end - - return (even, odd); - } - public int[] Task3(int[] array, int P) - { - int[] answer = null; - - // code here - - int n = array.Length; - int sum_elem = 0; - foreach (int elem in array) - { - sum_elem += elem; - } - double mid = (double)sum_elem / n; - - int closest_ind = 0; - double closest_dif = Math.Abs(array[0] - mid); - for (int i = 0; i < n; i++) - { - double new_dif = Math.Abs(array[i] - mid); - if (new_dif < closest_dif) - { - closest_dif = new_dif; - closest_ind = i; - } - } - answer = new int[n + 1]; - for (int i = 0; i <= closest_ind; i++) - { - answer[i] = array[i]; - } - answer[closest_ind + 1] = P; - for (int i = closest_ind + 1; i < n; i++) - { - answer[i + 1] = array[i]; - } - - // end - - return answer; - } - public void Task4(int[] array) - { - - // code here - - int n = array.Length; - int neg_size = 0; - for (int i = 0; i < n; i++) - { - if (array[i] < 0) - { - neg_size++; - } - } - int pos_size = n - neg_size; - - var pos = new int[pos_size]; - var neg = new int[neg_size]; - - int pos_ind = 0, neg_ind = 0; - for (int i = 0; i < n; i++) - { - if (array[i] < 0) - { - neg[neg_ind++] = array[i]; - } - else - { - pos[pos_ind++] = array[i]; - } - } - for (int i = 0; i < pos_size; i++) - { - array[i] = pos[i]; - } - for (int i = pos_size; i < n; i++) - { - array[i] = neg[i - pos_size]; - } - - // end - - } - public int[] Task5(int[] A, int[] B, int k) - { - int[] answer = null; - - // code here - - if (k < A.Length) - { - answer = new int[A.Length + B.Length]; - for (int i = 0; i < k; i++) - { - answer[i] = A[i]; - } - for (int i = 0; i < B.Length; i++) - { - answer[i + k] = B[i]; - } - for (int i = k; i < A.Length; i++) - { - answer[B.Length + i] = A[i]; - } - } - if (answer == null) - { - answer = A; - } - - // end - - return answer; - } - public (int[] sum, int[] dif) Task6(int[] A, int[] B) - { - int[] sum = null, dif = null; - - // code here - - if (A.Length == B.Length) - { - int n = A.Length; - sum = new int[n]; - dif = new int[n]; - - for (int i = 0; i < A.Length; i++) - { - sum[i] = A[i] + B[i]; - dif[i] = A[i] - B[i]; - } - } - - // end - - return (sum, dif); - } - public double[] Task7(int[] array) - { - double[] normalized = null; - - // code here - - int n = array.Length; - int MinVal = array[0], MaxVal = array[0]; - for (int i = 0; i < n; i++) - { - MinVal = Math.Min(MinVal, array[i]); - MaxVal = Math.Max(MaxVal, array[i]); - } - if (MinVal != MaxVal) - { - normalized = new double[n]; - for (int i = 0; i < n; i++) - { - normalized[i] = ((double)(array[i] - MinVal)) / (MaxVal - MinVal) * 2 - 1; - } - } - - // end - - return normalized; - } - public int[] Task8(int[] A, int[] B) - { - int[] C = null; - - // code here - - int n = A.Length; - for (int i = 0; i < n - 1; i++) - { - int MaxInd = i; - int MaxVal = A[i]; - for (int j = i + 1; j < n; j++) - { - if (A[j] > MaxVal) - { - MaxInd = j; - MaxVal = A[j]; - } - } - (A[i], A[MaxInd]) = (A[MaxInd], A[i]); - } - - int m = B.Length; - for (int i = 0; i < m - 1; i++) - { - int MaxInd = i; - int MaxVal = B[i]; - for (int j = i + 1; j < m; j++) - { - if (B[j] > MaxVal) - { - MaxInd = j; - MaxVal = B[j]; - } - } - (B[i], B[MaxInd]) = (B[MaxInd], B[i]); - } - - C = new int[n + m]; - int i_A = 0, i_B = 0, i_C = 0; - while (i_A < n || i_B < m) - { - if (i_A < n && i_B < m) - { - if (A[i_A] > B[i_B]) - { - C[i_C++] = A[i_A++]; - } - else - { - C[i_C++] = B[i_B++]; - } - } - else if (i_A < n) - { - C[i_C++] = A[i_A++]; - } - else - { - C[i_C++] = B[i_B++]; - } - } - - // end - - return C; - } - public void Task9(int[] array) - { - - // code here - - int n = array.Length; - int MaxInd = 0; - int MaxVal = array[0]; - for (int i = 0; i < n; i++) - { - if (array[i] > MaxVal) - { - MaxVal = array[i]; - MaxInd = i; - } - } - - var moved = new int[n]; - for (int i = 0; i < n; i++) - { - moved[(i + MaxInd) % n] = array[i]; - } - // // Console.WriteLine(string.Join(" ", moved)); - for (int i = 0; i < n; i++) - { - array[i] = moved[i]; - } - - // end - - } - public void Task10(int[] array, int N) - { - - // code here - - N--; - for (int move = 1; move <= N && N + move < array.Length; move++) - { - array[N + move] = array[N - move]; - } - - // end - - } - public (double[], double[]) Task11(double a, double b, int n) - { - double[] X = null, Y = null; - double[] Xext = null, Yext = null; - - // code here - - X = new double[n]; - Y = new double[n]; - - if (n == 1) - { - X[0] = (a + b) / 2; - } - else if (n >= 2) - { - double dif = (b - a) / (n - 1); - for (int i = 0; i < n; i++) - { - X[i] = a + dif * i; - } - } - for (int i = 0; i < n; i++) - { - Y[i] = Math.Cos(X[i]) + X[i] * Math.Sin(X[i]); - } - - //// Console.WriteLine($"X: {string.Join(" ", X)}"); - //// Console.WriteLine($"Y: {string.Join(" ", Y)}"); - - if (a == b && n == 1 || a < b) - { - int cntExt = 0; - for (int i = 1; i < n - 1; i++) - { - if (Y[i - 1] < Y[i] && Y[i] > Y[i + 1] || - Y[i - 1] > Y[i] && Y[i] < Y[i + 1]) - { - cntExt++; - } - } - - Xext = new double[cntExt]; - Yext = new double[cntExt]; - int iExt = 0; - for (int i = 1; i < n - 1; i++) - { - if (Y[i - 1] < Y[i] && Y[i] > Y[i + 1] || - Y[i - 1] > Y[i] && Y[i] < Y[i + 1]) - { - Xext[iExt] = X[i]; - Yext[iExt++] = Y[i]; - } - } - } - - // end - - return (Xext, Yext); - } - - public (double[] bright, double[] normal, double[] dim) Task12(double[] raw) - { - double[] bright = null, normal = null, dim = null; - - // code here - - // a - - int n = raw.Length; - int bright_len = 0, dim_len = 0; - - double sum = 0; - foreach (double x in raw) - { - sum += x; - } - double mid = sum / n; - // Console.WriteLine($"mid: {mid}"); - - foreach (double x in raw) - { - if (x > mid * 2) - { - bright_len++; - } - if (x * 2 < mid) - { - dim_len++; - } - } - - bright = new double[bright_len]; - dim = new double[dim_len]; - int i_bright = 0, i_dim = 0; - - for (int i = 0; i < n; i++) - { - if (raw[i] > mid * 2) - { - bright[i_bright++] = raw[i]; - } - if (raw[i] * 2 < mid) - { - dim[i_dim++] = raw[i]; - } - } - // Console.WriteLine($"bright: {string.Join(" ", bright)}"); - // Console.WriteLine($"dim: {string.Join(" ", dim)}"); - - // b - - int good_cnt = n - bright_len - dim_len; - normal = new double[n]; - double good_sum = 0; - - for (int i = 0; i < n; i++) - { - if (!(raw[i] > mid * 2 || raw[i] * 2 < mid)) - { - normal[i] = raw[i]; - good_sum += normal[i]; - } - } - // Console.WriteLine($"normal: {string.Join(" ", normal)}"); - double good_mid = good_sum / good_cnt; - // Console.WriteLine($"good_mid: {good_mid}"); - - // c - - for (int i = 0; i < n; i++) - { - if (raw[i] > mid * 2 || raw[i] * 2 < mid) - { - normal[i] = (good_mid + raw[i]) / 2; - } - } - // Console.WriteLine($"normal: {string.Join(" ", normal)}"); - - // d - - for (int i = 0; i < n; i++) - { - bool changed = false; - for (int j = 1; j < n - i; j++) - { - if (normal[j - 1] < normal[j]) - { - (normal[j - 1], normal[j]) = (normal[j], normal[j - 1]); - changed = true; - } - } - if (!changed) - { - break; - } - } - // Console.WriteLine($"normal: {string.Join(" ", normal)}"); - - // end - - return (bright, normal, dim); - } - } -} \ No newline at end of file From 0ea60a65064126833043df8f301ced80b5e13954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=88=D1=82=D0=B0=D0=BA=20=D0=98=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D1=8C?= Date: Fri, 24 Oct 2025 22:51:49 +0300 Subject: [PATCH 04/11] Delete Program.cs --- Program.cs | 144 ----------------------------------------------------- 1 file changed, 144 deletions(-) delete mode 100644 Program.cs diff --git a/Program.cs b/Program.cs deleted file mode 100644 index b4b8df7..0000000 --- a/Program.cs +++ /dev/null @@ -1,144 +0,0 @@ -namespace Lab4 -{ - public class Program - { - public static void Main() - { - Purple purple = new Purple(); - - // Task 3 - - Console.WriteLine("Task 3\n"); - - var sizes = new int [] { 4, 8, 12, 10, 15, 22 }; - for (int i = 0; i < 6; i++) - { - int n = sizes[i]; - var a = new int[n]; - Random k = new Random(); - int sum = 0; - for (int j = 0; j < n; j++) - { - a[j] = k.Next(11); - sum += a[j]; - } - Console.WriteLine(string.Join(" ", a)); - double mid = (double)sum / n; - Console.WriteLine($"mid: {mid}"); - int p = (int)(mid + k.Next(-3, 4)); - Console.WriteLine($"p: {p}"); - var ans_a = purple.Task3(a, p); - Console.WriteLine(string.Join(" ", ans_a)); - Console.WriteLine(); - } - - // Task 4 - - Console.WriteLine("Task 4\n"); - - var a4 = new int[] { 3, -6, -2, 2, 4, 62, -32, 1, 0, 2}; - Console.WriteLine(string.Join(" ", a4)); - purple.Task4(a4); - Console.WriteLine($"{string.Join(" ", a4)}\n"); - - // Task 5 - - Console.WriteLine("Task 5\n"); - - Random gen = new Random(); - - var a5 = new int[] { 334, -61, -243, 23, 41, 62, -32, 10, 54, 24 }; - var b5 = new int[] { 0, 1, 2, 3 }; - Console.WriteLine($"a: {string.Join(" ", b5)}\n"); - Console.WriteLine($"b: {string.Join(" ", a5)}\n"); - - var k_ind = new int[] {0, gen.Next(4, 7), a5.Length - 1, 50}; - - foreach (int k_ind_i in k_ind) - { - Console.WriteLine($"k: {k_ind_i}"); - var ans5 = purple.Task5(a5, b5, k_ind_i); - if (ans5 != null) - { - Console.WriteLine($"{string.Join(" ", ans5)}\n"); - } - } - - // Task 7 - - Console.WriteLine("Task 7\n"); - - var a7 = new int[] { 0, 1, 4, 8, 6, 7, 10 }; - var ans7 = purple.Task7(a7); - Console.WriteLine(string.Join(" ", a7)); - Console.WriteLine($"{string.Join(" ", ans7)}\n"); - - // Task 8 - - Console.WriteLine("Task 8\n"); - - var a8 = new int[] { 3, -4, 2, 1 }; - var b8 = new int[] { 9, -2, 5, -2, 4, 2, 2 }; - Console.WriteLine(string.Join(" ", a8)); - Console.WriteLine(string.Join(" ", b8)); - var ans8 = purple.Task8(a8, b8); - Console.WriteLine(string.Join(" ", a8)); - Console.WriteLine(string.Join(" ", b8)); - Console.WriteLine($"{string.Join(" ", ans8)}\n"); - - // Task 9 - - Console.WriteLine("Task 9\n"); - - var a9 = new int[] { -2, -5, 3, -4, 2, 1, 2, 3, 3, 0, -6 }; - Console.WriteLine(string.Join(" ", a9)); - purple.Task9(a9); - Console.WriteLine($"{string.Join(" ", a9)}\n"); - - // Task 10 - - Console.WriteLine("Task 10\n"); - - var a10 = new int[] { 3, 1, 8, 4, 9, 5, 7, 0, -2, 31, 4 }; - int N10 = 3; - Console.WriteLine(string.Join(" ", a10)); - purple.Task10(a10, N10); - Console.WriteLine($"{string.Join(" ", a10)}\n"); - - N10 = 8; - Console.WriteLine(string.Join(" ", a10)); - purple.Task10(a10, N10); - Console.WriteLine($"{string.Join(" ", a10)}\n"); - - // Task 11 - - Console.WriteLine("Task 11\n"); - - double[] a11 = { 7, 7, 4, 10, 10, 10, 10, 10 }; - double[] b11 = { 7, 7, 0, 19, 19, 19, 19, 19 }; - int[] n11 = { 1, 5, 3, 0, 1, 2, 3, 6 }; - double[] Xext, Yext; - - for (int i = 0; i < n11.Length; i++) - { - Console.WriteLine($"a = {a11[i]} b = {b11[i]} n = {n11[i]}"); - (Xext, Yext) = purple.Task11(a11[i], b11[i], n11[i]); - if (Xext != null) - { - Console.WriteLine(Xext.Length); - Console.WriteLine($"Xext: {string.Join(" ", Xext)}"); - Console.WriteLine($"Yext: {string.Join(" ", Yext)}"); - } - Console.WriteLine(); - } - - // Task 12 - - Console.WriteLine("Task 12\n"); - - double[] raw12 = { 7, 10, 3, 8, 4, -1, -3, 1, -2, 4, 6, -3, 6, 0, -3 }; - Console.WriteLine($"raw: {string.Join(" ", raw12)}"); - purple.Task12(raw12); - } - } -} From f65b4bdbecb309a77a649889f8adedd83c1c2453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=88=D1=82=D0=B0=D0=BA=20=D0=98=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D1=8C?= Date: Fri, 24 Oct 2025 22:52:17 +0300 Subject: [PATCH 05/11] Add files via upload --- Lab4/Program.cs | 135 +++++++++++++++++ Lab4/Purple.cs | 383 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 516 insertions(+), 2 deletions(-) diff --git a/Lab4/Program.cs b/Lab4/Program.cs index 59898f2..b4b8df7 100644 --- a/Lab4/Program.cs +++ b/Lab4/Program.cs @@ -4,6 +4,141 @@ public class Program { public static void Main() { + Purple purple = new Purple(); + + // Task 3 + + Console.WriteLine("Task 3\n"); + + var sizes = new int [] { 4, 8, 12, 10, 15, 22 }; + for (int i = 0; i < 6; i++) + { + int n = sizes[i]; + var a = new int[n]; + Random k = new Random(); + int sum = 0; + for (int j = 0; j < n; j++) + { + a[j] = k.Next(11); + sum += a[j]; + } + Console.WriteLine(string.Join(" ", a)); + double mid = (double)sum / n; + Console.WriteLine($"mid: {mid}"); + int p = (int)(mid + k.Next(-3, 4)); + Console.WriteLine($"p: {p}"); + var ans_a = purple.Task3(a, p); + Console.WriteLine(string.Join(" ", ans_a)); + Console.WriteLine(); + } + + // Task 4 + + Console.WriteLine("Task 4\n"); + + var a4 = new int[] { 3, -6, -2, 2, 4, 62, -32, 1, 0, 2}; + Console.WriteLine(string.Join(" ", a4)); + purple.Task4(a4); + Console.WriteLine($"{string.Join(" ", a4)}\n"); + + // Task 5 + + Console.WriteLine("Task 5\n"); + + Random gen = new Random(); + + var a5 = new int[] { 334, -61, -243, 23, 41, 62, -32, 10, 54, 24 }; + var b5 = new int[] { 0, 1, 2, 3 }; + Console.WriteLine($"a: {string.Join(" ", b5)}\n"); + Console.WriteLine($"b: {string.Join(" ", a5)}\n"); + + var k_ind = new int[] {0, gen.Next(4, 7), a5.Length - 1, 50}; + + foreach (int k_ind_i in k_ind) + { + Console.WriteLine($"k: {k_ind_i}"); + var ans5 = purple.Task5(a5, b5, k_ind_i); + if (ans5 != null) + { + Console.WriteLine($"{string.Join(" ", ans5)}\n"); + } + } + + // Task 7 + + Console.WriteLine("Task 7\n"); + + var a7 = new int[] { 0, 1, 4, 8, 6, 7, 10 }; + var ans7 = purple.Task7(a7); + Console.WriteLine(string.Join(" ", a7)); + Console.WriteLine($"{string.Join(" ", ans7)}\n"); + + // Task 8 + + Console.WriteLine("Task 8\n"); + + var a8 = new int[] { 3, -4, 2, 1 }; + var b8 = new int[] { 9, -2, 5, -2, 4, 2, 2 }; + Console.WriteLine(string.Join(" ", a8)); + Console.WriteLine(string.Join(" ", b8)); + var ans8 = purple.Task8(a8, b8); + Console.WriteLine(string.Join(" ", a8)); + Console.WriteLine(string.Join(" ", b8)); + Console.WriteLine($"{string.Join(" ", ans8)}\n"); + + // Task 9 + + Console.WriteLine("Task 9\n"); + + var a9 = new int[] { -2, -5, 3, -4, 2, 1, 2, 3, 3, 0, -6 }; + Console.WriteLine(string.Join(" ", a9)); + purple.Task9(a9); + Console.WriteLine($"{string.Join(" ", a9)}\n"); + + // Task 10 + + Console.WriteLine("Task 10\n"); + + var a10 = new int[] { 3, 1, 8, 4, 9, 5, 7, 0, -2, 31, 4 }; + int N10 = 3; + Console.WriteLine(string.Join(" ", a10)); + purple.Task10(a10, N10); + Console.WriteLine($"{string.Join(" ", a10)}\n"); + + N10 = 8; + Console.WriteLine(string.Join(" ", a10)); + purple.Task10(a10, N10); + Console.WriteLine($"{string.Join(" ", a10)}\n"); + + // Task 11 + + Console.WriteLine("Task 11\n"); + + double[] a11 = { 7, 7, 4, 10, 10, 10, 10, 10 }; + double[] b11 = { 7, 7, 0, 19, 19, 19, 19, 19 }; + int[] n11 = { 1, 5, 3, 0, 1, 2, 3, 6 }; + double[] Xext, Yext; + + for (int i = 0; i < n11.Length; i++) + { + Console.WriteLine($"a = {a11[i]} b = {b11[i]} n = {n11[i]}"); + (Xext, Yext) = purple.Task11(a11[i], b11[i], n11[i]); + if (Xext != null) + { + Console.WriteLine(Xext.Length); + Console.WriteLine($"Xext: {string.Join(" ", Xext)}"); + Console.WriteLine($"Yext: {string.Join(" ", Yext)}"); + } + Console.WriteLine(); + } + + // Task 12 + + Console.WriteLine("Task 12\n"); + + double[] raw12 = { 7, 10, 3, 8, 4, -1, -3, 1, -2, 4, 6, -3, 6, 0, -3 }; + Console.WriteLine($"raw: {string.Join(" ", raw12)}"); + purple.Task12(raw12); } } } diff --git a/Lab4/Purple.cs b/Lab4/Purple.cs index cb4bd6a..1e24694 100644 --- a/Lab4/Purple.cs +++ b/Lab4/Purple.cs @@ -1,4 +1,6 @@ -namespace Lab4 +using System.Security.Cryptography; + +namespace Lab4 { public class Purple { @@ -7,6 +9,26 @@ public void Task1(double[] array) // code here + int n = array.Length; + double sum_elem = 0; + double max_val = array[0]; + int max_ind = 0; + for (int i = 0; i < n; i++) + { + if (max_val < array[i]) + { + max_val = array[i]; + max_ind = i; + } + sum_elem += array[i]; + } + double mid = sum_elem / n; + + for (int i = max_ind + 1; i < n; i++) + { + array[i] = mid; + } + // end } @@ -16,6 +38,21 @@ public void Task1(double[] array) // code here + int n = array.Length; + odd = new int[n / 2]; + even = new int[n - odd.Length]; + for (int i = 0; i < n; i++) + { + if (i % 2 == 0) + { + even[i / 2] = array[i]; + } + else + { + odd[i / 2] = array[i]; + } + } + // end return (even, odd); @@ -26,6 +63,36 @@ public int[] Task3(int[] array, int P) // code here + int n = array.Length; + int sum_elem = 0; + foreach (int elem in array) + { + sum_elem += elem; + } + double mid = (double)sum_elem / n; + + int closest_ind = 0; + double closest_dif = Math.Abs(array[0] - mid); + for (int i = 0; i < n; i++) + { + double new_dif = Math.Abs(array[i] - mid); + if (new_dif < closest_dif) + { + closest_dif = new_dif; + closest_ind = i; + } + } + answer = new int[n + 1]; + for (int i = 0; i <= closest_ind; i++) + { + answer[i] = array[i]; + } + answer[closest_ind + 1] = P; + for (int i = closest_ind + 1; i < n; i++) + { + answer[i + 1] = array[i]; + } + // end return answer; @@ -35,6 +102,41 @@ public void Task4(int[] array) // code here + int n = array.Length; + int neg_size = 0; + for (int i = 0; i < n; i++) + { + if (array[i] < 0) + { + neg_size++; + } + } + int pos_size = n - neg_size; + + var pos = new int[pos_size]; + var neg = new int[neg_size]; + + int pos_ind = 0, neg_ind = 0; + for (int i = 0; i < n; i++) + { + if (array[i] < 0) + { + neg[neg_ind++] = array[i]; + } + else + { + pos[pos_ind++] = array[i]; + } + } + for (int i = 0; i < pos_size; i++) + { + array[i] = pos[i]; + } + for (int i = pos_size; i < n; i++) + { + array[i] = neg[i - pos_size]; + } + // end } @@ -44,6 +146,27 @@ public int[] Task5(int[] A, int[] B, int k) // code here + if (k < A.Length) + { + answer = new int[A.Length + B.Length]; + for (int i = 0; i < k; i++) + { + answer[i] = A[i]; + } + for (int i = 0; i < B.Length; i++) + { + answer[i + k] = B[i]; + } + for (int i = k; i < A.Length; i++) + { + answer[B.Length + i] = A[i]; + } + } + if (answer == null) + { + answer = A; + } + // end return answer; @@ -54,6 +177,19 @@ public int[] Task5(int[] A, int[] B, int k) // code here + if (A.Length == B.Length) + { + int n = A.Length; + sum = new int[n]; + dif = new int[n]; + + for (int i = 0; i < A.Length; i++) + { + sum[i] = A[i] + B[i]; + dif[i] = A[i] - B[i]; + } + } + // end return (sum, dif); @@ -64,8 +200,24 @@ public double[] Task7(int[] array) // code here + int n = array.Length; + int MinVal = array[0], MaxVal = array[0]; + for (int i = 0; i < n; i++) + { + MinVal = Math.Min(MinVal, array[i]); + MaxVal = Math.Max(MaxVal, array[i]); + } + if (MinVal != MaxVal) + { + normalized = new double[n]; + for (int i = 0; i < n; i++) + { + normalized[i] = ((double)(array[i] - MinVal)) / (MaxVal - MinVal) * 2 - 1; + } + } + // end - + return normalized; } public int[] Task8(int[] A, int[] B) @@ -74,6 +226,63 @@ public int[] Task8(int[] A, int[] B) // code here + int n = A.Length; + for (int i = 0; i < n - 1; i++) + { + int MaxInd = i; + int MaxVal = A[i]; + for (int j = i + 1; j < n; j++) + { + if (A[j] > MaxVal) + { + MaxInd = j; + MaxVal = A[j]; + } + } + (A[i], A[MaxInd]) = (A[MaxInd], A[i]); + } + + int m = B.Length; + for (int i = 0; i < m - 1; i++) + { + int MaxInd = i; + int MaxVal = B[i]; + for (int j = i + 1; j < m; j++) + { + if (B[j] > MaxVal) + { + MaxInd = j; + MaxVal = B[j]; + } + } + (B[i], B[MaxInd]) = (B[MaxInd], B[i]); + } + + C = new int[n + m]; + int i_A = 0, i_B = 0, i_C = 0; + while (i_A < n || i_B < m) + { + if (i_A < n && i_B < m) + { + if (A[i_A] > B[i_B]) + { + C[i_C++] = A[i_A++]; + } + else + { + C[i_C++] = B[i_B++]; + } + } + else if (i_A < n) + { + C[i_C++] = A[i_A++]; + } + else + { + C[i_C++] = B[i_B++]; + } + } + // end return C; @@ -83,6 +292,29 @@ public void Task9(int[] array) // code here + int n = array.Length; + int MaxInd = 0; + int MaxVal = array[0]; + for (int i = 0; i < n; i++) + { + if (array[i] > MaxVal) + { + MaxVal = array[i]; + MaxInd = i; + } + } + + var moved = new int[n]; + for (int i = 0; i < n; i++) + { + moved[(i + MaxInd) % n] = array[i]; + } + // // Console.WriteLine(string.Join(" ", moved)); + for (int i = 0; i < n; i++) + { + array[i] = moved[i]; + } + // end } @@ -91,6 +323,12 @@ public void Task10(int[] array, int N) // code here + N--; + for (int move = 1; move <= N && N + move < array.Length; move++) + { + array[N + move] = array[N - move]; + } + // end } @@ -101,6 +339,55 @@ public void Task10(int[] array, int N) // code here + X = new double[n]; + Y = new double[n]; + + if (n == 1) + { + X[0] = (a + b) / 2; + } + else if (n >= 2) + { + double dif = (b - a) / (n - 1); + for (int i = 0; i < n; i++) + { + X[i] = a + dif * i; + } + } + for (int i = 0; i < n; i++) + { + Y[i] = Math.Cos(X[i]) + X[i] * Math.Sin(X[i]); + } + + //// Console.WriteLine($"X: {string.Join(" ", X)}"); + //// Console.WriteLine($"Y: {string.Join(" ", Y)}"); + + if (a == b && n == 1 || a < b) + { + int cntExt = 0; + for (int i = 1; i < n - 1; i++) + { + if (Y[i - 1] < Y[i] && Y[i] > Y[i + 1] || + Y[i - 1] > Y[i] && Y[i] < Y[i + 1]) + { + cntExt++; + } + } + + Xext = new double[cntExt]; + Yext = new double[cntExt]; + int iExt = 0; + for (int i = 1; i < n - 1; i++) + { + if (Y[i - 1] < Y[i] && Y[i] > Y[i + 1] || + Y[i - 1] > Y[i] && Y[i] < Y[i + 1]) + { + Xext[iExt] = X[i]; + Yext[iExt++] = Y[i]; + } + } + } + // end return (Xext, Yext); @@ -112,6 +399,98 @@ public void Task10(int[] array, int N) // code here + // a + + int n = raw.Length; + int bright_len = 0, dim_len = 0; + + double sum = 0; + foreach (double x in raw) + { + sum += x; + } + double mid = sum / n; + // Console.WriteLine($"mid: {mid}"); + + foreach (double x in raw) + { + if (x > mid * 2) + { + bright_len++; + } + if (x * 2 < mid) + { + dim_len++; + } + } + + bright = new double[bright_len]; + dim = new double[dim_len]; + int i_bright = 0, i_dim = 0; + + for (int i = 0; i < n; i++) + { + if (raw[i] > mid * 2) + { + bright[i_bright++] = raw[i]; + } + if (raw[i] * 2 < mid) + { + dim[i_dim++] = raw[i]; + } + } + // Console.WriteLine($"bright: {string.Join(" ", bright)}"); + // Console.WriteLine($"dim: {string.Join(" ", dim)}"); + + // b + + int good_cnt = n - bright_len - dim_len; + normal = new double[n]; + double good_sum = 0; + + for (int i = 0; i < n; i++) + { + if (!(raw[i] > mid * 2 || raw[i] * 2 < mid)) + { + normal[i] = raw[i]; + good_sum += normal[i]; + } + } + // Console.WriteLine($"normal: {string.Join(" ", normal)}"); + double good_mid = good_sum / good_cnt; + // Console.WriteLine($"good_mid: {good_mid}"); + + // c + + for (int i = 0; i < n; i++) + { + if (raw[i] > mid * 2 || raw[i] * 2 < mid) + { + normal[i] = (good_mid + raw[i]) / 2; + } + } + // Console.WriteLine($"normal: {string.Join(" ", normal)}"); + + // d + + for (int i = 0; i < n; i++) + { + bool changed = false; + for (int j = 1; j < n - i; j++) + { + if (normal[j - 1] < normal[j]) + { + (normal[j - 1], normal[j]) = (normal[j], normal[j - 1]); + changed = true; + } + } + if (!changed) + { + break; + } + } + // Console.WriteLine($"normal: {string.Join(" ", normal)}"); + // end return (bright, normal, dim); From 1e30dd82f608b559f74ebc7c0155287d0f112cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=88=D1=82=D0=B0=D0=BA=20=D0=98=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D1=8C?= Date: Fri, 24 Oct 2025 23:08:44 +0300 Subject: [PATCH 06/11] Add files via upload --- Lab4/Purple.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lab4/Purple.cs b/Lab4/Purple.cs index 1e24694..badc5f6 100644 --- a/Lab4/Purple.cs +++ b/Lab4/Purple.cs @@ -1,6 +1,4 @@ -using System.Security.Cryptography; - -namespace Lab4 +namespace Lab4 { public class Purple { From ae6fe73ec4cf49f308a06a3e89e3998df83ec012 Mon Sep 17 00:00:00 2001 From: Mark <112826355+NemirovichMark@users.noreply.github.com> Date: Tue, 28 Oct 2025 12:28:21 +0300 Subject: [PATCH 07/11] =?UTF-8?q?Revert=20"=D0=9C=D0=B0=D1=88=D1=82=D0=B0?= =?UTF-8?q?=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=91=D0=98=D0=92?= =?UTF-8?q?=D0=A2-25-16=20Purple"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lab4/Program.cs | 135 ----------------- Lab4/Purple.cs | 379 +----------------------------------------------- 2 files changed, 1 insertion(+), 513 deletions(-) diff --git a/Lab4/Program.cs b/Lab4/Program.cs index b4b8df7..59898f2 100644 --- a/Lab4/Program.cs +++ b/Lab4/Program.cs @@ -4,141 +4,6 @@ public class Program { public static void Main() { - Purple purple = new Purple(); - - // Task 3 - - Console.WriteLine("Task 3\n"); - - var sizes = new int [] { 4, 8, 12, 10, 15, 22 }; - for (int i = 0; i < 6; i++) - { - int n = sizes[i]; - var a = new int[n]; - Random k = new Random(); - int sum = 0; - for (int j = 0; j < n; j++) - { - a[j] = k.Next(11); - sum += a[j]; - } - Console.WriteLine(string.Join(" ", a)); - double mid = (double)sum / n; - Console.WriteLine($"mid: {mid}"); - int p = (int)(mid + k.Next(-3, 4)); - Console.WriteLine($"p: {p}"); - var ans_a = purple.Task3(a, p); - Console.WriteLine(string.Join(" ", ans_a)); - Console.WriteLine(); - } - - // Task 4 - - Console.WriteLine("Task 4\n"); - - var a4 = new int[] { 3, -6, -2, 2, 4, 62, -32, 1, 0, 2}; - Console.WriteLine(string.Join(" ", a4)); - purple.Task4(a4); - Console.WriteLine($"{string.Join(" ", a4)}\n"); - - // Task 5 - - Console.WriteLine("Task 5\n"); - - Random gen = new Random(); - - var a5 = new int[] { 334, -61, -243, 23, 41, 62, -32, 10, 54, 24 }; - var b5 = new int[] { 0, 1, 2, 3 }; - Console.WriteLine($"a: {string.Join(" ", b5)}\n"); - Console.WriteLine($"b: {string.Join(" ", a5)}\n"); - - var k_ind = new int[] {0, gen.Next(4, 7), a5.Length - 1, 50}; - - foreach (int k_ind_i in k_ind) - { - Console.WriteLine($"k: {k_ind_i}"); - var ans5 = purple.Task5(a5, b5, k_ind_i); - if (ans5 != null) - { - Console.WriteLine($"{string.Join(" ", ans5)}\n"); - } - } - - // Task 7 - - Console.WriteLine("Task 7\n"); - - var a7 = new int[] { 0, 1, 4, 8, 6, 7, 10 }; - var ans7 = purple.Task7(a7); - Console.WriteLine(string.Join(" ", a7)); - Console.WriteLine($"{string.Join(" ", ans7)}\n"); - - // Task 8 - - Console.WriteLine("Task 8\n"); - - var a8 = new int[] { 3, -4, 2, 1 }; - var b8 = new int[] { 9, -2, 5, -2, 4, 2, 2 }; - Console.WriteLine(string.Join(" ", a8)); - Console.WriteLine(string.Join(" ", b8)); - var ans8 = purple.Task8(a8, b8); - Console.WriteLine(string.Join(" ", a8)); - Console.WriteLine(string.Join(" ", b8)); - Console.WriteLine($"{string.Join(" ", ans8)}\n"); - - // Task 9 - - Console.WriteLine("Task 9\n"); - - var a9 = new int[] { -2, -5, 3, -4, 2, 1, 2, 3, 3, 0, -6 }; - Console.WriteLine(string.Join(" ", a9)); - purple.Task9(a9); - Console.WriteLine($"{string.Join(" ", a9)}\n"); - - // Task 10 - - Console.WriteLine("Task 10\n"); - - var a10 = new int[] { 3, 1, 8, 4, 9, 5, 7, 0, -2, 31, 4 }; - int N10 = 3; - Console.WriteLine(string.Join(" ", a10)); - purple.Task10(a10, N10); - Console.WriteLine($"{string.Join(" ", a10)}\n"); - - N10 = 8; - Console.WriteLine(string.Join(" ", a10)); - purple.Task10(a10, N10); - Console.WriteLine($"{string.Join(" ", a10)}\n"); - - // Task 11 - - Console.WriteLine("Task 11\n"); - - double[] a11 = { 7, 7, 4, 10, 10, 10, 10, 10 }; - double[] b11 = { 7, 7, 0, 19, 19, 19, 19, 19 }; - int[] n11 = { 1, 5, 3, 0, 1, 2, 3, 6 }; - double[] Xext, Yext; - - for (int i = 0; i < n11.Length; i++) - { - Console.WriteLine($"a = {a11[i]} b = {b11[i]} n = {n11[i]}"); - (Xext, Yext) = purple.Task11(a11[i], b11[i], n11[i]); - if (Xext != null) - { - Console.WriteLine(Xext.Length); - Console.WriteLine($"Xext: {string.Join(" ", Xext)}"); - Console.WriteLine($"Yext: {string.Join(" ", Yext)}"); - } - Console.WriteLine(); - } - - // Task 12 - - Console.WriteLine("Task 12\n"); - - double[] raw12 = { 7, 10, 3, 8, 4, -1, -3, 1, -2, 4, 6, -3, 6, 0, -3 }; - Console.WriteLine($"raw: {string.Join(" ", raw12)}"); - purple.Task12(raw12); } } } diff --git a/Lab4/Purple.cs b/Lab4/Purple.cs index badc5f6..cb4bd6a 100644 --- a/Lab4/Purple.cs +++ b/Lab4/Purple.cs @@ -7,26 +7,6 @@ public void Task1(double[] array) // code here - int n = array.Length; - double sum_elem = 0; - double max_val = array[0]; - int max_ind = 0; - for (int i = 0; i < n; i++) - { - if (max_val < array[i]) - { - max_val = array[i]; - max_ind = i; - } - sum_elem += array[i]; - } - double mid = sum_elem / n; - - for (int i = max_ind + 1; i < n; i++) - { - array[i] = mid; - } - // end } @@ -36,21 +16,6 @@ public void Task1(double[] array) // code here - int n = array.Length; - odd = new int[n / 2]; - even = new int[n - odd.Length]; - for (int i = 0; i < n; i++) - { - if (i % 2 == 0) - { - even[i / 2] = array[i]; - } - else - { - odd[i / 2] = array[i]; - } - } - // end return (even, odd); @@ -61,36 +26,6 @@ public int[] Task3(int[] array, int P) // code here - int n = array.Length; - int sum_elem = 0; - foreach (int elem in array) - { - sum_elem += elem; - } - double mid = (double)sum_elem / n; - - int closest_ind = 0; - double closest_dif = Math.Abs(array[0] - mid); - for (int i = 0; i < n; i++) - { - double new_dif = Math.Abs(array[i] - mid); - if (new_dif < closest_dif) - { - closest_dif = new_dif; - closest_ind = i; - } - } - answer = new int[n + 1]; - for (int i = 0; i <= closest_ind; i++) - { - answer[i] = array[i]; - } - answer[closest_ind + 1] = P; - for (int i = closest_ind + 1; i < n; i++) - { - answer[i + 1] = array[i]; - } - // end return answer; @@ -100,41 +35,6 @@ public void Task4(int[] array) // code here - int n = array.Length; - int neg_size = 0; - for (int i = 0; i < n; i++) - { - if (array[i] < 0) - { - neg_size++; - } - } - int pos_size = n - neg_size; - - var pos = new int[pos_size]; - var neg = new int[neg_size]; - - int pos_ind = 0, neg_ind = 0; - for (int i = 0; i < n; i++) - { - if (array[i] < 0) - { - neg[neg_ind++] = array[i]; - } - else - { - pos[pos_ind++] = array[i]; - } - } - for (int i = 0; i < pos_size; i++) - { - array[i] = pos[i]; - } - for (int i = pos_size; i < n; i++) - { - array[i] = neg[i - pos_size]; - } - // end } @@ -144,27 +44,6 @@ public int[] Task5(int[] A, int[] B, int k) // code here - if (k < A.Length) - { - answer = new int[A.Length + B.Length]; - for (int i = 0; i < k; i++) - { - answer[i] = A[i]; - } - for (int i = 0; i < B.Length; i++) - { - answer[i + k] = B[i]; - } - for (int i = k; i < A.Length; i++) - { - answer[B.Length + i] = A[i]; - } - } - if (answer == null) - { - answer = A; - } - // end return answer; @@ -175,19 +54,6 @@ public int[] Task5(int[] A, int[] B, int k) // code here - if (A.Length == B.Length) - { - int n = A.Length; - sum = new int[n]; - dif = new int[n]; - - for (int i = 0; i < A.Length; i++) - { - sum[i] = A[i] + B[i]; - dif[i] = A[i] - B[i]; - } - } - // end return (sum, dif); @@ -198,24 +64,8 @@ public double[] Task7(int[] array) // code here - int n = array.Length; - int MinVal = array[0], MaxVal = array[0]; - for (int i = 0; i < n; i++) - { - MinVal = Math.Min(MinVal, array[i]); - MaxVal = Math.Max(MaxVal, array[i]); - } - if (MinVal != MaxVal) - { - normalized = new double[n]; - for (int i = 0; i < n; i++) - { - normalized[i] = ((double)(array[i] - MinVal)) / (MaxVal - MinVal) * 2 - 1; - } - } - // end - + return normalized; } public int[] Task8(int[] A, int[] B) @@ -224,63 +74,6 @@ public int[] Task8(int[] A, int[] B) // code here - int n = A.Length; - for (int i = 0; i < n - 1; i++) - { - int MaxInd = i; - int MaxVal = A[i]; - for (int j = i + 1; j < n; j++) - { - if (A[j] > MaxVal) - { - MaxInd = j; - MaxVal = A[j]; - } - } - (A[i], A[MaxInd]) = (A[MaxInd], A[i]); - } - - int m = B.Length; - for (int i = 0; i < m - 1; i++) - { - int MaxInd = i; - int MaxVal = B[i]; - for (int j = i + 1; j < m; j++) - { - if (B[j] > MaxVal) - { - MaxInd = j; - MaxVal = B[j]; - } - } - (B[i], B[MaxInd]) = (B[MaxInd], B[i]); - } - - C = new int[n + m]; - int i_A = 0, i_B = 0, i_C = 0; - while (i_A < n || i_B < m) - { - if (i_A < n && i_B < m) - { - if (A[i_A] > B[i_B]) - { - C[i_C++] = A[i_A++]; - } - else - { - C[i_C++] = B[i_B++]; - } - } - else if (i_A < n) - { - C[i_C++] = A[i_A++]; - } - else - { - C[i_C++] = B[i_B++]; - } - } - // end return C; @@ -290,29 +83,6 @@ public void Task9(int[] array) // code here - int n = array.Length; - int MaxInd = 0; - int MaxVal = array[0]; - for (int i = 0; i < n; i++) - { - if (array[i] > MaxVal) - { - MaxVal = array[i]; - MaxInd = i; - } - } - - var moved = new int[n]; - for (int i = 0; i < n; i++) - { - moved[(i + MaxInd) % n] = array[i]; - } - // // Console.WriteLine(string.Join(" ", moved)); - for (int i = 0; i < n; i++) - { - array[i] = moved[i]; - } - // end } @@ -321,12 +91,6 @@ public void Task10(int[] array, int N) // code here - N--; - for (int move = 1; move <= N && N + move < array.Length; move++) - { - array[N + move] = array[N - move]; - } - // end } @@ -337,55 +101,6 @@ public void Task10(int[] array, int N) // code here - X = new double[n]; - Y = new double[n]; - - if (n == 1) - { - X[0] = (a + b) / 2; - } - else if (n >= 2) - { - double dif = (b - a) / (n - 1); - for (int i = 0; i < n; i++) - { - X[i] = a + dif * i; - } - } - for (int i = 0; i < n; i++) - { - Y[i] = Math.Cos(X[i]) + X[i] * Math.Sin(X[i]); - } - - //// Console.WriteLine($"X: {string.Join(" ", X)}"); - //// Console.WriteLine($"Y: {string.Join(" ", Y)}"); - - if (a == b && n == 1 || a < b) - { - int cntExt = 0; - for (int i = 1; i < n - 1; i++) - { - if (Y[i - 1] < Y[i] && Y[i] > Y[i + 1] || - Y[i - 1] > Y[i] && Y[i] < Y[i + 1]) - { - cntExt++; - } - } - - Xext = new double[cntExt]; - Yext = new double[cntExt]; - int iExt = 0; - for (int i = 1; i < n - 1; i++) - { - if (Y[i - 1] < Y[i] && Y[i] > Y[i + 1] || - Y[i - 1] > Y[i] && Y[i] < Y[i + 1]) - { - Xext[iExt] = X[i]; - Yext[iExt++] = Y[i]; - } - } - } - // end return (Xext, Yext); @@ -397,98 +112,6 @@ public void Task10(int[] array, int N) // code here - // a - - int n = raw.Length; - int bright_len = 0, dim_len = 0; - - double sum = 0; - foreach (double x in raw) - { - sum += x; - } - double mid = sum / n; - // Console.WriteLine($"mid: {mid}"); - - foreach (double x in raw) - { - if (x > mid * 2) - { - bright_len++; - } - if (x * 2 < mid) - { - dim_len++; - } - } - - bright = new double[bright_len]; - dim = new double[dim_len]; - int i_bright = 0, i_dim = 0; - - for (int i = 0; i < n; i++) - { - if (raw[i] > mid * 2) - { - bright[i_bright++] = raw[i]; - } - if (raw[i] * 2 < mid) - { - dim[i_dim++] = raw[i]; - } - } - // Console.WriteLine($"bright: {string.Join(" ", bright)}"); - // Console.WriteLine($"dim: {string.Join(" ", dim)}"); - - // b - - int good_cnt = n - bright_len - dim_len; - normal = new double[n]; - double good_sum = 0; - - for (int i = 0; i < n; i++) - { - if (!(raw[i] > mid * 2 || raw[i] * 2 < mid)) - { - normal[i] = raw[i]; - good_sum += normal[i]; - } - } - // Console.WriteLine($"normal: {string.Join(" ", normal)}"); - double good_mid = good_sum / good_cnt; - // Console.WriteLine($"good_mid: {good_mid}"); - - // c - - for (int i = 0; i < n; i++) - { - if (raw[i] > mid * 2 || raw[i] * 2 < mid) - { - normal[i] = (good_mid + raw[i]) / 2; - } - } - // Console.WriteLine($"normal: {string.Join(" ", normal)}"); - - // d - - for (int i = 0; i < n; i++) - { - bool changed = false; - for (int j = 1; j < n - i; j++) - { - if (normal[j - 1] < normal[j]) - { - (normal[j - 1], normal[j]) = (normal[j], normal[j - 1]); - changed = true; - } - } - if (!changed) - { - break; - } - } - // Console.WriteLine($"normal: {string.Join(" ", normal)}"); - // end return (bright, normal, dim); From 5209ef5b714dc6cafce37a902f3a5a3d1b5a6851 Mon Sep 17 00:00:00 2001 From: Mark <112826355+NemirovichMark@users.noreply.github.com> Date: Tue, 28 Oct 2025 12:30:40 +0300 Subject: [PATCH 08/11] Update Program.cs --- Lab4/Program.cs | 2 ++ 1 file changed, 2 insertions(+) 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() { + } } } + From 04fd162a69868fef74e226311562480863e121e2 Mon Sep 17 00:00:00 2001 From: Mark <112826355+NemirovichMark@users.noreply.github.com> Date: Tue, 28 Oct 2025 12:31:03 +0300 Subject: [PATCH 09/11] Update Purple.cs --- Lab4/Purple.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 + } From 5a4b85c3f2db0e9078d602b2deec326f4b996249 Mon Sep 17 00:00:00 2001 From: Mark <112826355+NemirovichMark@users.noreply.github.com> Date: Wed, 29 Oct 2025 15:01:13 +0300 Subject: [PATCH 10/11] Update PurpleTest.cs --- Lab4test/PurpleTest.cs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) 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 + +} From e2f3eb9b4a9ee940288cff38e514628453615d12 Mon Sep 17 00:00:00 2001 From: sardornr19 Date: Mon, 29 Dec 2025 16:28:53 +0300 Subject: [PATCH 11/11] =?UTF-8?q?=D0=9D=D0=BE=D1=80=D0=BC=D1=83=D1=80?= =?UTF-8?q?=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=A1=D0=B0=D1=80=D0=B4=D0=BE=D1=80?= =?UTF-8?q?=20=D0=91=D0=98=D0=92=D0=A2-25-12=20=D0=91=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B8=D0=B3=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lab4/White.cs | 259 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 218 insertions(+), 41 deletions(-) diff --git a/Lab4/White.cs b/Lab4/White.cs index d39815d..546db7e 100644 --- a/Lab4/White.cs +++ b/Lab4/White.cs @@ -1,103 +1,261 @@ -namespace Lab4 +namespace Lab4 { public class White { public double Task1(int[] vector) { double length = 0; - - // code here - + // code here + + for (int i = 0; i < vector.Length; i++) + { + length += vector[i] * vector[i]; + } + + double sqrt = 0; + double step = 0.00001; + while (sqrt * sqrt < length) + { + sqrt += step; + } // end - return length; + return sqrt; } + public int Task2(int[] array, int P, int Q) { int count = 0; - // code here + for (int i = 0; i < array.Length; i++) + { + if (array[i] > P && array[i] < Q) + { + count++; + } + } // end return count; } + public void Task3(int[] array) { - + int maxIndex = 0; // code here + for (int i = 1; i < array.Length; i++) + { + if (array[i] > array[maxIndex]) + { + maxIndex = i; + } + } + + if (maxIndex == array.Length - 1) + { + return; + } + + int minIndex = maxIndex + 1; + for (int i = maxIndex + 1; i < array.Length; i++) + { + if (array[i] < array[minIndex]) + { + minIndex = i; + } + } // end + int temp = array[maxIndex]; + array[maxIndex] = array[minIndex]; + array[minIndex] = temp; } + public void Task4(int[] array) { - + int max = array[0]; + int index = 0; // code here - // end - + for (int i = 1; i < array.Length; i++) + { + if (array[i] > max) + { + max = array[i]; + index = i; + } + } + // end + array[index] = index; } + public int Task5(int[] array, int P) { - int index = 0; - + int index = -1; // code here + for (int i = 0; i < array.Length; i++) + { + if (array[i] == P) + { + index = i; + break; + } + } // end return index; } + public void Task6(int[] array) { - + int maxIndex = 0; // code here + for (int i = 1; i < array.Length; i++) + { + if (array[i] > array[maxIndex]) + { + maxIndex = i; + } + } + + for (int i = 0; i + 1 < maxIndex; i += 2) + { + int temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + } // end - } + public int[] Task7(int[] array) { - int[] answer = null; - - // code here - + int count = 0; + // code here + for (int i = 0; i < array.Length; i++) + { + if (array[i] >= 0) + { + count++; + } + } + + int[] answer = new int[count]; + int k = 0; + + for (int i = 0; i < array.Length; i++) + { + if (array[i] >= 0) + { + answer[k] = array[i]; + k++; + } + } // end return answer; } + public void Task8(int[] array) { - - // code here - + // code here + for (int i = 0; i < array.Length - 1; i++) + { + for (int j = i + 1; j < array.Length; j++) + { + if (array[i] < array[j]) + { + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } + } // end - } + public void Task9(int[] array) { - - // code here - + int left = 0; + int right = array.Length - 1; + // code here + while (left < right) + { + int temp = array[left]; + array[left] = array[right]; + array[right] = temp; + + left++; + right--; + } // end - } + public int[] Task10(int[] A, int[] B) { - int[] C = null; - - // code here - - // end - + // code here + if (A.Length == 0) + { + return B; + } + + if (B.Length == 0) + { + return A; + } + + int[] C = new int[A.Length + B.Length]; + int k = 0; + + for (int i = 0; i < A.Length; i++) + { + C[k++] = A[i]; + if (i < B.Length) + { + C[k++] = B[i]; + } + } + + for (int i = A.Length; i < B.Length; i++) + { + C[k++] = B[i]; + } + // end return C; } + public double[] Task11(double a, double b, int n) { - double[] array = null; - - // code here - + // code here + if (n <= 0) + { + return null; + } + + if (a == b) + { + return new double[] { a }; + } + + double[] array = new double[n]; + + if (a > b) + { + for (int i = 0; i < n; i++) + { + array[i] = a - i; + } + } + else + { + for (int i = 0; i < n; i++) + { + array[i] = a + i; + } + } // end return array; @@ -105,13 +263,32 @@ public double[] Task11(double a, double b, int n) public double[] Task12(double[] raw) { - double[] restored = null; - - // code here - + // code here + if (raw.Length < 3) + { + return null; + } + + double[] restored = new double[raw.Length]; + + for (int i = 0; i < raw.Length; i++) + { + restored[i] = raw[i]; + } + + for (int i = 1; i < raw.Length - 1; i++) + { + if (restored[i] == -1) + { + if (restored[i - 1] != -1 && restored[i + 1] != -1) + { + restored[i] = (restored[i - 1] + restored[i + 1]) / 2; + } + } + } // end return restored; } } -} \ No newline at end of file +}