In this part, you will write a Perl script called exam.pl to compute the final g
ID: 3829322 • Letter: I
Question
In this part, you will write a Perl script called exam.pl to compute the final grade for a given student. Your script will be run with the command below:
$ perl exam.pl
The table below summarizes the grading components:
Total Max
Quiz
10
Class Activities
135
Homework
150
Labs
155
Discussion
105
Exams
180
1. Set the total max of each grade component as constant
2. Write a function called sum that take an array of values, compute and return their sum
3. Write a function called percent that takes 2 values, compute and return the percentage of the first value relative to the second. For example, if 5 and 20 are passed to the function, it would return 25 (i.e. 5*100/20).
4. Write a function called grade that takes a percentage (number) and return the letter grade, with respect to the table below:
Percentage
Letter Grade
Percentage
Letter Grade
>= 95.0 <= 100.0
A+
>= 80.0 < 82.5
B-
>= 90.0 < 95.0
A
>= 77.5 < 80.0
C+
>= 87.5 < 90.0
A-
>= 70.0 < 77.5
C
>= 85.0 < 87.5
B+
>= 60.0 < 70.0
D
>= 82.5 < 85.0
B
< 60
F
5. Initialize a hash of scores to 0. The keys for the hash are the grading components. So the initial value for each component (i.e. quiz, hwk, etc.) in the hash would be 0.
6. For each component (use the foreach construct)
a. Prompt the user for a component score;
b. Store into the appropriate hash.
For example:
Enter the Quiz score: 9
Enter the Activities score: 125
7. Call the function sum passing the hash values as argument, store the return value in variable cumulative
8. Call the function percent passing the values cumulative and “sum of Total Max” as arguments. Store the return value in finalScore
9. Call the function grade passing finalScore as argument. Store the return value in variable finalGrade
10. Display the final Score and the final grade of the student.
Total Max
Quiz
10
Class Activities
135
Homework
150
Labs
155
Discussion
105
Exams
180
Explanation / Answer
my %hash = ("quiz" => 10,
"class activities" => 135,
"homework" => 150,
"labs" => 155,
"discussion" => 105,
"exams" => 180 );
my @scores = (65,67,78,90); #array of number
my $returned_values;
$returned_values = sum(@scores); #calling sum function
print "sum value = $returned_values ";
$returned_values = percent(5,20); #pass any value here
print "percent value = $returned_values ";
$returned_values = grade(72);
print "grade $returned_values ";
sub sum{
my @score_list = @_; #parameters are stored in @_
my $result = 0;
for (@score_list) {
$result = $result + $_; #computing sum
}
return $result;
}
sub percent{
my($first,$second) = @_;
my $result = ($first*100)/$second;
return $result;
}
sub grade{
my $percentage = $_[0];
return "unknown grade" if($percentage > 100); #passed value is greated then 100
return "A+" if($percentage >= 95.0 and $percentage <= 100.0);
return "A" if($percentage >= 90.0 and $percentage <= 95.0);
return "A-" if($percentage >= 87.5 and $percentage <= 90.0);
return "B+" if($percentage >= 85.0 and $percentage <= 87.5);
return "B" if($percentage >= 82.5 and $percentage <= 85.0);
return "B-" if($percentage >= 80.0 and $percentage <= 82.5);
return "C+" if($percentage >= 77.5 and $percentage <= 80.0);
return "C" if($percentage >= 70.0 and $percentage <= 77.5);
return "D" if($percentage >= 60.0 and $percentage <= 70.0);
return "F" if($percentage <= 60.0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.