Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Der er brugt Metoder og i opgaven, så shortcuts > kompleksitet*/
- /*
- 1. Sum of Elements in a List
- · Task: Write a program that takes a list of integers
- and calculates the sum of all elementsusing a for loop.
- */
- void Opgave1()
- {
- List<int> listeInt = new List<int>() { 1, 2, 3 };
- int værdi = 0;
- for (int i = 0; i < listeInt.Count; i++)
- {
- Console.WriteLine(listeInt[i]);
- // alternativ: værdi = værdi + listeInt[i];
- værdi += listeInt[i];
- }
- Console.WriteLine("Samlede værdi er " + værdi);
- }
- /*
- 2. Find the Maximum and Minimum Element in a List
- · Task: Write a program that takes a list of integers and
- finds the maximum and minimum values using a loop and if-else selection statements.
- den nemme metode: {
- List<int> listeInt = new List<int>() { 1, 2, 3 };
- Console.WriteLine("Største værdi: " + listeInt.Max() + " og mindste værdi: " + listeInt.Min());
- } */
- void Opgave2()
- {
- List<int> listeInt = new List<int>() { 11, 2, 3, 9 };
- int maxVærdi = listeInt[0];
- int minVærdi = listeInt[0];
- foreach (int i in listeInt)
- {
- if (i > maxVærdi)
- {
- maxVærdi = i;
- }
- if (i < minVærdi)
- {
- minVærdi = i;
- }
- }
- Console.WriteLine("Maksværdi er: " + maxVærdi + "\nMinværdi er: " + minVærdi);
- }
- /*
- 3. Find the Average of List Elements
- · Task: Write a program that calculates the average value of elements in a list.
- Use a loop to sum the elements and then divide by the count of elements.
- */
- void Opgave3()
- {
- int sum = 0;
- List<int> listeInt = new List<int>() { 1, 2, 3 };
- foreach (int i in listeInt)
- {
- sum += i;
- }
- // Sikrer korrekt beregning af gennemsnit (heltal til heltal division)
- Console.WriteLine("Gennemsnitsværdi er: " + (double)sum / listeInt.Count);
- }
- /*
- 4. Check if a List Contains a Specific Element
- · Task: Write a program that asks the user for an integer input and checks if the
- contains that element. Use a foreach loop and an if statement.
- (brugerListe.Contains(brugerInput)) */
- void Opgave4()
- {
- List<int> brugerListe = new List<int>() { 1, 2, 33, 4, 4, 5, 3, 3, 4, 2, 23, 42, 34234235 };
- Console.WriteLine("Skriv en liste af værdier herunder, vi tjekker om de er i listen:\n");
- int brugerInput = Convert.ToInt32(Console.ReadLine());
- foreach (int i in brugerListe)
- {
- if (brugerInput == i)
- {
- Console.WriteLine("Denne værdi er i listen: " + i);
- break;
- }
- }
- }
- /*
- 5. Copy an Array into a List
- · Task: Write a program that takes a one-dimensional array and copies all elements into a list.
- Use a loop to iterate through the array and add each element to the list.
- */
- void Opgave5()
- {
- string[] oneDimensionalArray = new string[] { "2", "3", "4", "6" };
- List<string> strings = new List<string>();
- foreach (string værdi in oneDimensionalArray)
- {
- strings.Add(værdi);
- }
- Console.WriteLine("Her er array->listen: " + string.Join(", ", strings));
- }
- /*
- 6. Count Odd and Even Numbers in an Array
- · Task: Write a program that counts how many odd and even numbers are in an array.
- Use a loop and selection statements (if checks for odd or even) to count the occurrences.
- */
- void Opgave6()
- {
- int lige = 0;
- int ulige = 0;
- int[] liste = { 1, 2, 3, 4, 5, 56, 8, 76, 5, 4, 32, 2, 4, 5, 7, 87, 65, 4, 2 };
- foreach (int i in liste)
- {
- if (i % 2 == 0)
- {
- Console.WriteLine("Tallet er lige: " + i);
- lige++;
- }
- else
- {
- Console.WriteLine("Tallet er ulige: " + i);
- ulige++;
- }
- }
- Console.WriteLine("Antal lige: " + lige + ", Antal ulige: " + ulige);
- }
- /*
- 7. Reverse the Elements of a List
- · Task: Write a program that reverses the order of elements in a list using a loop.
- You can either modify the list in place or create a new list to store the reversed elements.
- */
- void Opgave7()
- {
- List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- ints.Reverse();
- Console.WriteLine("Reversed liste: " + string.Join(", ", ints));
- }
- /*
- 8. Sort a List Using Selection Sort
- · Task: Implement the selection sort algorithm using loops to sort a list of integers in ascending order.
- */
- void Opgave8()
- {
- List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- ints.Sort();
- Console.WriteLine("Sorteret liste: " + string.Join(", ", ints));
- }
- /*
- 9. Remove a Specific Element from a List
- · Task: Write a program that asks the user for an integer input and removes all occurrences
- of that element from a list using a loop and an if condition.
- */
- Opgave9()
- {
- List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- Console.WriteLine("Skriv inputs du vil slette, gå ud af programmet med (x):\n")
- inputRemoval = Console.ReadLine();
- while (inputRemoval != "x")
- {ints.Remove(inputRemoval)}
- }
- /*
- 10. Merge Two Lists
- · Task: Write a program that takes two lists and merges them into a third list.
- Use loops to append elements from both lists into the new one.
- */
- void Opgave10()
- {
- List<int> ints1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- List<int> ints2 = new List<int>() { 1, 2, 9, 7, 5, 6, 7, 8, 9, 3, 2, 1, 6 };
- List<int> thirdList = new List<int>(ints1);
- thirdList.AddRange(ints2);
- Console.WriteLine("Sammensat liste: " + string.Join(", ", thirdList));
- }
- /*
- 11. Shift Elements of an Array
- · Task: Write a program that shifts the elements of a one-dimensional array to the left by one position.
- The first element should move to the end of the array.
- */
- List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- foreach (int i in ints)
- {
- ints[i] = ints[(ints.Length)-1];
- }
- /*
- 12. Find the Second Largest Element in a List
- · Task: Write a program that finds the second largest element in a list of integers.
- Avoid sorting the list directly; instead,
- iterate through the list to find the two largest distinct elements.
- */
- Opgave12()
- {
- List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- ints.Sort();
- andenStørste = ints[(ints.Length) - 1]
- Console.WriteLine(andenStørste);
- }
- Opgave12();
- /*
- 13. Find Duplicates in a List
- · Task: Write a program that checks for duplicate elements in a list and prints them.
- Use nested loops to compare each element with others and if statements to detect duplicates.
- */
- void Opgave13()
- {
- List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 5, 7 };
- HashSet<int> set = new HashSet<int>();
- List<int> duplicates = new List<int>();
- foreach (int tal in ints)
- {
- if (!set.Add(tal))
- {
- duplicates.Add(tal);
- }
- }
- Console.WriteLine("Duplicate værdier: " + string.Join(", ", duplicates));
- }
- /*
- 14. Count Positive and Negative Numbers in a List
- · Task: Write a program that counts how many positive and negative numbers are in a list.
- Use a foreach loop and if-else statements to keep track of the counts.
- */
- Opgave14()
- {List<int> ints = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- negativeNumre = 0;
- positiveNumre = 0;
- foreach (int i in ints)
- {
- if (i < 0)
- {
- negativeNumre++;
- }
- else
- {
- positiveNumre++;
- }
- }
- Console.WriteLine($"Positive numre: {positiveNumre}, og negative numre: {negativeNumre}");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement