#Making a diagonal using matrix

1 messages · Page 1 of 1 (latest)

vital seal
#

How can I start to make a matrix that is made up of diagonals of numbers

celest scaffoldBOT
#

<@&987246399047479336> please have a look, thanks.

celest scaffoldBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

vital seal
#

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

granite bison
#

u can use (i-j) to get the diagonal index and put the max of (i, j) in that diagonal

granite bison
#
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");
            }
celest scaffoldBOT
# granite bison ``` int n = get.nextInt(); int[][] mat = new int[n][n];...

Detected code, here are some useful tools:

Formatted code
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");
}
granite bison
#

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

eager dust
vital seal
#

I’m having a bit of trouble understanding it

granite bison
#

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

granite bison
#

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

granite bison
# granite bison

@vital seal try to solve this for practise on these questions of observation

granite bison
vital seal
#

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