public static int[] GenIntArray(int lungime, int valoare) { int[] array1 = new int[lungime]; int i = 1; while (i <= lungime) { int x = Functie.NrAleatorii(valoare); array1[i - 1] = x; //Console.WriteLine(" pos. {0} -- val. {1}", i.ToString(), x.ToString()); i++; } return(array1); }
public static void Mod2Ex5() { Console.WriteLine(" We need to create an array in order to solve this exercise. OK? y/n"); string raspuns = Console.ReadLine(); if (raspuns == "n") { Console.WriteLine(" Ok. Bye!"); } if (raspuns == "y") { Console.WriteLine(" How many elements do you want this array to have?"); int lungUser = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(" Enter the max value the elements should have:"); int valMaxUser = Convert.ToInt32(Console.ReadLine()); int[] Array1 = new int[lungUser]; Functie.GenIntArray(lungUser, valMaxUser).CopyTo(Array1, 0); Procedura.AfisareIntArr(Array1, " Array no. 1 elements: "); Console.WriteLine(" Enter the index of the value you want to add:"); int newIdx = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(" Enter the new value you want to add:"); int newVal = Convert.ToInt32(Console.ReadLine()); int[] Array2 = new int[Array1.Length + 1]; for (int i = 0; i <= Array1.Length - 1; i++) { if (i < newIdx) { Array2[i] = Array1[i]; } if (i == newIdx) { Array2[i] = newVal; Array2[i + 1] = Array1[i]; } if (i > newIdx) { Array2[i + 1] = Array1[i]; } } Procedura.AfisareIntArr(Array2, " Array no. 2 elements: "); } Procedura.MeniuPrincipal(); }