Exercise: Arrays and Loops
Solve 17 problems with arrays and loops (filling, searching, totaling, finding the highest and lowest, reversing) and check each with tests.
Apprentice · C# · Updated
Almost everything a program does with a group of values is one of a few patterns: fill it, add it up, find something in it, find the biggest or smallest, or turn it around. In this exercise you write each of them yourself, 17 methods in all, and tests check every one.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-arrays-and-loops, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your bootcamp folder. Or clone it with Git.
The ArraysExercise project has four classes of methods to write:
| File | Methods |
|---|---|
Calendar.cs |
DaysOfTheWeek, MonthsOfTheYear, FirstDayOfWeek, LastDayOfWeek, MonthName |
NumberLists.cs |
OneToFifty, EvenNumbers, SumNumbers, DoubleEvens, SumEveryThird, SumFirstTwoNumbers |
Names.cs |
HasName, CountName, ReverseNames |
Prices.cs |
AveragePrice, HighestPrice, LowestPrice |
ArraysExercise.Tests holds the tests; you don't need to change it. Open ArraysExercise.slnx in Visual Studio, or the folder in VS Code.
What to do
Each method returns an empty array, 0, "" or false for now, and the comment above it says what it should return, with examples. A method that returns an array can return one written with square brackets:
public static int[] FirstThreeOdds()
{
return [1, 3, 5];
}
Replace each return line with your own code. Work through the files in order: the first problems are short, and they get harder.
Check your work
In Visual Studio, choose Test > Run All Tests. Or, in Git Bash, from the folder with the .slnx file:
dotnet test
Each method you finish turns its tests green. A failing test's message says what it expected, such as [1, 2, 3, 4] adds up to 10.
Hints
- Index or element? If you need the position (
OneToFifty,SumEveryThird,ReverseNames), usefor. If you need only each value (SumNumbers,HasName),foreachis simpler. - Off by one. Indexes run from
0toLength - 1. Month number 1 is at index 0. - Totals start at
0; add each element. Highest starts at the first element; replace it when you find a bigger one. - Searching: return
trueas soon as you find the name. Returnfalseonly after the loop has looked at every name. - Capital letters:
"Kent".ToLower()is"kent". Compare two names in lowercase and capitals stop mattering. You'll learn more about strings in the next chapter. - A new array:
ReverseNamesmust return a new array,new string[names.Length], and leave the one it was given unchanged.
When you're done
Every test is green. Look back at your code: did you use the loop that fits each problem best? Is any variable named x or temp that could say what it holds?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-arrays-and-loops), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. If you downloaded the ZIP:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-arrays-and-loops.git git push -u origin mainIf you cloned the starter instead, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-arrays-and-loops.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-arrays-and-loops, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
Fill, total, search, count, find the extreme, reverse: these patterns come back in every program that handles data, from shopping carts to sports scores.