Advertisement
Tvor0zhok

C# работа с массивами

Feb 10th, 2022
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Example
  4. {
  5. class Program
  6. {
  7. const int MAX_N = 128;
  8.  
  9. static void add(int k, int x, ref int n, ref int[] a)
  10. {
  11. if (k >= 0 && k < n && n < MAX_N)
  12. {
  13. for (int i = n; i > k; --i)
  14. a[i] = a[i - 1];
  15.  
  16. a[k] = x;
  17.  
  18. ++n;
  19. }
  20. }
  21.  
  22. static void erase(int k, ref int n, ref int[] a)
  23. {
  24. if (n > 0 && k >= 0 && k < n)
  25. {
  26. for (int i = k; i < n - 1; ++i)
  27. a[i] = a[i + 1];
  28.  
  29. --n;
  30. }
  31. }
  32.  
  33. static void print(int n, int[] a)
  34. {
  35. for (int i = 0; i < n; ++i)
  36. Console.Write("{0} ", a[i]);
  37.  
  38. Console.Write("\n");
  39. }
  40.  
  41. static void Main()
  42. {
  43. Console.Write("n = ");
  44. int n = int.Parse(Console.ReadLine());
  45.  
  46. int[] a = new int[MAX_N];
  47.  
  48. Random num = new Random();
  49.  
  50. for (int i = 0; i < n; ++i)
  51. a[i] = num.Next(100);
  52.  
  53. print(n, a);
  54.  
  55. string state = "";
  56.  
  57. while (state != "stop")
  58. {
  59. state = Console.ReadLine();
  60.  
  61. string[] s = state.Split(" ");
  62.  
  63. if (s[0] == "add") add(int.Parse(s[1]), int.Parse(s[2]), ref n, ref a);
  64. else if (s[0] == "erase") erase(int.Parse(s[1]), ref n, ref a);
  65.  
  66. print(n, a);
  67. }
  68.  
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement