From a88065976c048ad0060eca7a3ba1bb94b659adab Mon Sep 17 00:00:00 2001 From: ipvg Date: Wed, 17 Jul 2024 23:45:33 -0400 Subject: [PATCH] avnc 012 ChallengeProject --- 012_foreach_array_2/012_csharp.md | 263 +++++++++++++++++- .../ChallengeProject/Final/Final.csproj | 10 + .../ChallengeProject/Final/Program.cs | 144 ++++++++++ 012_foreach_array_2/ChallengeProject/LICENSE | 21 ++ .../ChallengeProject/README.md | 2 + .../ChallengeProject/Starter/Program.cs | 122 ++++++++ .../ChallengeProject/Starter/Starter.csproj | 10 + 7 files changed, 571 insertions(+), 1 deletion(-) create mode 100644 012_foreach_array_2/ChallengeProject/Final/Final.csproj create mode 100644 012_foreach_array_2/ChallengeProject/Final/Program.cs create mode 100644 012_foreach_array_2/ChallengeProject/LICENSE create mode 100644 012_foreach_array_2/ChallengeProject/README.md create mode 100644 012_foreach_array_2/ChallengeProject/Starter/Program.cs create mode 100644 012_foreach_array_2/ChallengeProject/Starter/Starter.csproj diff --git a/012_foreach_array_2/012_csharp.md b/012_foreach_array_2/012_csharp.md index 2ae0408..6487c79 100644 --- a/012_foreach_array_2/012_csharp.md +++ b/012_foreach_array_2/012_csharp.md @@ -16,4 +16,265 @@ array data based on an updated feature specification. the code branches and calculations required to satisfy an updated feature specification. -## +## Introduction + +End users often ask developers to add new features to an application. User +requests indicate that your application is being used, and more importantly, +that the customer plans to continue using your application. The customer just +wants some features updated. The ability to update an existing application +based on user requests is very important. A successful update will preserve +the integrity of the original application while providing the user with the +improved experience that they requested. + +Suppose you're a teacher's assistant at a school and that you developed an +application to automate the grading process. The application uses arrays to +store student names and graded assignments. The application also implements a +combination of iteration and selection statements to calculate and report each +student's final grade. Your application even differentiates between exam +assignments and extra credit assignments when calculating student grades. +Although the application does everything that the teacher asked for, you've +received a request for new features. The teacher has asked you to update the +grading report to show exam and extra credit assignment scores separately from +the final numeric score and letter grade. + +This module challenges you to add new capabilities to your Student Grading +application. + +In short, you need to use the teacher's updated grading report specification +to: + +- update the iteration and selection code to calculate separate final scores +for exams, extra credit assignments, and the overall grade. +- update the code that writes the grading report to the console. + +By the end of this module, you'll have an updated version of the Student Grading application that's able to calculate and report student grades in accordance with the teacher's updated requirements. + +> Note +> This is a challenge project module where you’ll complete an end-to-end +project from a specification. This module is intended to be a test of your +skills; there’s little guidance and no step-by-step instructions . + +### Learning Objectives + +- In this module, you'll demonstrate your ability to: + +- Use Visual Studio Code to revise a C# console application based on an updated feature specification. +- Update the variables and if statements in a C# console application to create the code branches and calculations required to satisfy an updated feature specification. +#### Prerequisites + +- Experience using Visual Studio Code to develop, build, and run C# console +applications that include console I/O and access the methods of .NET classes. +- Experience using `if` statements in C# application to evaluate expressions and +branch code accordingly. +- Experience using a `foreach` loop to access elements of array variables. + +--- + +## Prepare + +In this guided project, you'll use Visual Studio Code to develop portions of a +C# console application. You'll begin by writing the code that performs various +numeric calculations. All calculations must be completed within the existing +iteration and selection structures. This Prepare unit provides you with the +overall goals of the project and the requirements for your application. The +Setup section describes how to set up your development environment, including +a "Starter" code project. + +### Project specification + +The Starter code project for this module is a C# console application that +implements the following code features: + +- Uses arrays to store student names and assignment scores. + +- Uses a `foreach` statement to iterate through the student names as an outer +program loop. + +- Uses an `if` statement within the outer loop to identify the current student +'s name and access that student's assignment scores. + +- Uses a `foreach` statement within the outer loop to iterate through the +assignment scores array and sum the values. + +- Uses an algorithm within the outer loop to calculate the average exam score +for each student. + +- Use an `if-elseif-else` construct within the outer loop to evaluate the +average exam score and assign a letter grade automatically. + +- Integrates extra credit scores when calculating the student's final score and + letter grade as follows: + + - Detects extra credit assignments based on the number of elements in the + student's scores array. + - Applies a 10% weighting factor to extra credit assignments before adding + extra credit scores to the sum of exam scores. + +Your goal in this challenge is to implement the coding updates required to +produce the teacher's requested score report. + +The current score report lists the student's name followed by the calculated +overall grade and letter grade. Here is the existing report format: + +```txt +Student Grade Letter Grade + +Sophia 95.6 A +Andrew 91.6 A- +Emma 89.2 B+ +Logan 93 A +``` + +In addition to the student's final numeric score and letter grade, the teacher +wants the updated report to include the exam score and the impact that extra +credit work has on the student's final grade. The format of the updated score +report should appear as follows: + +```txt +Student Exam Score Overall Grade Extra Credit + +Sophia 92.2 95.88 A 92 (3.68 pts) +Andrew 89.6 91.38 A- 89 (1.78 pts) +Emma 85.6 90.94 A- 89 (5.34 pts) +Logan 91.2 93.12 A 96 (1.92 pts) +``` + +### Setup + +Use the following steps to prepare for the Challenge project exercises: + +To download a zip file containing the Starter project code, select the +following link: +[Lab Files](https://github.com/MicrosoftLearning/Challenge-project-foreach-if-array-CSharp/archive/refs/heads/main.zip). + +#### Unzip the download files. + +> Note +> If a folder named ChallengeProject already exists, you can select Replace +the files in the destination to complete the copy operation. + +You're now ready to begin the Challenge project exercises. Good luck! + +--- + +## Exercise + +### Update your formatted output + +The Student Grading application is used to calculate and report student grades +based on their graded exam and extra credit assignments. Your goal in this +challenge is to update the code that generates a score report in accordance +with the teacher's updated requirements. + +### Specification + +In this first challenge exercise, you need to instantiate the variables that +are required for the updated score report, and then update the +`Console.WriteLine()` statement that writes student grades to the console. + +Your updated application must: + +- use the existing arrays and array values for all student grade calculations. +- use the nested structure provided by the existing `foreach` and `if` +statements. +- declare and initialize any other integer variables that are required for +calculating sums. +- declare and initialize any other decimal variables that are required for +calculations and/or score report values. + +The format of the update score report is: + +```txt +Student Exam Score Overall Grade Extra Credit + +Sophia 0 95.8 A 0 (0 pts) +Andrew 0 91.2 A- 0 (0 pts) +Emma 0 90.4 A- 0 (0 pts) +Logan 0 93 A 0 (0 pts) +``` + +> Note +> Since this exercise does not include updating calculations, the score report +will show 0 for the new fields as shown above. + +### Check your work + +To validate that your code satisfies the specified requirements for this +exercise, complete the following steps: + +- build and run your app. +- Verify that your application creates the following output: + +```txt +Student Exam Score Overall Grade Extra Credit + +Sophia 0 95.8 A 0 (0 pts) +Andrew 0 91.2 A- 0 (0 pts) +Emma 0 90.4 A- 0 (0 pts) +Logan 0 93 A 0 (0 pts) +``` + +--- + +## Exercise + +### Update your calculated values + +The Student Grading application is used to calculate and report student grades +based on their graded exam and extra credit assignments. Your goal in this challenge is to update the code that calculates student grades in accordance with the teacher's updated requirements. + +### Specification + +In this second challenge exercise, you need to instantiate the variables that +are required for the updated score report, complete the required calculations, +and then update the `Console.WriteLine()` statement that writes student grades +to the console. + +Your updated application must: + +- use the existing arrays and array values for all student grade calculations. +- use the nested structure provided by the existing foreach and if statements. +- calculate the sum of exam and extra credit assignment scores using variables +from the first exercise or the original code. +- calculate the average for exam scores and extra credit scores using +variables from the first exercise or the original code. +- calculate the final numeric score as follows: add 10% of the extra credit +score sum to the exam score sum, and then divide that value by the number of +exams. +- calculate the extra credit points earned as follows: divide 10% of the extra +credit score sum by the number of exams. + +> Tip +> You will need to use (decimal) casting in your equations to preserve the +fractional component during your calculations. + +The required score report format is: + +```txt +Student Exam Score Overall Grade Extra Credit + +Sophia 92.2 95.88 A 92 (3.68 pts) +Andrew 89.6 91.38 A- 89 (1.78 pts) +Emma 85.6 90.94 A- 89 (5.34 pts) +Logan 91.2 93.12 A 96 (1.92 pts) +``` + +### Check your work + +To validate that your code satisfies the specified requirements, complete the +following steps: + +- Build and run your app. + +- Verify that your application creates the following output: + +```txt +Student Exam Score Overall Grade Extra Credit + +Sophia 92.2 95.88 A 92 (3.68 pts) +Andrew 89.6 91.38 A- 89 (1.78 pts) +Emma 85.6 90.94 A- 89 (5.34 pts) +Logan 91.2 93.12 A 96 (1.92 pts) +``` + +Congratulations if you succeeded in this challenge! diff --git a/012_foreach_array_2/ChallengeProject/Final/Final.csproj b/012_foreach_array_2/ChallengeProject/Final/Final.csproj new file mode 100644 index 0000000..91b464a --- /dev/null +++ b/012_foreach_array_2/ChallengeProject/Final/Final.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/012_foreach_array_2/ChallengeProject/Final/Program.cs b/012_foreach_array_2/ChallengeProject/Final/Program.cs new file mode 100644 index 0000000..3a26828 --- /dev/null +++ b/012_foreach_array_2/ChallengeProject/Final/Program.cs @@ -0,0 +1,144 @@ +/* +This C# console application is designed to: +- Use arrays to store student names and assignment scores. +- Use a `foreach` statement to iterate through the student names as an outer program loop. +- Use an `if` statement within the outer loop to identify the current student name and access that student's assignment scores. +- Use a `foreach` statement within the outer loop to iterate though the assignment scores array and sum the values. +- Use an algorithm within the outer loop to calculate the average exam score for each student. +- Use an `if-elseif-else` construct within the outer loop to evaluate the average exam score and assign a letter grade automatically. +- Integrate extra credit scores when calculating the student's final score and letter grade as follows: + - detects extra credit assignments based on the number of elements in the student's scores array. + - divides the values of extra credit assignments by 10 before adding extra credit scores to the sum of exam scores. +- use the following report format to report student grades: + +Student Exam Score Overall Grade Extra Credit + +Sophia 92.2 95.88 A 92 (3.68 pts) + +*/ +int examAssignments = 5; + +string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" }; + +int[] sophiaScores = new int[] { 90, 86, 87, 98, 100, 94, 90 }; +int[] andrewScores = new int[] { 92, 89, 81, 96, 90, 89 }; +int[] emmaScores = new int[] { 90, 85, 87, 98, 68, 89, 89, 89 }; +int[] loganScores = new int[] { 90, 95, 87, 88, 96, 96 }; + +int[] studentScores = new int[10]; + +string currentStudentLetterGrade = ""; + +// display the header row for scores/grades +Console.Clear(); +Console.WriteLine("Student\t\tExam Score\tOverall Grade\tExtra Credit\n"); + +/* +The outer foreach loop is used to: +- iterate through student names +- assign a student's grades to the studentScores array +- calculate exam and extra credit sums (inner foreach loop) +- calculate numeric and letter grade +- write the score report information +*/ +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 gradedAssignments = 0; + int gradedExtraCreditAssignments = 0; + + int sumExamScores = 0; + int sumExtraCreditScores = 0; + + decimal currentStudentGrade = 0; + decimal currentStudentExamScore = 0; + decimal currentStudentExtraCreditScore = 0; + + /* + the inner foreach loop: + - sums the exam and extra credit scores + - counts the extra credit assignments + */ + foreach (int score in studentScores) + { + gradedAssignments += 1; + + if (gradedAssignments <= examAssignments) + { + sumExamScores = sumExamScores + score; + } + + else + { + gradedExtraCreditAssignments += 1; + sumExtraCreditScores += score; + } + } + + currentStudentExamScore = (decimal)(sumExamScores) / examAssignments; + currentStudentExtraCreditScore = (decimal)(sumExtraCreditScores) / gradedExtraCreditAssignments; + + currentStudentGrade = (decimal)((decimal)sumExamScores + ((decimal)sumExtraCreditScores / 10)) / examAssignments; + + if (currentStudentGrade >= 97) + currentStudentLetterGrade = "A+"; + + else if (currentStudentGrade >= 93) + currentStudentLetterGrade = "A"; + + else if (currentStudentGrade >= 90) + currentStudentLetterGrade = "A-"; + + else if (currentStudentGrade >= 87) + currentStudentLetterGrade = "B+"; + + else if (currentStudentGrade >= 83) + currentStudentLetterGrade = "B"; + + else if (currentStudentGrade >= 80) + currentStudentLetterGrade = "B-"; + + else if (currentStudentGrade >= 77) + currentStudentLetterGrade = "C+"; + + else if (currentStudentGrade >= 73) + currentStudentLetterGrade = "C"; + + else if (currentStudentGrade >= 70) + currentStudentLetterGrade = "C-"; + + else if (currentStudentGrade >= 67) + currentStudentLetterGrade = "D+"; + + else if (currentStudentGrade >= 63) + currentStudentLetterGrade = "D"; + + else if (currentStudentGrade >= 60) + currentStudentLetterGrade = "D-"; + + else + currentStudentLetterGrade = "F"; + + + // Student Exam Score Overall Grade Extra Credit + // Sophia 92.2 95.88 A 92 (3.68 pts) + + Console.WriteLine($"{currentStudent}\t\t{currentStudentExamScore}\t\t{currentStudentGrade}\t{currentStudentLetterGrade}\t{currentStudentExtraCreditScore} ({(((decimal)sumExtraCreditScores / 10) / examAssignments)} pts)"); +} + +// required for running in VS Code (keeps the Output windows open to view results) +Console.WriteLine("\n\rPress the Enter key to continue"); +Console.ReadLine(); diff --git a/012_foreach_array_2/ChallengeProject/LICENSE b/012_foreach_array_2/ChallengeProject/LICENSE new file mode 100644 index 0000000..2080d95 --- /dev/null +++ b/012_foreach_array_2/ChallengeProject/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Microsoft Learning + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/012_foreach_array_2/ChallengeProject/README.md b/012_foreach_array_2/ChallengeProject/README.md new file mode 100644 index 0000000..b555e6f --- /dev/null +++ b/012_foreach_array_2/ChallengeProject/README.md @@ -0,0 +1,2 @@ +# Challenge-project-foreach-if-array-CSharp +Starter and Solution code for the Challenge project: "Develop foreach and if-elseif-else structures to process array data in C#" from the Microsoft Learn collection "Getting started with C#" diff --git a/012_foreach_array_2/ChallengeProject/Starter/Program.cs b/012_foreach_array_2/ChallengeProject/Starter/Program.cs new file mode 100644 index 0000000..929e66a --- /dev/null +++ b/012_foreach_array_2/ChallengeProject/Starter/Program.cs @@ -0,0 +1,122 @@ +/* +This C# console application is designed to: +- Use arrays to store student names and assignment scores. +- Use a `foreach` statement to iterate through the student names as an outer +program loop. +- Use an `if` statement within the outer loop to identify the current student +name and access that student's assignment scores. +- Use a `foreach` statement within the outer loop to iterate though the +assignment scores array and sum the values. +- Use an algorithm within the outer loop to calculate the average exam score +for each student. +- Use an `if-elseif-else` construct within the outer loop to evaluate the +average exam score and assign a letter grade automatically. +- Integrate extra credit scores when calculating the student's final score and +letter grade as follows: + - detects extra credit assignments based on the number of elements in the + student's scores array. + - divides the values of extra credit assignments by 10 before adding extra + credit scores to the sum of exam scores. +- use the following report format to report student grades: + + Student Grade + + Sophia: 92.2 A- + Andrew: 89.6 B+ + Emma: 85.6 B + Logan: 91.2 A- +*/ +int examAssignments = 5; +string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" }; + +int[] sophiaScores = new int[] { 90, 86, 87, 98, 100, 94, 90 }; +int[] andrewScores = new int[] { 92, 89, 81, 96, 90, 89 }; +int[] emmaScores = new int[] { 90, 85, 87, 98, 68, 89, 89, 89 }; +int[] loganScores = new int[] { 90, 95, 87, 88, 96, 96 }; + +int[] studentScores = new int[10]; +string currentStudentLetterGrade = ""; + +Console.Clear(); +Console.WriteLine("Student\t\tExam Score\tOverall\tGrade\tExtra Credit\n"); + +/* +The outer foreach loop is used to: +- iterate through student names +- assign a student's grades to the studentScores array +- sum assignment scores (inner foreach loop) +- calculate numeric and letter grade +- write the score report information +*/ +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 sumAssignmentScores = 0; + decimal currentStudentGrade = 0; + int gradedAssignments = 0; + int sumExamScore = 0; + int extraCredit = 0; + // the inner foreach loop sums assignment scores + // extra credit assignments are worth 10% of an exam score + foreach (int score in studentScores){ + gradedAssignments += 1; + if (gradedAssignments <= examAssignments){ + sumAssignmentScores += score; + sumExamScore = sumAssignmentScores; + } else { + sumAssignmentScores += score / 10; + extraCredit += score / 10; + } + } + decimal examScore = (decimal)(sumExamScore)/examAssignments; + currentStudentGrade = (decimal)sumAssignmentScores / examAssignments; + decimal extraCreditPts = (decimal)(extraCredit) / examAssignments; + + currentStudentLetterGrade = getLetterGrade(currentStudentGrade); + Console.Write($"{currentStudent}\t\t"); + Console.Write($"{examScore}\t\t"); + Console.Write($"{currentStudentGrade}\t"); + Console.Write($"{currentStudentLetterGrade}\t"); + Console.WriteLine($"{extraCredit} ({extraCreditPts} pts)"); +} + +string getLetterGrade(decimal score){ + string grade; + if (score < 60){ + grade = "F"; + } else if (score < 63){ + grade = "D-"; + } else if (score < 67){ + grade = "D"; + } else if (score < 70){ + grade = "D+"; + } else if (score < 73){ + grade = "C-"; + } else if (score < 77){ + grade = "C"; + } else if (score < 80){ + grade = "C+"; + } else if (score < 83){ + grade = "B-"; + } else if (score < 87){ + grade = "B"; + } else if (score < 90){ + grade = "B+"; + } else if (score < 93){ + grade = "A-"; + } else if (score < 97){ + grade = "A"; + } else { + grade = "A+"; + } + return grade; +} diff --git a/012_foreach_array_2/ChallengeProject/Starter/Starter.csproj b/012_foreach_array_2/ChallengeProject/Starter/Starter.csproj new file mode 100644 index 0000000..91b464a --- /dev/null +++ b/012_foreach_array_2/ChallengeProject/Starter/Starter.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + +