#Making a diagonal using matrix
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If your code is long, or you have multiple files to share, consider posting it on sites like https://pastebin.com/ and share the link instead, that is easier to browse for helpers.
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
an example of what the program should be able to do
it takes in a user inputted number
and then makes a diagonal nxn and starts at 0 diagonal with the numbers adjacent to both sides of it
also a diagonal but increasing by one
here is one more example
if input was 2
output should look like that
u can use (i-j) to get the diagonal index and put the max of (i, j) in that diagonal
int n = get.nextInt();
int[][] mat = new int[n][n];
for(int gap=-1*n; gap<=n; gap++)
{
int i=0, j=0;
if(gap < 0) {
i = -1*gap;
j = 0;
}
else {
i = 0;
j = gap;
}
int val = Math.max(i, j);
while(i<n && j<n)
{
mat[i][j] = val;
i++;
j++;
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
put.write(mat[i][j]+" ");
}
put.write("\n");
}
Detected code, here are some useful tools:
int n = get.nextInt();
int [] [] mat = new int [n] [n] ;
for (int gap = - 1 * n; gap <= n; gap++) {
int i = 0, j = 0;
if (gap < 0) {
i = - 1 * gap;
j = 0;
}
else {
i = 0;
j = gap;
}
int val = Math.max(i, j);
while (i < n && j < n) {
mat[i] [j] = val;
i++;
j++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
put.write(mat[i] [j] + " ");
}
put.write("\n");
}
ig, this is it, any problem, ping me
@vital seal
well try more, if not then only, see code : D
you should be able to do it. JUST DRY RUN
please try to avoid spoon feeding, the idea is to direct them to a feasible solution 🙂
Could you explain this?
I’m having a bit of trouble understanding it
well, try to dry run, u will get this
there are many ways to print this
this above program is printing them diagonally, so it looks good but there are many approaches, some simple ones too
for this example, what we see?
in row 0, 0 is present in col 0, right?
in row 1, 0 is present in col 1?
can i say for any row=i, 0 is present on col=i ?
@vital seal
dont look above code, i am explaining u a different approach to see, when looks on test case
well i guess u can see this, like it follows in every
so we know the location of 0
loop from 0 th position towards right, incrementing by one
loop from 0 th position towards left incrementing by one
for every row, this will give the same solution, u need
u can also think column-wise too
this is an OBSERVATION problem only
for the above code, if u want to understand that, i will explain, but try to program it this way
for practise
try to print this one
@vital seal try to solve this for practise on these questions of observation
and i am really sorry for that, will keep in mind ( SAY NO TO SHARE CODE ) 
Ohh
I do see the pattern now
I prob could’ve done this
Ty for your help
I’ll attempt ur example
If I can
Many Pattern Problems :- https://www.pepcoding.com/resources/online-java-foundation/patterns