I am trying to access column elements in a 2D array.
I tried using “foreach” construct but it does not work.
My goal is to traverse the column and see if all bits are 1 or all bits are 0.
This is the logic.
//column traverse
int matrix[3][3] = '{'{1,0,1},'{0,1,1},'{0,0,1}};
int res_1[$],res_0[$];
foreach(matrix[,j]) begin
res_1 = matrix[j].find(x) with (x == 1);
if (res_1.size == 3)
return 1;
res_0 = matrix[j].find(y) with (y == 0);
if (res_0.size == 3)
return 0;
end
//row traverse is working fine
foreach(matrix[i]) begin
res_1 = matrix[i].find(x) with (x == 1);
if (res_1.size == 3)
return 1;
res_0 = matrix[i].find(y) with (y == 0);
if (res_0.size == 3)
return 0;
end
Please use code tags making your code easier to read. I have added them for you.
Did you mean to declare a 2D array of bit and not int? And you probably want to return something other than 0 or 1 if neither condition is met.
Within the body of the foreach loop, it has no idea what I and k are iterating over. matrix[k] is still a select of the first dimension. You might want to use the array reductions operators.
foreach(matrix[,k]) begin
if(matrix.and(col) with (matrix[col.index][k] == 1)) return 1;
if(matrix.and(col) with (matrix[col.index][k] == 0)) return 0;
end
return -1;
foreach(matrix[i]) begin
if (matrix[i].and(row) with (row == 1)) return 1;
if (matrix[i].and(row) with (row == 0)) return 0;
end
return -1;
(Actual contents of “matrix_transpose” left out for brevity - as I said it could be a function or even a module instance.)
I’m wondering if there’s better ways to do these sorts of things in the language…
Could you please explain what below lines do? I checked the array reduction methods but I am having hard time to understand array reduction methods when used with ‘with’ clause. Also col.index?
matrix.and(col) with (matrix[col.index][k] == 1)
matrix.and(col) with (matrix[col.index][k] == 0)
I tried to run above code but could not make sense of the output.
bit arr[5] = {1,0,1,0,1};
arr.and() // and operation of all the element of arr , ans = 1 & 0 & 1 & 0 & 1
arr.and(x) with (x) //This is same as above, here x represent the element , ans = 1 & 0 & 1 & 0 & 1
arr.and(x) with (x.index) //and operation of all the index, x represent the element , x.index represent the index of that element , ans = 0 & 1 & 2 & 3 & 4
arr.and(x) with (x.index > 2) // and operation of all the index higher than 2, ans = 3 & 4
bit matrix[3][3] = '{'{1,0,1},'{0,1,1},'{0,0,1}};
matrix.and(col) with (matrix[col.index][k] == 1)
matrix.and(col) // Here col is 1st dimension values of matrix
// col = {1,0,1} when matrix[0] -> here col.index = 0
// col = {0,1,1} when matrix[1] -> here col.index = 1
// col = {0,0,1} when matrix[2] -> here col.index = 2
matrix[col.index][k] //lets assume k = 0;
//matrix[col.index][0] -> col.index= 0 -> matrix[0][0] = 1
//matrix[col.index][0] -> col.index= 1 -> matrix[1][0] = 0
//matrix[col.index][0] -> col.index= 2 -> matrix[2][0] = 0
(matrix[col.index][k] == 1)
// {1==1, 0==1, 0==1}
matrix.and(col) with (matrix[col.index][k] == 1)
// and of {1==1, 0==1, 0==1}
// and of {1, 0, 0} which is 0