Search⌘ K
AI Features

Calculate Average

Explore how to calculate an average in Ruby by collecting user-input scores, storing them in an array, and summing the elements using iteration or the inject method. This lesson helps you develop familiarity with array manipulation and basic Ruby functions for data processing.

We'll cover the following...

Problem

Write a program that asks the user to enter a set of student scores (non-negative integers) and then calculates the average. The program should end when the user enters -1.

Enter scores: (enter -1 to finish):
87
94
100
56
74
67
75
88
-1
Average score : 80
Calculate average score

Purpose

  • Insert elements into an array ...