Advertisement
augu

Untitled

Oct 1st, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.57 KB | None | 0 0
  1. /*Der er brugt Metoder og  i opgaven, så shortcuts > kompleksitet*/
  2.  
  3.  
  4.  
  5. /*
  6.  1. Sum of Elements in a List
  7.  
  8. · Task: Write a program that takes a list of integers
  9. and calculates the sum of all elementsusing a for loop.
  10.  
  11. */
  12.  
  13. void Opgave1()
  14. {
  15.     List<int> listeInt = new List<int>() { 1, 2, 3 };
  16.  
  17.     int værdi = 0;
  18.     for (int i = 0; i < listeInt.Count; i++)
  19.     {
  20.         Console.WriteLine(listeInt[i]);
  21.         // alternativ: værdi = værdi + listeInt[i];
  22.         værdi += listeInt[i];
  23.     }
  24.     Console.WriteLine("Samlede værdi er " + værdi);
  25. }
  26.  
  27. /*
  28.  
  29. 2. Find the Maximum and Minimum Element in a List
  30.  
  31. · Task: Write a program that takes a list of integers and
  32. finds the maximum and minimum values using a loop and if-else selection statements.
  33.  
  34. den nemme metode: {
  35.     List<int> listeInt = new List<int>() { 1, 2, 3 };
  36.     Console.WriteLine("Største værdi: " + listeInt.Max() + " og mindste værdi: " + listeInt.Min());
  37. } */
  38.  
  39. void Opgave2()
  40. {
  41.     List<int> listeInt = new List<int>() { 11, 2, 3, 9 };
  42.     int maxVærdi = listeInt[0];
  43.     int minVærdi = listeInt[0];
  44.  
  45.     foreach (int i in listeInt)
  46.     {
  47.         if (i > maxVærdi)
  48.         {
  49.             maxVærdi = i;
  50.         }
  51.  
  52.         if (i < minVærdi)
  53.         {
  54.             minVærdi = i;
  55.         }
  56.     }
  57.     Console.WriteLine("Maksværdi er: " + maxVærdi + "\nMinværdi er: " + minVærdi);
  58. }
  59.  
  60. /*
  61. 3. Find the Average of List Elements
  62.  
  63. · Task: Write a program that calculates the average value of elements in a list.
  64. Use a loop to sum the elements and then divide by the count of elements.
  65. */
  66.  
  67. void Opgave3()
  68. {
  69.     int sum = 0;
  70.     List<int> listeInt = new List<int>() { 1, 2, 3 };
  71.  
  72.     foreach (int i in listeInt)
  73.     {
  74.         sum += i;
  75.     }
  76.     // Sikrer korrekt beregning af gennemsnit (heltal til heltal division)
  77.     Console.WriteLine("Gennemsnitsværdi er: " + (double)sum / listeInt.Count);
  78. }
  79.  
  80. /*
  81. 4. Check if a List Contains a Specific Element
  82.  
  83. · Task: Write a program that asks the user for an integer input and checks if the
  84. contains that element. Use a foreach loop and an if statement.
  85.  
  86.         (brugerListe.Contains(brugerInput)) */
  87.  
  88. void Opgave4()
  89. {
  90.     List<int> brugerListe = new List<int>() { 1, 2, 33, 4, 4, 5, 3, 3, 4, 2, 23, 42, 34234235 };
  91.  
  92.     Console.WriteLine("Skriv en liste af værdier herunder, vi tjekker om de er i listen:\n");
  93.  
  94.     int brugerInput = Convert.ToInt32(Console.ReadLine());
  95.  
  96.     foreach (int i in brugerListe)
  97.     {
  98.         if (brugerInput == i)
  99.         {
  100.             Console.WriteLine("Denne værdi er i listen: " + i);
  101.             break;
  102.         }
  103.     }
  104.    
  105. }
  106.  
  107.  
  108. /*
  109. 5. Copy an Array into a List
  110.  
  111. · Task: Write a program that takes a one-dimensional array and copies all elements into a list.
  112. Use a loop to iterate through the array and add each element to the list.
  113. */
  114.  
  115. void Opgave5()
  116. {
  117.     string[] oneDimensionalArray = new string[] { "2", "3", "4", "6" };
  118.  
  119.     List<string> strings = new List<string>();
  120.  
  121.     foreach (string værdi in oneDimensionalArray)
  122.     {
  123.         strings.Add(værdi);
  124.     }
  125.     Console.WriteLine("Her er array->listen: " + string.Join(", ", strings));
  126. }
  127.  
  128. /*
  129. 6. Count Odd and Even Numbers in an Array
  130.  
  131. · Task: Write a program that counts how many odd and even numbers are in an array.
  132. Use a loop and selection statements (if checks for odd or even) to count the occurrences.
  133.  
  134. */
  135.  
  136. void Opgave6()
  137. {
  138.     int lige = 0;
  139.     int ulige = 0;
  140.  
  141.     int[] liste = { 1, 2, 3, 4, 5, 56, 8, 76, 5, 4, 32, 2, 4, 5, 7, 87, 65, 4, 2 };
  142.  
  143.     foreach (int i in liste)
  144.     {
  145.         if (i % 2 == 0)
  146.         {
  147.             Console.WriteLine("Tallet er lige: " + i);
  148.             lige++;
  149.         }
  150.         else
  151.         {
  152.             Console.WriteLine("Tallet er ulige: " + i);
  153.             ulige++;
  154.         }
  155.     }
  156.     Console.WriteLine("Antal lige: " + lige + ", Antal ulige: " + ulige);
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /*
  163. 7. Reverse the Elements of a List
  164.  
  165. · Task: Write a program that reverses the order of elements in a list using a loop.
  166. You can either modify the list in place or create a new list to store the reversed elements.
  167. */
  168. void Opgave7()
  169. {
  170.     List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  171.     ints.Reverse();
  172.     Console.WriteLine("Reversed liste: " + string.Join(", ", ints));
  173. }
  174.  
  175. /*
  176. 8. Sort a List Using Selection Sort
  177.  
  178. · Task: Implement the selection sort algorithm using loops to sort a list of integers in ascending order.
  179.  
  180. */
  181. void Opgave8()
  182. {
  183.     List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  184.     ints.Sort();
  185.     Console.WriteLine("Sorteret liste: " + string.Join(", ", ints));
  186. }
  187.  
  188. /*
  189. 9. Remove a Specific Element from a List
  190.  
  191. · Task: Write a program that asks the user for an integer input and removes all occurrences
  192. of that element from a list using a loop and an if condition.
  193. */
  194.  
  195. Opgave9()
  196.     {
  197.     List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  198.     Console.WriteLine("Skriv inputs du vil slette, gå ud af programmet med (x):\n")
  199.     inputRemoval = Console.ReadLine();
  200.     while (inputRemoval != "x")
  201.     {ints.Remove(inputRemoval)}
  202. }
  203.  
  204.  
  205. /*
  206. 10. Merge Two Lists
  207.  
  208. · Task: Write a program that takes two lists and merges them into a third list.
  209. Use loops to append elements from both lists into the new one.
  210. */
  211.  
  212. void Opgave10()
  213. {
  214.     List<int> ints1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  215.     List<int> ints2 = new List<int>() { 1, 2, 9, 7, 5, 6, 7, 8, 9, 3, 2, 1, 6 };
  216.  
  217.     List<int> thirdList = new List<int>(ints1);
  218.     thirdList.AddRange(ints2);
  219.  
  220.     Console.WriteLine("Sammensat liste: " + string.Join(", ", thirdList));
  221. }
  222.  
  223.  
  224. /*
  225. 11. Shift Elements of an Array
  226.  
  227. · Task: Write a program that shifts the elements of a one-dimensional array to the left by one position.
  228. The first element should move to the end of the array.
  229. */
  230.  
  231. List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  232. foreach (int i in ints)
  233. {
  234.     ints[i] = ints[(ints.Length)-1];
  235. }
  236.  
  237. /*
  238. 12. Find the Second Largest Element in a List
  239.  
  240. · Task: Write a program that finds the second largest element in a list of integers.
  241. Avoid sorting the list directly; instead,
  242. iterate through the list to find the two largest distinct elements.
  243. */
  244.  
  245. Opgave12()
  246. {
  247.     List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  248.     ints.Sort();
  249.     andenStørste = ints[(ints.Length) - 1]
  250.     Console.WriteLine(andenStørste);
  251. }
  252. Opgave12();
  253.  
  254. /*
  255. 13. Find Duplicates in a List
  256.  
  257. · Task: Write a program that checks for duplicate elements in a list and prints them.
  258. Use nested loops to compare each element with others and if statements to detect duplicates.
  259. */
  260. void Opgave13()
  261. {
  262.     List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 5, 7 };
  263.     HashSet<int> set = new HashSet<int>();
  264.     List<int> duplicates = new List<int>();
  265.  
  266.     foreach (int tal in ints)
  267.     {
  268.         if (!set.Add(tal))
  269.         {
  270.             duplicates.Add(tal);
  271.         }
  272.     }
  273.  
  274.     Console.WriteLine("Duplicate værdier: " + string.Join(", ", duplicates));
  275. }
  276.  
  277.  
  278. /*
  279. 14. Count Positive and Negative Numbers in a List
  280.  
  281. · Task: Write a program that counts how many positive and negative numbers are in a list.
  282. Use a foreach loop and if-else statements to keep track of the counts.
  283. */
  284.  
  285. Opgave14()
  286. {List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  287. negativeNumre = 0;
  288. positiveNumre = 0;
  289.  
  290. foreach (int i in ints)
  291. {
  292.     if (i < 0)
  293.     {
  294.         negativeNumre++;
  295.     }
  296.     else
  297.     {
  298.         positiveNumre++;
  299.     }
  300. }
  301.     Console.WriteLine($"Positive numre: {positiveNumre}, og negative numre: {negativeNumre}");
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement