Problem overview
Version 4 grades a whole class. The starter code has two parallel arrays: names holds "Alice", "Bob", "Carol" and "Dan", and scores holds 92, 78, 85 and 67. The student at position i in one array is the student at position i in the other.
The program prints one line per student, with the name, a colon, a space and the letter grade. The scale is the one from the previous version: 90 and above is A, 80 to 89 B, 70 to 79 C, 60 to 69 D and anything lower F. The expected output is:
Alice: A
Bob: C
Carol: B
Dan: D
Approach
The grading ladder from version 3 stays the same. What changes is that it must run once for each student, so it goes inside a loop. The loop variable i visits the positions 0, 1, 2 and 3, and each pass reads scores(i) for the ladder and names(i) for the output.
Parallel arrays only work while both have the same length and the same order. The loop trusts that, and it makes the whole program short: no student objects, no pairs, only an index shared by two arrays.
Step by step
- Write the loop.
for (i <- 0 until names.size)makesitake the values from 0 up to, but not including, the number of names. Close its brace at once; everything else goes inside. - Read the score.
val score = scores(i). Scala indexes an array with parentheses, not square brackets. - Declare
grade. Avarthat starts as empty text, because the ladder assigns it afterwards. Declaring it inside the loop gives every student a fresh one. - Start the ladder with an
iffor 90 or more that sets"A". - Add
else iffor 80 or more, which sets"B". The score is already known to be below 90. - Add
else iffor 70 or more, which sets"C". - Add
else iffor 60 or more, which sets"D". - End with
else, which sets"F", and close the ladder. - Print the student. Still inside the loop, after the ladder, so the line appears once per student.
The solution in Scala
val names = Array("Alice", "Bob", "Carol", "Dan")
val scores = Array(92, 78, 85, 67)
for (i <- 0 until names.size) {
val score = scores(i)
var grade = ""
if (score >= 90) {
grade = "A"
} else if (score >= 80) {
grade = "B"
} else if (score >= 70) {
grade = "C"
} else if (score >= 60) {
grade = "D"
} else {
grade = "F"
}
println(names(i) + ": " + grade)
}
What each pass does
The loop runs four times. Each pass reads one position from both arrays, runs the ladder and prints one line:
i | names(i) | scores(i) | printed |
|---|---|---|---|
| 0 | Alice | 92 | Alice: A |
| 1 | Bob | 78 | Bob: C |
| 2 | Carol | 85 | Carol: B |
| 3 | Dan | 67 | Dan: D |
Because grade is created inside the loop body, nothing carries over from one student to the next. Each pass begins with an empty string and ends with a letter.
Details that matter in Scala
0 until names.size is a Range that excludes its upper end, so for four names it covers 0 to 3. The solution could also write names.indices, which is the same range: printing either one gives Range 0 until 3 for a three-element array. A closed range, 0 to names.size, would include the last number and go one step too far.
names and scores are arrays. An Array is mutable but has a fixed size, and names(i) reads the element at position i. The line names(i) + ": " + grade joins strings with +, which is fine here because both sides are text.
A for loop without yield is shorthand for calling foreach on the range. The Range object stores only its start, end and step, so a range over a million positions takes no more memory than one over four. The loop body is ordinary code, so it can declare val and var names that live only for that pass.
Edge cases and common mistakes
- Going one step too far. With
for (i <- 0 to names.size)on an array of two names, the loop prints both names and then fails reading position 2 withIndex 2 out of bounds for length 2. - Arrays of different lengths. If
nameshas two entries andscoreshas one, the first line prints (Hal: Cfor a score of 75) and the second pass fails withIndex 1 out of bounds for length 1. The loop assumes the arrays match. - Empty arrays. With no students the loop body never runs and nothing is printed.
- Declaring
gradeoutside the loop. It would still work because every pass assigns it, but each student's grade should belong to that student's pass. - Boundary scores. A student with 90 gets an A, one with 59 gets an F and one with 60 gets a D. Running the ladder on the scores 90, 59 and 60 for Eve, Finn and Gus prints
Eve: A,Finn: FandGus: D.
Testing
Try the loop on other classes before trusting it. Scores of 90, 59 and 60 give an A, an F and a D, which checks the boundaries. An empty pair of arrays prints nothing. A class of one student prints one line. If a result looks wrong, print i and score inside the loop and compare them with the table above.
Key takeaways
- A shared index reads parallel arrays:
names(i)goes withscores(i). 0 until nstops beforen;0 to nincludes it.- Put a per-item calculation, and its output, inside the loop.
