Exercise - Construct a nested loop structure for student grade calculations
In this exercise, you 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 begin by constructing the studentNames
array and a foreach
loop that iterates through the array elements. Next, you move the code that's used to calculate Sophia's grades into the code block of the "names" loop. Finally, you 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 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
.To create a string array named
studentNames
that holds the names of the students, enter the following code:// Student names string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" };
Notice that you've specified the student names as part of the declaration.
To create a
foreach
statement that you can use to iterate through the student names, enter the following code:foreach (string name in studentNames) { }
To verify that your
foreach
loop is iterating through thestudentNames
array as intended, update the code block of theforeach
statement as follows:foreach (string name in studentNames) { Console.WriteLine($"{name}"); }
Take a minute to review the code that you've created.
// Student names string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" }; foreach (string name in studentNames) { Console.WriteLine($"{name}"); }
Your code will use this
foreach
loop as the outer loop of your application. During this exercise, you'll implement the following logic in your application:For each of the students in the
studentNames
array, your application will:- determine the current student.
- access the current student's scores.
- calculate the current student's grade (sum and average).
- write the current student's grade to the console.
For now, however, you'll just write the names of the students to the console.
On the Visual Studio Code File menu, click Save.
In the Visual Studio Code EXPLORER view, right-click Starter, and then select Open in Integrated Terminal.
Important
The Terminal command prompt must be displaying the folder path for your Program.cs file.
At the Terminal command prompt, type dotnet build and then press Enter.
The
dotnet build
command instructs the compiler to build the application. If any errors are detected, they'll be reported.If you see Error or Warning messages, you need to fix them before continuing.
At the Terminal command prompt, type dotnet run and then press Enter.
Verify that your code produced the following output:
Sophia Andrew Emma Logan Student Grade Sophia: 92.2 A- Press the Enter key to continue
Note
If you don't see the list of student names above Sophia's score report, go back and verify that you entered your code correctly.
In the TERMINAL panel, to stop your running application, press the Enter key.
Close the Terminal panel.
Calculate Sophia's score inside the outer names loop
In this task, you'll relocate the code that calculates and reports Sophia's score, placing it in the code block of the "names" loop.
In the Visual Studio Code Editor, locate the code lines that are used to calculate and report Sophia's grade.
int sophiaSum = 0; decimal sophiaScore; foreach (int score in sophiaScores) { // add the exam score to the sum sophiaSum += score; } sophiaScore = (decimal)sophiaSum / currentAssignments; Console.WriteLine("Student\t\tGrade\n"); Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA-");
Note
Your next step will be to move this code from its current location to the code block of the "names"
foreach
loop.Use a cut-and-paste operation to move the code that calculates and reports Sophia's grade to the code block of the "names"
foreach
loop.If you're unsure how to cut-and-paste in Visual Studio Code, try the approach described in the following steps:
Select the code that's used to calculate and report Sophia's grade.
You can click-and-drag to select code lines.
On the Visual Studio Code Edit menu, select Cut.
In the Visual Studio Code Editor, position the cursor on the blank code line below the following code:
Console.WriteLine($"{name}");
On the Visual Studio Code Edit menu, select Paste.
Update your code to display proper code line indentation.
Tip
Visual Studio Code provides a
Format Document
command that can be used to keep your code formatting updated. Right-click inside the Visual Studio Code Editor, and then select Format Document from the popup menu. On Windows computers, the keyboard shortcut for this command is:Shift + Alt + F
. Linux and macOS computers use alternative shortcuts when necessary to avoid conflicts with OS supplied shortcuts.Ensure that your updates match the following code:
// Student names string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" }; foreach (string name in studentNames) { Console.WriteLine($"{name}"); int sophiaSum = 0; decimal sophiaScore; foreach (int score in sophiaScores) { // add the exam score to the sum sophiaSum += score; } sophiaScore = (decimal)sophiaSum / currentAssignments; Console.WriteLine("Student\t\tGrade\n"); Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA-"); } Console.WriteLine("Press the Enter key to continue"); Console.ReadLine();
Notice that at this point, your code will calculate and report Sophia's score regardless of the
name
of the current student. You'll address that shortly.Delete the following code:
Console.WriteLine($"{name}");
On the blank code line that you just created, enter the following code:
if (name == "Sophia") {
Create a blank code line after the code that's used to write Sophia's grade to the console.
To close the code block of the
if
statement, enter the following code:}
Update your code to display proper code line indentation.
Tip
Use the
Format Document
command to keep your code formatting updated. Right-click inside the Visual Studio Code Editor panel, and then select Format Document from the popup menu.Take a minute to review your code.
Your code should match the following code:
// initialize variables - graded assignments int currentAssignments = 5; int[] sophiaScores = new int[] { 90, 86, 87, 98, 100 }; int[] andrewScores = new int[] { 92, 89, 81, 96, 90 }; int[] emmaScores = new int[] { 90, 85, 87, 98, 68 }; int[] loganScores = new int[] { 90, 95, 87, 88, 96 }; // Student names string[] studentNames = new string[] {"Sophia", "Andrew", "Emma", "Logan"}; foreach (string name in studentNames) { if (name == "Sophia") { int sophiaSum = 0; decimal sophiaScore; foreach (int score in sophiaScores) { // add the exam score to the sum sophiaSum += score; } sophiaScore = (decimal)(sophiaSum) / currentAssignments; Console.WriteLine("Student\t\tGrade\n"); Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA-"); } } Console.WriteLine("Press the Enter key to continue"); Console.ReadLine();
Notice that the
if
statement inside your outerforeach
code block limits which student's grade is calculated and reported. This isn't exactly what you need, but it's a step in the right direction.On the Visual Studio Code File menu, click Save.
In the Visual Studio Code EXPLORER view, right-click Starter, and then select Open in Integrated Terminal.
Important
The Terminal command prompt must be displaying the folder path for your Program.cs file.
At the Terminal command prompt, type dotnet build and then press Enter.
The
dotnet build
command instructs the compiler to build the application. If any errors are detected, they'll be reported.If you see Error or Warning messages, you need to fix them before continuing.
At the Terminal command prompt, type dotnet run and then press Enter.
Verify that your code produced the following output:
Student Grade Sophia: 92.2 A-
Note
If you still see the list of student names displayed above Sophia's score report, make sure that you saved your updates.
In the TERMINAL panel, to stop your running application, press the Enter key.
Close the Terminal panel.
Update the nested loop to calculate all student scores
In this task, you'll update the code that performs student score calculations using a new scores array. You'll begin by creating an array named studentScores
that can be used to hold the scores of any student. Next, you'll create an if .. elseif
construct that uses the current student's name to assign their scores array to studentScores
. Finally, you'll update the code that calculates and reports the student's grades. When you're done, the report should include the name and numeric score for all students.
Create a blank code line below the declaration of the
studentNames
array.The blank line should be above the outer
foreach
statement.To create an integer array that you can use to hold the scores of the current student, enter the following code:
int[] studentScores = new int[10];
Notice that this code doesn't assign any values to the array at this point. You simply specify that the array can contain 10 elements.
Create a blank code line at the top of the outer
foreach
code block.The blank line should be inside the
foreach
code block and above theif
statement that evaluates whethername
is equal to Sophia.To create a string variable that will be used to hold the name of the current student, enter the following code:
string currentStudent = name;
Note
You could continue to use
name
to track the name of the current student as you iterate through the names array, but usingcurrentStudent
will make it easier to understand your code logic as you build out your application in the upcoming steps.To substitute
currentStudent
forname
in theif
statement that evaluates whethername
is equal to Sophia, update your code as follows:if (currentStudent == "Sophia")
Move the code that calculates and reports Sophia's score to a location below the code block.
You're moving all of the code that's in the code block to a location below the code block. The reason for doing this will become apparent during the next few steps.
Verify that the code in your outer
foreach
code block matches the following code:{ string currentStudent = name; if (currentStudent == "Sophia") { } int sophiaSum = 0; decimal sophiaScore; foreach (int score in sophiaScores) { // add the exam score to the sum sophiaSum += score; } sophiaScore = (decimal)sophiaSum / currentAssignments; Console.WriteLine("Student\t\tGrade\n"); Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA-"); }
To assign the
sophiaScores
array tostudentScores
whencurrentStudent == "Sophia"
, update yourif
statement code as follows:if (currentStudent == "Sophia") studentScores = sophiaScores;
Notice that you've removed the curly braces from the
if
statement code block during this code update.To add an
else if
statement that assigns theandrewScores
array tostudentScores
whencurrentStudent == "Andrew"
, enter the following code:else if (currentStudent == "Andrew") studentScores = andrewScores;
Create another
else if
statement to assign theemmaScores
array tostudentScores
whencurrentStudent == "Emma"
.Create an
else if
statement to assign theloganScores
array tostudentScores
whencurrentStudent == "Logan"
.Ensure that your
foreach
code block matches the following code:foreach (string name in studentNames) { string currentStudent = name; if (currentStudent == "Sophia") studentScores = sophiaScores; else if (currentStudent == "Andrew") studentScores = andrewScores; else if (currentStudent == "Emma") studentScores = emmaScores; else if (currentStudent == "Logan") studentScores = loganScores; int sophiaSum = 0; decimal sophiaScore; foreach (int score in sophiaScores) { // add the exam score to the sum sophiaSum += score; } sophiaScore = (decimal)sophiaSum / currentAssignments; Console.WriteLine("Student\t\tGrade\n"); Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA-"); }
Next, you need to update the inner
foreach
loop to usestudentScores
and "depersonalize" the variables that you use in your calculations.To substitute
studentScores
forsophiaScores
in theforeach
loop that iterates through the scores array, update your code as follows:foreach (int score in studentScores)
To replace the Sophia-specific variable declarations with more generic names, update your code as follows:
int sumAssignmentScores = 0; decimal currentStudentGrade = 0;
These two variable declarations are intended to replace the following Sophia-specific variable declarations:
int sophiaSum = 0; decimal sophiaScore;
To apply the new variable name to the equation used to sum student scores, update your inner
foreach
code block as follows:foreach (int score in studentScores) { // add the exam score to the sum sumAssignmentScores += score; }
To apply the new variable name to the equation used to calculate the average score, update your code as follows:
currentStudentGrade = (decimal)(sumAssignmentScores) / currentAssignments;
Take a minute to review your code.
int[] studentScores = new int[10]; foreach (string name in studentNames) { string currentStudent = name; if (currentStudent == "Sophia") studentScores = sophiaScores; else if (currentStudent == "Andrew") studentScores = andrewScores; else if (currentStudent == "Emma") studentScores = emmaScores; else if (currentStudent == "Logan") studentScores = loganScores; // initialize/reset the sum of scored assignments int sumAssignmentScores = 0; // initialize/reset the calculated average of exam + extra credit scores decimal currentStudentGrade = 0; foreach (int score in studentScores) { // add the exam score to the sum sumAssignmentScores += score; } currentStudentGrade = (decimal)(sumAssignmentScores) / currentAssignments; Console.WriteLine("Student\t\tGrade\n"); Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA-"); }
Your nested
foreach
loops will now iterate through the student names and use the student's scores to calculate their grades, but you still need to update the code used to generate the score report.To print the student name and calculated score to the console, update the second
Console.WriteLine
statement as follows:Console.WriteLine($"{currentStudent}\t\t{currentStudentGrade}\t?");
Notice that this code has replaced the letter grade assignment with a "?". You'll work on automating the assignment of letter grades in the next exercise.
Move the
Console.WriteLine
statement that's used to write the column labels of your score report to the location just above the outerforeach
loop.You don't want to repeat the column headers for each student score, so you move this code to a point above the outer
foreach
loop.On the Visual Studio Code File menu, click Save.
Take a minute to review your application code.
Your full application should now match the following code:
// initialize variables - graded assignments int currentAssignments = 5; int[] sophiaScores = new int[] { 90, 86, 87, 98, 100 }; int[] andrewScores = new int[] { 92, 89, 81, 96, 90 }; int[] emmaScores = new int[] { 90, 85, 87, 98, 68 }; int[] loganScores = new int[] { 90, 95, 87, 88, 96 }; // Student names string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" }; int[] studentScores = new int[10]; // Write the Report Header to the console Console.WriteLine("Student\t\tGrade\n"); foreach (string name in studentNames) { string currentStudent = name; if (currentStudent == "Sophia") studentScores = sophiaScores; else if (currentStudent == "Andrew") studentScores = andrewScores; else if (currentStudent == "Emma") studentScores = emmaScores; else if (currentStudent == "Logan") studentScores = loganScores; // initialize/reset the sum of scored assignments int sumAssignmentScores = 0; // initialize/reset the calculated average of exam + extra credit scores decimal currentStudentGrade = 0; foreach (int score in studentScores) { // add the exam score to the sum sumAssignmentScores += score; } currentStudentGrade = (decimal)(sumAssignmentScores) / currentAssignments; Console.WriteLine($"{currentStudent}\t\t{currentStudentGrade}\t?"); }
With the code that generates the student's score report updated; it appears that you're ready to check your work.
Check your work
In this task, you'll run the application to verify that your code logic is working as expected.
Ensure that you've saved your changes to the Program.cs file.
In the Visual Studio Code EXPLORER view, right-click Starter, and then select Open in Integrated Terminal.
At the Terminal command prompt, type dotnet build and then press Enter.
If you see Error or Warning messages, you need to fix them before continuing.
At the Terminal command prompt, type dotnet run and then press Enter.
Verify that your code produced the following output:
Student Grade Sophia 92.2 ? Andrew 89.6 ? Emma 85.6 ? Logan 91.2 ? Press the Enter key to continue
In the TERMINAL panel, to stop your running application, press the Enter key.
Close the Terminal panel.
Congratulations, your application has come a long way from where you started out. You're making efficient use of arrays and foreach
iterations, and you've integrated an if
statement that enables your code to select the correct scores array.