Created
July 29, 2025 14:28
-
-
Save mukhtharcm/d935fabcb93efdc7a35cf01e26fa6a7c to your computer and use it in GitHub Desktop.
Day 2 Mini-Project: Write a function that takes a List of int (e.g., student scores) and returns their average.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This function calculates the average of a list of scores. | |
double calculateAverage(List<int> scores) { | |
if (scores.isEmpty) { | |
return 0; // Avoid division by zero if the list is empty. | |
} | |
int total = 0; | |
for (int score in scores) { | |
total = total + score; | |
} | |
// The .length property gives us the number of items in the list. | |
return total / scores.length; | |
} | |
void main() { | |
List<int> studentScores = [88, 95, 74, 100, 81]; | |
double average = calculateAverage(studentScores); | |
print("The student scores are: $studentScores"); | |
print("The average score is: $average"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment