diagonal value should be same like i=0 j=max_col-1; arr[i]([j=max_col-1-i]== arr[i+1][j-max_col-1-i]) for understanding purpose look at matrix example or you can search in google for diagnoal matrix.
In each row before(left side) diagonal value(column) value should be increment like arr[i][j-1] <arr[i][j].
In each row after(right side) diagonal value(column) value should be decrement like arr[i][j-1] > arr[i][j]
This is an interview Question, interviewer not explained properly to me, its took time to understand question properly.
In reply to Subbi Reddy:
I googled a diagonal matrix and from what I was able to comprehend, except the diagonal elements none of the other values with unidentical index values store zero. And if indeed that is what you are looking for, here is the code:
your code partially correct:
1. diagonal should be right to left and not left to right
2. diagonal value should be range between 1 to 100 any number i.e [i][j] == [i-1][j+1]
3. you forget before diagonal increment and after diagonal decrement in a row
4. please observe above matrix example and read my comments to get clarity.
I just don’t understand the increment from left and decrement from the right in your question! If it’s increasing from right, it is obv going to be in a decreasing order from the right, right?
No Need to generate the largest value in every row, please check result of second row diagonal value generated or not from your side that’s it i added comment above.
class matrix#(parameter size=3);
rand int arr[size][size];
//helper variable to fix daigonal element
rand int x;
constraint c0 {x inside {[1:100]};}
//if diagonal element then it should be x
constraint c1 {foreach(arr[i,j]) if(i+j==size-1) arr[i][j]==x;}
//if not a diagonal element
constraint c2 {foreach(arr[i,j]) if(i+j!=size-1) arr[i][j]==x+((i+j)-size+1);}
endclass
module top();
matrix#(5) m1;
initial begin
m1=new();
repeat(5) begin
m1.randomize();
foreach(m1.arr[i])
$display("%p",m1.arr[i]);
$display("///////////////////");
end
end
endmodule