Grid With Row and Column Adds
Implement a class GridAdder for a grid of numbers that starts filled with zeros. Rows are numbered from 0 at the top and columns from 0 at the left. Write these methods:
GridAdder(rows, cols)makes a grid withrowsrows andcolscolumns in which every cell is0.add_to_row(r, amount)addsamountto every cell of rowr. It returns nothing.add_to_col(c, amount)addsamountto every cell of columnc. It returns nothing.value_at(r, c)returns the value in rowrand columnc.row_sum(r)returns the sum of all values in rowr.
r and c are always inside the grid, and amount may be negative.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor and for the methods that return nothing.
Example 1
GridAdder(2, 3)
add_to_row(0, 5)
add_to_col(2, 1)
value_at(0, 2) returns 6
value_at(1, 2) returns 1
value_at(1, 0) returns 0
row_sum(0) returns 16
row_sum(1) returns 1
Row 0 becomes 5 5 5, then column 2 gets 1 more, so row 0 is 5 5 6 (sum 16) and row 1 is 0 0 1 (sum 1).
Constraints
1 <= rows, cols <= 8-50 <= amount <= 50- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Classes problems
- Guest Book SignaturesBeginner
- Odometer With a Trip MeterBeginner
- Panel of Light SwitchesBeginner
- Rectangle on the TableBeginner
- Scoreboard by RoundsBeginner
- Stack With a Size LimitBeginner
- Take a NumberBeginner
- Tally CounterBeginner