In: Computer Science
Question: How do I write a program in C# that calculates a student's final grade for a test with 20 multiple questions using arrays and helper methods?
Instructions: (0-incorrect answer, 1-correct answer) If the student answered the question right, add .5 points to the running total. If the student didn’t answer correctly subtract .5 points from the running total. The running total is initialized with 5 points (so the student receives 5 points extra credit). To define the final grade use the grading scale below
Score Grade
90+ A
80-89 B
70-79 C
60-69 D
<60 F
Write a helper method displayScoreGrade()that displays the score and calculated grade.
private static void displayGrade(double score, char grade)
{ //printing the score; //printing the grade; ... } Call the method from the main method.
using System;
class test
{
private static void displayGrade(double score, char grade)
{
Console.WriteLine("Total Score: " + score);
Console.WriteLine("Grade: " + grade);
}
public static void Main(String[] args)
{
double score = 5;
double correct = 5;
//read 20 scores
for (int i = 0; i < 20; i++)
{
Console.Write("Enter Score of Q-" + (i + 1) + ": ");
int current = int.Parse(Console.ReadLine());
if (current == 1)
score += correct;
}
char grade;
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'F';
displayGrade(score,grade);
}
}
Screenshot of code:
Output:
Enter Score of Q-1: 1 Enter Score of Q-2: 0 Enter Score of Q-3: 1 Enter Score of Q-5: 1 Enter Score of Q-6: 1 Enter Score of Q-7: 1 Enter Score of Q-8: 1 Enter Score of Q-9: 0 Enter Score of Q-10: 1 Enter Score of Q-11: 1 Enter Score of Q-12: 1 Enter Score of Q-13: 1 Enter Score of Q-14: 1 Enter Score of Q-15: 1 Enter Score of Q-16: 0 Enter Score of Q-17: 1 Enter Score of Q-18: 1 Enter Score of Q-19: 1 Enter Score of 0-20: 1 Total Score: 90 Grade: A
TJ L. LIALI . 1.CAL Enter Score of Q-1: 0 Enter Score of Q-2: 0 Enter Score of Q-3: 0 Enter Score of Q-4: 0 Enter Score of 2-5: 1 Enter Score of 0-6: 0 Enter Score of Q-7: 1 Enter Score of Q-8: 1 Enter Score of 0-9: 1 Enter Score of Q-10: 1 Enter Score of Q-11: 0 Enter Score of Q-12: 1 Enter Score of Q-13: 1 Enter Score of Q-14: 1 Enter Score of Q-15: 1 Enter Score of Q-16: 0 Enter Score of Q-17: 0 Enter Score of Q-18: 1 Enter Score of Q-19: 1 Enter Score of 0-20: 1 Total Score: 65 Grade: D