Skip to main content
Logo image

Section 12.15 Unit 4c 2D-Arrays Write Code for Toggle Code

90 minutes
This is the 2D-Arrays write code problems associated with the mixed up code problems.

Activity 12.15.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.

Activity 12.15.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?

Activity 12.15.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.

Activity 12.15.4.

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"} }.

Activity 12.15.5.

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} }.

Activity 12.15.6.

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.

Activity 12.15.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.

Activity 12.15.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.

Activity 12.15.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”}.

Activity 12.15.10.

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}.
You have attempted of activities on this page.