Unit 8 Write Code for Toggle Code¶
This is the write code problems associated with the mixed up code problems.
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.
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?
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
.
Fill in the flipImage
method. This should accept a two-dimensional String array image
and flip the “image” 180 degrees vertically. For example, { {"green", "red", "blue"}, {"cat", "dog", "yellow"} }
would become { {"blue", "red", "green"}, {"yellow", "dog", "cat"} }
.
Write the makeEvenNumbersZero
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} }
.
Fill in the numOccurrences
method. It should take in a two-dimension 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
.
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
.
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.
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”}.
Write the maxEachRow
method. It that 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}.