Explanation of Solution
Consider the given array declaration:
int[][] values = new int[ROWS][COLUMNS];
Fill all entries with “0”:
Java statements to fill all entries with “0” are as follows:
//Outer loop to refer rows of the array
for(int x = 0; x < ROWS; x++)
{
//Inner loop to refer columns of the array
for(int y = 0; y < COLUMNS; y++)
{
//Fill the array “values” with “0”
values[x][y] = 0;
}
}
Explanation:
In the above code,
- The outer “for” loop is used to refer the rows of the “values” array.
- The inner “for” loop is used to refer the columns of the “values” array.
- The statement “values[x][y] = 0;” is used to fill each element of the “values” array with “0”.
- The inner “for” loop is used to refer the columns of the “values” array.
Fill elements alternately with 0s and 1s in a checkerboard pattern:
Java statements to fill elements alternately with 0s and 1s in a checkerboard pattern are as follows:
//Outer loop to refer rows of the array
for(int x = 0; x < ROWS; x++)
{
//Inner loop to refer columns of the array
for(int y = 0; y < COLUMNS; y++)
{
//Fill the array “values” with “0”
values[x][y] = (x + y) % 2;
}
}
Explanation:
In the above code,
- The outer “for” loop is used to refer the rows of the “values” array.
- The inner “for” loop is used to refer the columns of the “values” array.
- The statement “values[x][y] = (x + y) % 2;” is used to fill elements of “values” array alternately with 0s and 1s in a checkerboard pattern.
- The inner “for” loop is used to refer the columns of the “values” array.
Fill only the elements at the top and bottom row with zeroes:
Java statements to fill only the elements at the top and bottom row with zeros are as follows:
//Outer loop to refer rows of the array
for(int y = 0; y < columns; y++)
{
//Assign top row with zeros
values[0][y] = 0;
//Assign bottom row with zeros
values[ROWS - 1][y] = 0;
}
Explanation:
In the above code,
- The “for” loop is used to refer the columns of the array...
Want to see the full answer?
Check out a sample textbook solutionChapter 6 Solutions
Big Java Late Objects
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education