Skip to content

Instantly share code, notes, and snippets.

@mukhtharcm
Created July 29, 2025 14:28
Show Gist options
  • Save mukhtharcm/d935fabcb93efdc7a35cf01e26fa6a7c to your computer and use it in GitHub Desktop.
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 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