# Unit 4C - Toggle Code Practice 4c 2D Arrays (4.11-4.13)

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

For each of the coding problems below, write your own solution.

## Exercise 1

Fix the errors (marked by comments) in the code so that it correctly creates a 10x10 array called `table` filled with numbers from 0 to 99 in left-right top-bottom order and prints the output (in row-column order) with the numbers separated by tabs. Most of the errors are syntactical, but one is logical.

> Open the `01-u4c-toggle1` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (1 test).

## Exercise 2

This program is supposed to create an 8x8 two-dimensional `int` array that is filled with a checkered patterns of 0s and 1s, starting with a 1 in the top left corner. It should also print the output in row-column order, separating each element with a space. The only missing part is the `if` statement that decides if a specific cell should be a 0 or a 1. Fill that in to finish the problem.

Hint: Try drawing out a smaller version (around 4x4) of a checkered two-dimensional array. Can you notice a pattern in the row numbers and column numbers of the 1s?

> Open the `02-u4c-toggle2` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (1 test).

## Exercise 3

The `sumVals` method in the below program should iterate through a two-dimensional `int` array `nums` and return its sum. Fill in the headers for the for loops such that the method iterates through the entirety of `nums`.

> Open the `03-u4c-toggle3` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (2 tests).

## Exercise 4

Fill in the `flipImage` method. This should accept a two-dimensional String array `image` and reverse each row from left to right. For example, `{ {"green", "red", "blue"}, {"cat", "dog", "yellow"} }` would become `{ {"blue", "red", "green"}, {"yellow", "dog", "cat"} }`.

> Open the `04-u4c-toggle4` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (1 test).

## Exercise 5

Write the `makeEvenNumsZero` method such that it iterates through the two-dimensional `int` array `nums` and replaces each instance of an even number with 0. For example, `{ {3, 4, 5}, {6, 7, 8} }` would become `{ {3, 0, 5}, {0, 7, 0} }`.

> Open the `05-u4c-toggle5` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (1 test).

## Exercise 6

Fill in the `numOccurrences` method. It should take in a two-dimensional `int` array `nums` and an `int` `desired` and return the number of times that `desired` appears in `nums`. E.g., with `{ {3, 1, 2}, {3, 4, 1} }` as `nums`, `numOccurrences(nums, 1)` should return `2`.

> Open the `06-u4c-toggle6` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (1 test).

## Exercise 7

Fill in the `averageCols` method. It should accept a two-dimensional int array `nums` and return a one-dimensional (normal) `int` array containing the integer average of each of the columns (NOT the rows). E.g., with `nums` as `{ {3, 5, 2}, {1, 1, 6} }`, `averageCols(nums)` should return `{2, 3, 4}` as that is `(3 + 1) / 2`, `(5 + 1) / 2`, and `(2 + 6) / 2`.

> Open the `07-u4c-toggle7` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (1 test).

## Exercise 8

Write the `oddRows` method. This should take in a two-dimensional `int` array `nums` and return a new two-dimensional `int` array containing only the odd-index rows. For example, with `nums` equal to `{ {3, 2, 1}, {4, 5, 6}, {1, 5, 7} }`, `oddRows(nums)` would return `{ {4, 5, 6} }` as that was the row at index 1, which is the only odd index present.

> Open the `08-u4c-toggle8` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (2 tests).

## Exercise 9

Write the `breakIntoLetters` method. It should accept a two-dimensional String array, in which each row contains the characters of a word. It should then return a single-dimensional (normal) String array containing the words in each row of the two-dimensional array. For example, calling `breakIntoLetters` on { {“b”, “a”, “t”, “h”}, {“t”, “e”, “n”, “s”}, {“j”, “a”, “c”, “k”}, {“l”, “a”, “z”, “y”} } should return {“bath”, “tens”, “jack”, “lazy”}.

> Open the `09-u4c-toggle9` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (2 tests).

## Exercise 10

Write the `maxEachRow` method. It should accept a two-dimensional `int` array `nums`, and return a single-dimensional (normal) `int` array containing the max of each row. For example, the returned array for { {3}, {4, 9, 6, -1}, {45, 1} } should be {3, 9, 45}.

> Open the `10-u4c-toggle10` folder and edit `Test1.java`. Press **Run** to try it; **Run Tests** checks it (2 tests).
