Hello! I was wondering if you can help me with Computer Science A.We has started
ID: 3684216 • Letter: H
Question
Hello! I was wondering if you can help me with Computer Science A.We has started 2D arrays and for each loops and I do not understand my homework. Here's a sample question: Which traversals increment all elements in the matrix by 1? a. for (int[][] row: matrix) for (int e: row) e++; b. for (int i = matrix.length-1; i >= 0; i--) for (int j = matrix[i].length-1; j >= 0; j--) matrix[i][j] += 1; c. for (int row = 0; row < matrix.length; row++) doSomething(matrix[row]); private static void doSomething(int[] x) for (int i = 0; x.length; i++) x[i] =1;
Explanation / Answer
The basic logic behind the increment 1 on each element in the array is
first read the array and incerment it by 1 or any value
below is snip in java
for(i = 0; i < n; i++)
{
a[i] = s.nextInt();
a[i]++;//increment by one
}
Here B is the correct answehe lemenet sin thr it reads the all the in the array 1 by 1 and incerment it by one.
for (int i = matrix.length-1; i >= 0; i--) // roeads up to length reaches to zero(row)
for (int j = matrix[i].length-1; j >= 0; j--)//roeads up to length reaches to zero(col)
matrix[i][j] += 1; //2-D indexing of element and increment by 1.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.