Exercises

Control Flow and Collections

You will be working in the csharp-web-dev-exercises repository. Each section will provide you with the project you will need.

Array Practice

Using the Arrays project in the Exercises repo complete the following:

  1. Create and initialize an array with the following values in a single line:

    1, 1, 2, 3, 5, 8
  2. Loop through the array and print out each value.

    foreach(int num in numberArray)
       {
          Console.WriteLine(num);
       }

    Remember: this is not the only way to loop through an array.

  3. Modify the loop to only print the odd numbers from the array.

String Practice

Using the Strings project in the Exercises repo complete the following:

  1. For this exercise, create a string for the value:

    I would not, could not, in a box. I would not, could not with a fox.
    I will not eat them in a house. I will not eat them with a mouse.
  2. Use the Split method to divide the string at each space and store the individual words in an array. If you need to review the method syntax, look back at the string methods table.

  3. Print the array of words to verify that your code works. The syntax is:

    Console.WriteLine(string.Join(",", arrayName));
    string sentence = "I would not, could not, in a box. I would not, could not with a fox. I will not eat them in a house. I will not eat them with a mouse.";
    
    string[] words = sentence.Split(" ");
    
    Console.WriteLine(string.Join(",", words));
  4. Repeat steps 2 and 3, but change the delimiter to split the string into separate sentences.

List Practice

Using the Lists project in the Exercises repo complete the following:

  1. Write a static method to find the sum of all the even numbers in a List.

    static int sumEven(List<int> arr)
    {
       int total = 0;
       foreach (int integer in arr)
       {
          if (integer % 2 == 0)
          {
             total += integer;
          }
       }
       return total;
    }
  2. Create a list with at least 10 integers and call your method on the list.

  3. Write a static method to print out each word in a list that has exactly 5 letters.

    static void printFiveLetterWords(List<string> wordlist)
    {
       foreach (string word in wordlist)
       {
          if (word.Length == 5)
          {
             Console.WriteLine(word);
          }
       }
    }
  4. Modify your code to prompt the user to enter the word length for the search.

       Console.WriteLine("Enter a word length: ");
       string numInput = Console.ReadLine();
       int numChars = int.Parse(numInput);
    
       // Call the method to print out list words of the chosen length:
       printXLetterWords(wordList, numChars);
    
    
       static void printXLetterWords(List<string> wordlist, int length)
       {
          foreach (string word in wordlist)
          {
             if (word.Length == length)
             {
                Console.WriteLine(word);
             }
          }
       }

Dictionary Practice

Using the Dictionaries project in the Exercises repo complete the following:

  1. Make a program similar to GradebookDictionary that does the following:

    1. It takes in student names and ID numbers (as integers) instead of names and grades.

      Console.WriteLine("Enter your students' names and ID numbers (or ENTER to finish):");
      
      Console.WriteLine("Student Name: ");
      newStudent = Console.ReadLine();
      
      if (newStudent!= "")
      {
         Console.WriteLine("ID: ");
         int newID = int.Parse(Console.ReadLine());
         students.Add(newID, newStudent);
      }

    2. The keys should be the IDs and the values should be the names.

      Console.WriteLine("\nClass roster:");
      
      foreach (KeyValuePair<int, string> student in students)
      {
         Console.WriteLine(student.Value + "'s ID: " + student.Key);
      }
      
      Console.WriteLine("Number of students in roster: " + students.Count);

      Review the Array and List Gradebooks to see how they used loops

    3. Modify the roster printing code accordingly.

Bonus Mission

Update your solution from the List Practice section above to use the string from the String Practice section. Search “C# convert string to list” online to see how to split a string into the more flexible List collection.