Opdracht 1 Initialisatie en gebruik van een Dictionary
Maak een lege Dictionary voor de namen van studenten en hun bijbehorende cijfers.
Specificaties
Maak een nieuwe Dictionary van string (sleutel) en int (waarde).
Voeg de namen “John”, “Alice” en “Bob” toe, met de cijfers 8, 7, en 9 respectievelijk.
Verwachte output
John: 8
Alice: 7
Bob: 9
Nu jij
using System; using System.Collections.Generic; Dictionary<string, int> studentCijfers = new ________; // Voeg studenten en cijfers toe aan de Dictionary studentCijfers.Add("John", 8); studentCijfers.Add("Alice", 7); studentCijfers.Add("Bob", 9); // Toon de Dictionary foreach (var student in studentCijfers) { Console.WriteLine($"{student.Key}: {student.Value}"); }
- Mogelijke uitwerking
using System; using System.Collections.Generic; Dictionary<string, int> studentCijfers = new Dictionary<string, int>(); // Voeg studenten en cijfers toe aan de Dictionary studentCijfers.Add("John", 8); studentCijfers.Add("Alice", 7); studentCijfers.Add("Bob", 9); // Toon de Dictionary foreach (var student in studentCijfers) { Console.WriteLine($"{student.Key}: {student.Value}"); }
Opdracht 2 Waarde ophalen uit de Dictionary
Haal het cijfer van een specifieke student uit de Dictionary.
Specificaties
Voeg drie studenten en hun cijfers toe aan de Dictionary.
Haal het cijfer op van de student “Alice” en toon dit.
Verwachte output
Cijfer van Alice: 7
Nu jij
using System; using System.Collections.Generic; Dictionary<string, int> studentCijfers = new Dictionary<string, int>(); // Voeg studenten en cijfers toe aan de Dictionary studentCijfers.Add("John", 8); studentCijfers.Add("Alice", 7); studentCijfers.Add("Bob", 9); // Haal en toon het cijfer van Alice int cijferAlice = _________ Console.WriteLine($"Cijfer van Alice: {cijferAlice}");
- Mogelijke uitwerking
using System; using System.Collections.Generic; Dictionary<string, int> studentCijfers = new Dictionary<string, int>(); // Voeg studenten en cijfers toe aan de Dictionary studentCijfers.Add("John", 8); studentCijfers.Add("Alice", 7); studentCijfers.Add("Bob", 9); // Haal en toon het cijfer van Alice int cijferAlice = studentCijfers["Alice"]; Console.WriteLine($"Cijfer van Alice: {cijferAlice}");
Opdracht 3 Controleer of een sleutel bestaat in de Dictionary
Controleer of een bepaalde student in de Dictionary bestaat en toon een bericht.
Specificaties
Voeg drie studenten en hun cijfers toe aan de Dictionary.
Controleer of de student “Charlie” bestaat in de Dictionary en toon een bericht.
Verwachte output
Student Charlie bestaat niet in de Dictionary.
Nu jij
using System; using System.Collections.Generic; Dictionary<string, int> studentCijfers = new Dictionary<string, int>(); // Voeg studenten en cijfers toe aan de Dictionary studentCijfers.Add("John", 8); studentCijfers.Add("Alice", 7); studentCijfers.Add("Bob", 9); // Controleer of Charlie bestaat in de Dictionary if (studentCijfers._________) { Console.WriteLine("Student Charlie bestaat in de Dictionary."); } else{ Console.WriteLine("Student Charlie bestaat niet in de Dictionary."); }
- Mogelijke uitwerking
using System; using System.Collections.Generic; Dictionary<string, int> studentCijfers = new Dictionary<string, int>(); // Voeg studenten en cijfers toe aan de Dictionary studentCijfers.Add("John", 8); studentCijfers.Add("Alice", 7); studentCijfers.Add("Bob", 9); // Controleer of Charlie bestaat in de Dictionary if (studentCijfers.ContainsKey("Charlie")) { Console.WriteLine("Student Charlie bestaat in de Dictionary."); } else{ Console.WriteLine("Student Charlie bestaat niet in de Dictionary."); }
Opdracht 4 Verwijder een item uit de Dictionary
Verwijder een student uit de Dictionary en toon de gewijzigde Dictionary.
Specificaties
Voeg drie studenten en hun cijfers toe aan de Dictionary.
Verwijder de student “Bob” en toon de Dictionary zonder “Bob”.
Verwachte output
John: 8
Alice: 7
Nu jij
using System; using System.Collections.Generic; Dictionary<string, int> studentCijfers = new Dictionary<string, int>(); // Voeg studenten en cijfers toe aan de Dictionary studentCijfers.Add("John", 8); studentCijfers.Add("Alice", 7); studentCijfers.Add("Bob", 9); // Verwijder Bob uit de Dictionary studentCijfers._________// Toon de Dictionary foreach (var student in studentCijfers) { Console.WriteLine($"{student.Key}: {student.Value}"); }
- Mogelijke uitwerking
using System; using System.Collections.Generic; Dictionary<string, int> studentCijfers = new Dictionary<string, int>(); // Voeg studenten en cijfers toe aan de Dictionary studentCijfers.Add("John", 8); studentCijfers.Add("Alice", 7); studentCijfers.Add("Bob", 9); // Verwijder Bob uit de Dictionary studentCijfers.Remove("Bob"); // Toon de Dictionary foreach (var student in studentCijfers) { Console.WriteLine($"{student.Key}: {student.Value}"); }
Opdracht 5 Itereren door een Dictionary
Gebruik een foreach-lus om alle items in de Dictionary weer te geven.
Specificaties
Voeg vijf landen en hun bijbehorende bevolkingsaantallen toe aan de Dictionary.
Toon alle landen en hun bevolkingsaantallen met een foreach-lus.
using System; using System.Collections.Generic; Dictionary<string, long> landenBevolking = new Dictionary<string, long>(); // Voeg landen en bevolkingsaantallen toe aan de Dictionary landenBevolking.Add("USA", 331000000); landenBevolking.Add("China", 1444216107); landenBevolking.Add("India", 1393409038); landenBevolking.Add("Brazil", 213993437); landenBevolking.Add("Nigeria", 211400708); // Toon de landen en hun bevolkingsaantallen _________
- Mogelijke uitwerking
using System; using System.Collections.Generic; Dictionary<string, long> landenBevolking = new Dictionary<string, long>(); // Voeg landen en bevolkingsaantallen toe aan de Dictionary landenBevolking.Add("USA", 331000000); landenBevolking.Add("China", 1444216107); landenBevolking.Add("India", 1393409038); landenBevolking.Add("Brazil", 213993437); landenBevolking.Add("Nigeria", 211400708); // Toon de landen en hun bevolkingsaantallen foreach (var land in landenBevolking) { Console.WriteLine($"{land.Key}: {land.Value}"); }
Opdracht 6 Woordentelling met Dictionary
Tel hoe vaak elk woord voorkomt in een zin met behulp van een Dictionary.
Specificaties
Vraag de gebruiker om een zin in te voeren.
Splits de zin in woorden en gebruik een Dictionary om het aantal keren dat elk woord voorkomt bij te houden.
Toon het resultaat in de vorm woord: aantal.
Verwachte output
input: Dit is een zin met twee keer het woord is erin
output:
Dit: 1
is: 2
een: 1
zin: 1
met: 1
twee: 1
keer: 1
het: 1
woord: 1
erin: 1
Nu jij
using System;using System.Collections.Generic;string input = "________";string[] words = input.Split(' ');Dictionary<string, int> wordCount = new Dictionary<string, int>();// Tel de woordenforeach (string word in words){ if (wordCount._________) { _________ } else { _________ }}// Toon het resultaatforeach (KeyValuePair<string, int> entry in wordCount){ Console.WriteLine($"{entry.Key}: {entry.Value}");}
Mogelijke uitwerking
using System;using System.Collections.Generic;string input = "Dit is een zin met twee keer het woord is erin";string[] words = input.Split(' ');Dictionary<string, int> wordCount = new Dictionary<string, int>();// Tel de woordenforeach (string word in words){ if (wordCount.ContainsKey(word)) { wordCount[word] += 1; } else { wordCount.Add(word, 1); }}// Toon het resultaatforeach (KeyValuePair<string, int> entry in wordCount){ Console.WriteLine($"{entry.Key}: {entry.Value}");}