Exercise 10 of 10 · Putting it all together
Build a Grade Report
What you will make
A small grade report that labels each score as Pass or Fail and prints the class average, using everything from earlier exercises together.
The one new idea: Combining loops, conditionals, a vector, and formatted output in one program
Real programs rarely use just one idea in isolation — this exercise combines a vector, a loop, a conditional, an accumulator, and formatted output into a single small but realistic program, the same way a real script would.
Go straight to the code ↓The concept
This exercise doesn't introduce a brand-new R feature — instead, it asks you to combine everything from the exercises before it into one realistic program: a vector holding a collection of data, a for loop with length() visiting every item, an if/else making a decision about each item, an accumulator building a running total across all of them, and formatted cat() output tying it all together. Real-world scripts look exactly like this — several small, familiar pieces working together rather than one exotic new trick.
When a bug shows up in a program built from several pieces, it helps to check each piece against what it's supposed to do on its own, one at a time, rather than assuming a single change will fix everything.
Worked example
stock <- c(2, 9, 0, 5)
total <- 0
for (i in 1:length(stock)) {
count <- stock[i]
total <- total + count
if (count == 0) {
note <- "Out of stock"
} else {
note <- "In stock"
}
cat("Item ", i, ": ", count, " (", note, ")\n", sep = "")
}
cat("Total items: ", total, "\n", sep = "")Item 1: 2 (In stock)
Item 2: 9 (In stock)
Item 3: 0 (Out of stock)
Item 4: 5 (In stock)
Total items: 16Each item gets its own labeled line based on a condition, while total quietly accumulates the sum of every item, printed once at the very end — the same shape as the program you'll fix.
Your turn
The starter code has two separate, unrelated bugs. First, inside the loop, the passing cutoff used in the if condition doesn't match the cutoff described in the comment at the top of the file — fix the number being compared. Second, after the loop, the average is computed by dividing by a number typed directly into the code instead of by the actual count of scores — fix it to use length(scores) instead.
If something goes wrong
Work through the two bugs separately. If a score you'd expect to pass is labeled "Fail" (or the reverse), re-check the number in the if condition against the comment at the top of the file. If the individual Pass/Fail labels look right but the final average seems off, look specifically at the line computing average, after the loop has finished, and confirm it divides by length(scores) rather than a fixed number.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Fixing only one of the two bugs and assuming the program is done.
- The pass/fail cutoff and the average calculation are independent problems in different parts of the code — fixing the threshold doesn't correct the average, and vice versa; both need attention.
- Hard-coding the average's denominator to match the current vector by counting the values in c(...) manually.
- That would produce a correct-looking number right now, but the whole point of using length(scores) is that the average calculation keeps working automatically if the scores vector ever changes size.
- Changing the numbers inside the scores vector to make the pass/fail results look different.
- The scores themselves (58, 76, 91, 64, 83) are the given data for this exercise; the bugs are in how that data is evaluated and summarized, not in the data itself.
- Moving the total <- total + score line inside the if block.
- The running total needs to include every score, not just the passing ones — it belongs directly inside the loop but outside (before) the if/else, exactly as the starter code already has it.
Want a blank editor instead? Open the R playground.