1.The code box below includes a live 2 dimensional array variable called gridNum
ID: 3858715 • Letter: 1
Question
1.The code box below includes a live 2 dimensional array variable called gridNums. This array holds integers. You cannot see its declaration or initialization.
What value is in the zeroth (initial) position of the second row? Print this array entry to the console. (Note: remember that the second row is actually row 1). Enter your code in the box below.
2.The code box below includes a live 3x3 2-dimensional array variable called fredsNums. This array holds integers. You cannot see its declaration or initialization. What value is in the southwest corner of the array? Write a statement (or statements) that prints this value to the console.
3.The code box below contains a live 2D array of integers called someInts. This array has a rectangular shape. Write one or several lines of code that determines and then prints to the console the number of columns in the array.
4.The code box below contains a live 2D rectangular array of characters (char) called someChars. Write a looping statement that allows you to calculate, and then print to the console, the number of entries in the array that are (lower case) 'x'.
For example, if the array looks like this:
then your code should print 3 to the console.
The answer you enter should execute exactly one System.out.println statement.
Explanation / Answer
Answer:
1. Assuming, the 2D array gridNums is a 2-Dimensional array of integers, the code snippet to print the integer at zeroth position of second row is as follows:
System.out.println(gridNums[1][0] + "");
2. Statement to print the southwest element of 3X3 2-dimensional array fredsNums:
System.out.println(fredsNums[2][0] + "");
3. Statements to print no of columns in a rectangular matrix - someInts :
//someInts[0] represents the first row of the matrix
int noOfColumns = someInts[0].length;
System.out.println(noOfColumns + """);
4. Statements to print the no of entries in array someChars, that are lower case 'x':
int noOfRows = someChars.length;
int noOfColumns = someChars[0].length;
for(int i=0; i<noOfRows; i++)
{
for(int j=0; j<noOfColumns; j++)
{
if(someChars[i][j] == 'x')
count++;
}
}
System.out.println(count + "");
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.