This is the code my teacher gave us. We have to draw a 2D array of v.
int[][] b = { {1, 2, 3, 4},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0} };
int[][] v = new int[2][4];
int row = 0;
for (int i = 0; i < b.length; i++)
{
for (int j = 0; j < b[i].length; j++)
v[row][j] += b[i][j];
row = (row + 1) % 2;
}
First of all I created a 2D array with 2 rows and then 4 columns for each row. I don´t really understand these 2 lines:
v[row][j] += b[i][j];
row = (row + 1) % 2;
This is the answer . Why is it?
v[row][j] += b[i][j];
stands for v[row][j] =v[row][j] + b[i][j];
row = (row + 1) % 2;
means divide row+1 by 2 and assign the remainder to row
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.