This is the solution for the challenge titled ChallengeForXmenBattleCount, dealing with arrays and using the for loop. This challenge isn't very complicated, but the details are tricky. Read on as much as you need in order to solve the problem facing you, then finish as much as you can on your own.
In the CS-ASP_026-Challenge folder, navigate to the ChallengeForXmenBattleCount folder, then copy and paste it into your Visual Studio Projects directory.
Once this is done, open it and go to the Default.aspx.cs page. In the Page_Load, you'll find two starting arrays called names and numbers, as well as an empty string result. As described in the challenge document, the numbers and names in the arrays correspond to each other. So index 1 in both arrays would be "Iceman" in the names and 9 in the numbers array, meaning Iceman has fought 9 battles:
Our task is to loop through these arrays and locate the X-Man with the most battles, and the X-Man with the least battles. In order to do this, we'll create a for loop like so:
Remember that you can either manually type this code, or you can create a "code snippet". To do this, type 'for' and press the Tab key twice to create a template that you can then edit.
This loop will continue to increment i until it it reaches the end of the names array, meaning that we will be able to evaluate every entry in it.
In order to keep track of the X-Men with the most and fewest battles, we'll create a few variables outside the scope of the loop:
The purpose of these variables will be to hold the value of the index that has the smallest and largest number, respectively. To do this, we'll create two if() statements within the loop:
These statements check to see if the value of numbers[i] (whatever index it is currently at) is smaller than numbers[smallestNumberIndex] (the current minimum value). If it is, then it replaces the current value of smallestNumberIndex with the value of i. This will continue to loop through every value of numbers, and will set the smallestNumberIndex variable to the index value with the minimum integer value; in this case numbers. And, because the names and numbers are directly related, the same index will satisfy the requirement for the names index. The reverse is also true of the latter statement.
This method for evaluating the array is used because it logically makes the most sense to explain. If the current value is less than the previous lowest value, replace that value with the current value. However, you could also code it to evaluate if the current index is the minimum (or maximum) value in the array:
Now that we have found the index that houses the least and greatest value, we need to create a result to print out in the resultLabel. Per the requirements of the challenge, the result should look like:
We now know the index values for both arrays that will give us both the name and number, so we can use a String.Format() method to display the desired result:
This takes the values of the arrays at the index with the largest and smallest values, then assigns the names and numbers to their correct places. It is separated into two different methods for readability and simplicity's sake. If you wanted, it would be perfectly acceptable to type it like this:
Finally, set the resultLabel.Text equal to the result. Save and run your project to see the result:
This completes the ChallengeForXmenBattleCount solution. While this challenge didn't require much code to solve, it did take a lot of thinking and applying several concepts we've covered up to this point. Hopefully you were able to complete it on your own. If not, come back and try it again later. Do it differently, come up with your own solution or tailor it to your tastes. Keep trying until you understand the concepts, then move on and continue with the course. Great job!
Solution - Challenge For Xmen Battle Count