Menu
Coddy logo textTech

Student Grade Calculator

Part of the Fundamentals section of Coddy's Rust journey — lesson 75 of 75.

challenge icon

Challenge

Easy

Create 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:

  1. Calculate the sum of all grades in the array.
  2. Calculate the average grade by dividing the sum by the number of grades.
  3. 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
  4. 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