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
-
You should not edit any files that have the comment
DO NOT MODIFY THIS FILE
at the top of them. Any edits you make to these files will be removed before grading. - Running all tests for this assignment: Click on Run and Debug in the sidebar, then select this week's code assignment from the drop-down at the top. Click the green Start Debugging button next to the drop-down. The integrated terminal will show the files being compiled and then the tests being run. It will print a summary of which tests passed and failed.
-
Running individual tests: Navigate to the test file ending in
_Tests.cs
in the week's code directory. Visual Studio Code will show inline buttons to Run Test for each test method, or to Run All Tests for each test class. -
Debugging individual tests: Navigate to the test file ending in
_Tests.cs
in the week's code directory. Visual Studio Code will show inline buttons to Debug Test for each test method, or to Debug All Tests for each test class.- If you put a breakpoint inside your code or inside the test, the only way to hit it will be by clicking Debug Test or Debug All Tests in this file.
-
If you want to debug by printing output to the terminal, you must use
Debug.WriteLine()
to do so. Using that may require you to addusing System.Diagnostics;
to the top of the file. UsingConsole.WriteLine()
, as is done in other parts of the template project, will not correctly print output to the console when a test is running. Any output generated withDebug.WriteLine()
will appear in the Debug Console portion of the integrated terminal when clicking Debug Test or Debug All Tests in this file.
All of the code files for this assignment will be found in the week01/code
folder in the course Git repository.
Part 1: Arrays
- Open up the following code:
Arrays.cs
-
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}
. - Using comments in your program, write down your process for solving this problem by step before you write the code.
- 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
-
Come up with a plan on how to implement the
RotateListRight
function. This function receives a list ofdata
and anamount
to rotate to the right.For example, if the
data
is<List>{1, 2, 3, 4, 5, 6, 7, 8, 9}
and anamount
is 5 then the list after the function runs should be<List>{5, 6, 7, 8, 9, 1, 2, 3, 4}
. If thedata
is<List>{1, 2, 3, 4, 5, 6, 7, 8, 9}
and anamount
is 3 then the list after the function runs should be<List>{7, 8, 9, 1, 2, 3, 4, 5, 6}
. The value ofamount
will be in the range of 1 anddata.Count
, inclusive. - Using comments in your program, write down your process for solving this problem by step before you write the code.
GetRange(index, count)
RemoveRange(index, count)
AddRange(list or array)
InsertRange(index, list or array)
Add(value)
Insert(index, value)
RemoveAt(index)
- Implement the
RotateListRight
function. (Make sure to leave your comments in the code, even after you have implemented it.)
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:
See the documentation for a C# List to see all the methods available.
Submission
When you have completed the assignment:
- Make sure all of your changes are committed and pushed to your course GitHub repository.
- Return to I-Learn to submit a link to your repository.