Exercise – Construct a nested loop structure for student grade calculations
In this exercise, you’ll add a string array to hold the student names, and then implement a nested foreach
structure that iterates through the student names in an outer loop and student scores in the inner loop. You’ll begin by constructing the studentNames
array and a foreach
loop that iterates through the array elements. Next, you’ll move the code that’s used to calculate Sophia’s grades into the code block of the “names” loop. Finally, you’ll implement the code logic that uses the student’s name to access their scores array, calculate their average score, and write their grade to the console. The detailed tasks that you’ll complete during this exercise are:
- Create names array: Create a student names array.
- Create outer loop: Create a
foreach
loop that iterates through the student names. - Develop outer loop code block: Relocate the code that calculates and reports Sophia’s score, placing it in the code block of the “names” loop.
- Update calculations and reporting: Update the code that performs student score calculations using a new scores array.
Important
You need to have completed this module’s previous Exercise, “Create arrays and foreach loops”, before you begin this Exercise.
Create a student names array and outer foreach loop
In this task, you’ll create a student names array and a foreach
loop that iterates through the student names.
- Ensure that you have the Program.cs file open in the Visual Studio Code Editor.
- Scroll to the top of your code file, and then locate the code lines that are used to declare the scores arrays.
- Create a blank code line below the declaration of the scores arrays.Your blank code line should be located between the lines used to declare the scores arrays and the line used to declare
sophiaSum
.