In: Computer Science
Write a program that calculates the student's final grade for a test with 20 multiple questions using C# .net Framework.
se an array of integers to store the test scores (0-incorrect answer, 1-correct answer). Initialize it as follows:
int [] scores = {1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1};
3. 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.
4. The running total is initialized with 5 points (so the student receives 5 points extra credit).
5. 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 |
6. 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.
Code -
using System;
class HelloWorld {
//function to display Grade
private static void displayGrade(double score, char grade){
//calculate grade according to score
if(score>=90 && score <=100){
grade = 'A';
}
else if(score>=80 && score <=89){
grade = 'B';
}
else if(score>=70 && score <=79){
grade = 'C';
}
else if(score>=60 && score <=69){
grade = 'D';
}
else {
grade = 'F';
}
//print the grade in Console
Console.WriteLine("Total marks is "+score+" grade is "+grade
);
}
static void Main() {
//SCORE ARRAY
int [] scores = {1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1,
1, 0, 1};
int i;
//define sum by 5
double sum=5;
char grade='A';
//read the score from array and add marks to sum for correct
answer
for(i=0;i<scores.Length;i++){
//correct answer check
if(scores[i] == 1){
sum+=5;
}
//incorrect answer check
else{
sum-=0.5;
}
}
//call function to dislay grade
displayGrade(sum,grade);
}
}
Screenshots -