CSE 212: Programming with Data Structures

W01 Code: Dynamic Arrays

This assignment must be completed individually to ensure you are meeting all course outcomes. You should not complete this assignment within a group. If you obtain help from a tutor, the tutor should help you understand principles but should not help you answer these problems. It is an honor code violation to obtain answers for these problems from others including using the internet (i.e. sites that allow students to share their solutions).

Running and Debugging

All of the code files for this assignment will be found in the week01/code folder in the course Git repository.

Part 1: Arrays

  1. Open up the following code: Arrays.cs
  2. Come up with a plan on how to implement the MultiplesOf function. The function should create and return an array of multiples of a number. The starting number and the number of multiples are provided as inputs to the function. For example, MultiplesOf(3,5), where the 3 is the starting number and 5 is the number of multiples, would result in <double>{3, 6, 9, 12, 15}.
  3. Using comments in your program, write down your process for solving this problem by step before you write the code.

  4. Implement the MultiplesOf function. (Make sure to leave your comments in the code, even after you have implemented it.)

Part 2: Solving a Complicated Problem Using a List

  1. Come up with a plan on how to implement the RotateListRight function. This function receives a list of data and an amount to rotate to the right.

    For example, if the data is <List>{1, 2, 3, 4, 5, 6, 7, 8, 9} and an amount is 5 then the list after the function runs should be <List>{5, 6, 7, 8, 9, 1, 2, 3, 4}. If the data is <List>{1, 2, 3, 4, 5, 6, 7, 8, 9} and an amount is 3 then the list after the function runs should be <List>{7, 8, 9, 1, 2, 3, 4, 5, 6}. The value of amount will be in the range of 1 and data.Count, inclusive.

  2. Using comments in your program, write down your process for solving this problem by step before you write the code.
  3. Hint: There are multiple ways to solve this problem. Don't worry about trying to find the most elegant solution this week. Your solution could use modulo (%) to deal with wrapping around the index number back to 0.

    Your solution alternatively could use a technique called list slicing. While you can use the syntax array[3..] for arrays, List in C# uses methods that include the word __Range to get the slice. For example, if you had a list data = <List>{1,2,3,4,5} then data.GetRange(0, 3) will give you a new list <List>{1,2,3} which goes from the beginning of the list (index 0) up for 3 values. If you wrote the slice as data.GetRange(3, data.Count - 3) then you would get the list <List>{4,5} which is index 3 to the end.

    Other methods you might find helpful are:

    • GetRange(index, count)
    • RemoveRange(index, count)
    • AddRange(list or array)
    • InsertRange(index, list or array)
    • Add(value)
    • Insert(index, value)
    • RemoveAt(index)

    See the documentation for a C# List to see all the methods available.

  4. Implement the RotateListRight function. (Make sure to leave your comments in the code, even after you have implemented it.)

Submission

When you have completed the assignment: