Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Task6
- {
- class Program
- {
- static int[] tempArray;
- static void Main(string[] args)
- {
- /*
- Сделайте 3 функции:
- Удаление элемента из массива.
- Добавление элемента в массив.
- Перенос одного массива в другой.
- */
- int[] array = new int[20];
- Random rand = new Random();
- for (int i = 0; i < array.GetLength(0); i++)
- array[i] = rand.Next(0, 100);
- Console.WriteLine("Массив элементов:\n");
- for (int i = 0; i < array.GetLength(0); i++)
- {
- Console.Write(array[i] + " | ");
- }
- Console.WriteLine("\nДействия с массивом:\ndelete - удалить элемент\nadd - добавить элемент\ntransfer - перенести в новый массив\nesc - выход\n\n");
- string command = " ";
- bool b = true;
- while (b)
- {
- Console.WriteLine("Введите команду: ");
- command = Console.ReadLine();
- switch (command.ToLower())
- {
- case "add": //Как сделать чтобы при команде эдд всегда добавлялся плюс 1 эдемент? у меня добавляется один раз, а потом заменяется последний
- Console.Write("Напишите число, которое хотите добавить - ");
- int number = Convert.ToInt32(Console.ReadLine());
- AddNumber(array, number);
- array = new int[array.Length + 1];
- break;
- case "delete": //как сделать чтобы создавался новый массив без этого элемента? у меня получается, что удаляется значение (выводит 0)
- Console.Write("число, которое хотите удалить из массива - ");
- number = Convert.ToInt32(Console.ReadLine());
- DeleteNumber(array, number);
- break;
- case "transfer":
- Console.Write("введите название нового массива - ");
- string newArray = Console.ReadLine();
- Console.WriteLine("Новый массив ");
- ChangeArray(array);
- break;
- case "esc":
- b = false;
- Console.WriteLine("Пока!\n");
- break;
- }
- }
- }
- static void AddNumber(int[] array, int newNumber)
- {
- int[] tempArray = new int [array.Length + 1];
- for (int i = 0; i < array.Length; i++)
- {
- tempArray[i] = array[i];
- if (i == array.Length - 1)
- {
- tempArray[i + 1] = newNumber;
- }
- }
- for(int i = 0; i < tempArray.Length; i++)
- {
- Console.Write(tempArray[i] + " | ");
- //return tempArray[i];
- }
- Console.WriteLine();
- }
- static void DeleteNumber (int[] array, int deletedNumber)
- {
- int[] tempArray = new int[array.Length];
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] == deletedNumber)
- {
- i+=1;
- }
- tempArray[i] = array[i];
- }
- for (int i = 0; i < tempArray.Length; i++)
- {
- Console.Write(tempArray[i] + " | ");
- }
- Console.WriteLine();
- }
- static void ChangeArray (int[] array)
- {
- for (int i = 0; i < array.Length; i++)
- {
- Random rand = new Random();
- int j1 = rand.Next(0, array.Length);
- int j2 = rand.Next(0, array.Length);
- while (j1 == j2)
- {
- j1 = rand.Next(0, array.Length);
- j2 = rand.Next(0, array.Length);
- }
- int temp = array[j1];
- array[j1] = array[j2];
- array[j2] = temp;
- }
- for (int i = 0; i < array.Length; i++)
- {
- Console.Write(array[i] + " | ");
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement