public static void Exercitiul19() { Console.WriteLine(" 19. How to rotate an array by a given pivot ?"); int lungime, valMax; Metoda.PrelParamInt(out lungime, out valMax); int[] arrInitial = new int[lungime]; arrInitial = Metoda.GenIntArray(lungime, valMax); Metoda.AfisareArr(arrInitial); Console.WriteLine(" Enter a value with the role of 'pivot':"); int pivot = Convert.ToInt32(Console.ReadLine()); int x = 0; for (int i = 0; i < arrInitial.Length; i++) { if (pivot == arrInitial[i]) { x = i; } } Console.WriteLine(" The index of {0} is {1}.", pivot.ToString(), x.ToString()); List <int> lstRotit = new List <int> { }; int j = 0; int k = 0; while (j < arrInitial.Length - x) { lstRotit.Add(arrInitial[j + x]); j++; } for (j = arrInitial.Length - x; j < arrInitial.Length; j++) { while (k < x) { lstRotit.Add(arrInitial[k]); k++; } } //Metoda.AfisareList(lstRotit); int[] arrRotit = new int[arrInitial.Length]; lstRotit.CopyTo(arrRotit, 0); Metoda.AfisareArr(arrRotit); }
public static void Exercitiul20() { Console.WriteLine(" 20. Sorting an Array using Selection Sort"); int lungime, valMax; Metoda.PrelParamInt(out lungime, out valMax); int[] arrNesortat = new int[lungime]; arrNesortat = Metoda.GenIntArray(lungime, valMax); Metoda.AfisareArr(arrNesortat); Metoda.SortareIntArray(arrNesortat); Metoda.AfisareArr(arrNesortat); }
public static void Exercitiul1() { Console.WriteLine(" Ex.1. Given an array of ints, write a C# method to total all the values that are even numbers."); int lungArr, valArr; Metoda.PrelParamInt(out lungArr, out valArr); int[] newArr = new int[lungArr]; newArr = Metoda.GenIntArray(lungArr, valArr); Metoda.AfisareArr(newArr); Console.WriteLine("\n"); Console.WriteLine(" The sum of the EVEN numbers is: {0}", Metoda.SumaNrPareArr(newArr).ToString()); }
public static void Exercitiul4() { Console.WriteLine(" Ex.4. Algorithm to find if Array contains duplicates?"); int lungArr, valArr; Metoda.PrelParamInt(out lungArr, out valArr); int[] newArr = new int [lungArr]; newArr = Metoda.GenIntArray(lungArr, valArr); Metoda.AfisareArr(newArr); List <int> lstUnicate = new List <int>(); lstUnicate = Metoda.EliminaDuplicate(newArr); }
public static void Exercitiul3() { Console.WriteLine(" Ex.3. How to calculate factorial using recursion in C# + iterative + time difference."); int numar = Metoda.PrelParamInt(); int[] factArr = new int[numar + 1]; factArr = Metoda.GenIntArray(numar);//.CopyTo(factArr,0); Metoda.AfisareArr(factArr); int x = Metoda.FactorialIterativ(factArr); int y = Metoda.FactorialRecursiv(numar); Console.WriteLine("\n"); Console.WriteLine(" Iterative: !{0} = {1}", numar.ToString(), x.ToString()); Console.WriteLine(" Recursive: !{0} = {1}", numar.ToString(), y.ToString()); }
public static void Exercitiul7() { Console.WriteLine(" Ex.7. Given an unsorted array which has a number in the majority"); Console.WriteLine(" (a number appears more than 50% in the array), find that number?"); int lungime, valMax; Metoda.PrelParamInt(out lungime, out valMax); int[] arr = new int[lungime]; arr = Metoda.GenIntArray(lungime, valMax); List <int> lstUnice = new List <int>(); for (int x = 0; x < arr.Length; x++) { int count = 0; if (Metoda.Exista(arr[x], lstUnice) == true)//(lstUnice.Contains(newArr[x])) { continue; } lstUnice.Add(arr[x]); for (int y = 0; y < arr.Length; y++) { if (arr[x] == arr[y]) { count++; } } if (count == 1) { Console.WriteLine(" Item {0} is unique.", arr[x].ToString()); } if (count != 1) { Console.WriteLine(" Item {0} appears {1} times.", arr[x], count.ToString()); } if (count > arr.Length / 2) { Console.WriteLine(" Item {0} is in majority!", arr[x]); } } }
public static void Exercitiul5() { Console.WriteLine(" Ex.5. How to remove duplicates from a sorted linked list?"); int lungime, valMax; Metoda.PrelParamInt(out lungime, out valMax); int[] arrayNou = new int[lungime]; arrayNou = Metoda.GenIntArray(lungime, valMax); List <int> listaUnice = new List <int>(); listaUnice = Metoda.EliminaDuplicate(arrayNou); Console.Write(" All the elements of the initial list..."); Metoda.AfisareArr(arrayNou); Metoda.AfisareList(listaUnice, " Unique elements of the list..."); }
public static void Exercitiul2() { Console.WriteLine(" Ex.2. How to find all pairs of elements in an integer array, "); Console.WriteLine(" whose sum is equal to a given number?"); int numar = Metoda.PrelParamInt(); int[] termeniAddArr = new int[numar + 1]; //termeniAddArr = Metoda.GenIntArray(numar);//array ce contine toate nr de la 0 la nr introdus de utilizator termeniAddArr = Metoda.GenIntArray(numar, numar);//array cu continut aleatoriu, functie de nr introdus de utilizator List <int> lstDupl = new List <int>(); Metoda.AfisareArr(termeniAddArr); for (int x = 0; x < termeniAddArr.Length; x++) //prima valoare din array, apoi a doua, ulterior a treia... { for (int y = 0; y < termeniAddArr.Length; y++) //comparata pe rand cu fiecare din valorile array-ului { if (Metoda.Exista(termeniAddArr[x], lstDupl) == true) //la prima rulare a buclei, aici e gol si trece peste { //incepand cu a doua, verifica daca exista in lista si sare la urmatoarea valoare continue; } else { if (numar == termeniAddArr[x] + termeniAddArr[y]) //daca indeplineste conditia { lstDupl.Add(termeniAddArr[x]); //sunt ambele adaugate in lista lstDupl.Add(termeniAddArr[y]); Console.WriteLine("\n"); Console.Write(" {0} = {1} + {2}", numar.ToString(), termeniAddArr[x].ToString(), termeniAddArr[y].ToString()); } } } } }