Student Grade Calculator
Part of the Fundamentals section of Coddy's Rust journey — lesson 75 of 75.
Challenge
EasyCreate a function named calculate_average_grade that takes an array of integers (representing student grades) of length 8 as input and returns the average grade as a string. The method should do the following:
- Calculate the sum of all grades in the array.
- Calculate the average grade by dividing the sum by the number of grades.
- Determine the letter grade based on the average:
- A (Excellent): 90-100
- B (Good): 80-89
- C (Satisfactory): 70-79
- D (Needs Improvement): 60-69
- F (Failed): Below 60
- Print the result using the following format:
Average grade: [The result] - [Letter Grade]and format the result to two decimal places.
Try it yourself
use std::io;
use std::convert::TryInto;
fn calculate_average_grade(grades: [i32; 8]) -> String {
// Write your code below
}
fn main() {
let mut input_str_arr = String::new();
io::stdin().read_line(&mut input_str_arr).unwrap();
let arr: [i32; 8] = input_str_arr.trim().split(',').map(|s| s.parse::<i32>().unwrap()).collect::<Vec<i32>>().try_into().unwrap();
let res = calculate_average_grade(arr);
println!("{}", res);
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input