AP CSA 2.11 Debugging: The Inner Loop That Remembers

Unit 2: Selection and Iteration · Lesson 2.11 · Debugging

The Inner Loop That Remembers

Nested Iteration. This is not a blank editor: it is someone else's attempt. Find what is wrong, fix it, and submit to be graded against hidden test cases.

Why debugging is its own skill

An inner loop runs completely, from its start value to its end value, every single time the outer loop takes one step. When something that should be reset per row is initialized outside the outer loop instead, the rows quietly contaminate each other.

What is wrong with it

  1. The grid should print rows rows of cols stars each. The inner loop uses the OUTER loop variable in its bound, so the rows come out ragged instead of rectangular. Fix the inner bound to use the column count.
  2. The per-row star count is supposed to reset at the start of each row. It is declared before the outer loop, so it keeps growing and every row reports a larger number than the one before. Move the declaration where it belongs.
  3. The total across all rows, printed at the very end, is correct and should stay outside the loops.

The program reads

Two integers, a number of rows and a number of columns, both at least 1.

The program should print

For each row, a line of that many stars followed by a line with the count of stars in that row. After all rows, the total number of stars printed.

Worked examples

These show what the FIXED program should print. There are more cases you cannot see, and they use different values, so patching around just these numbers will fail.

Example 1 input
3 4
Example 1 output
****
4
****
4
****
4
12
Example 2 input
1 5
Example 2 output
*****
5
5

Your answer

Main.java

Input for the Run button

Run sends whatever is in the code box below, bugs and all, so you can see the crash or the wrong answer for yourself before you fix anything.


  

  

Stuck?

Hint 1

c <= r makes the number of stars depend on which row you are on, which is how you draw a triangle. A rectangle needs the inner bound to be the same every row, and that value is cols.

Hint 2

Ask where each variable should be born. rowCount describes one row, so it is born at the top of a row and dies at the bottom: declare it inside the outer loop. total describes the whole grid, so it is born before any row starts.

Hint 3

A variable declared inside a loop body is created fresh on every pass, which IS the reset. You do not need an explicit assignment back to zero if you put the declaration in the right place.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]