Recap - Grade Summarizer
Part of the Logic & Flow section of Coddy's Rust journey — lesson 66 of 66.
Challenge
EasyYou will receive multiple inputs representing student grades. The first input is a number indicating how many grades will follow. Then you will receive that many grade values (one per line). Process these grades and provide a comprehensive summary.
Requirements:
- Read the first input (number of grades), trim whitespace, and parse it to
i32 - Create an empty vector to store the grades
- Use a loop to read each grade, trim whitespace, parse it to
i32, and add it to the vector - Calculate the average grade (sum of all grades divided by the count)
- Find the highest grade in the vector
- Find the lowest grade in the vector
- Count how many grades are passing (70 or above)
- Count how many grades are failing (below 70)
- Print the results in the exact format shown below
Output Format:
- Line 1:
Average: X(where X is the average as an integer) - Line 2:
Highest: X(where X is the highest grade) - Line 3:
Lowest: X(where X is the lowest grade) - Line 4:
Passing: X(where X is the count of passing grades) - Line 5:
Failing: X(where X is the count of failing grades)
Input:
- First line: Number of grades (e.g.,
5) - Following lines: Individual grade values (e.g.,
85,92,67,78,95)
Output:
- Five lines showing the average, highest, lowest, passing count, and failing count
Try it yourself
use std::io;
fn main() {
// Read the number of grades
let mut count_input = String::new();
io::stdin().read_line(&mut count_input).expect("Failed to read line");
let count: i32 = count_input.trim().parse().expect("Invalid number");
// Create a vector to store grades
let mut grades: Vec<i32> = Vec::new();
// Read each grade
for _ in 0..count {
let mut grade_input = String::new();
io::stdin().read_line(&mut grade_input).expect("Failed to read line");
let grade: i32 = grade_input.trim().parse().expect("Invalid number");
grades.push(grade);
}
// TODO: Write your code below
// Calculate average, find highest, find lowest, count passing and failing grades
// Output the results in the required format
// println!("Average: {}", average);
// println!("Highest: {}", highest);
// println!("Lowest: {}", lowest);
// println!("Passing: {}", passing_count);
// println!("Failing: {}", failing_count);
}All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together