Multi Dimentional Number of Array

multidimensional array java
multidimensional array javascript
multidimensional array in c++
multidimensional array php
multidimensional array c#
multidimensional array python
2d array
multidimensional array example

I have to make a 2 dimentional array that contain numbers iterate from 1 to height^2. the input is height and the output like this if the height is 5:

[ [ 21, 22, 23, 24, 25 ],
  [ 20, 19, 18, 17, 16 ],
  [ 11, 12, 13, 14, 15 ],
  [ 10, 9, 8, 7, 6 ],
  [ 1, 2, 3, 4, 5 ] ]

I've already code it like here:

function snakeLadder(height) {

  for (var i = 1; i <= height; i++) {
    var output = [];
    for (var j = 1; j < height; j++) {
      output.push(i + j);
    }
  }
  return output;
}

and the output turns out to be like this :

[ 6, 7, 8, 9, 10 ]
[ 4, 5, 6 ]
[ 3, 4 ]

can anyone pinpoint the problem in my code?

function snakeLadder(height) {
  var result = []
  for(var i = height-1; i >= 0; i--) {
    var row = [];
    for(var j = 1; j <= height; j++) {
      // if it's odd rows, increase(push), otherwise decrease(unshift)
      if((height-i-1)%2 == 0)
        row.push(height*i + j);
      else
        row.unshift(height*i + j);
    }
    result.push(row);
  }
  return result;
}
console.log(snakeLadder(5));

A multidimensional array in MATLAB® is an array with more than two dimensions​. To access elements in a multidimensional array, use integer subscripts just  Multi-Dimensional Arrays in C++ arrays are used to store the data in the form of a table of rows and columns. Here we can create single or multidimensional arrays to hold values in different scenarios. Single dimensional has one dimension whereas, a multidimensional array can be 2 dimensional, 3 dimensional, etc.

You need an outer array where you unshift the row.

Then you better loop from zero to less than height and take a multiplication with the height and an offset of one to get the right value.

function snakeLadder(height) {
    var temp, result = []
    for (var i = 0; i < height; i++) {
        temp = [];
        for (var j = 0; j < height; j++) {
            temp.push(i * height + j + 1);
        }
        if (i & 1) {
            temp.reverse();
        }
        result.unshift(temp);
    }
    return result;
}

console.log(snakeLadder(5).map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }

A two-dimensional array is really nothing more than an array of arrays (a A matrix can be thought of as a grid of numbers, arranged in rows and columns, kind  The simplest form of the multidimensional array is the 2-dimensional array. A 2-dimensional array is a list of one-dimensional arrays. A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns. Following is a 2-dimensional array, which contains 3 rows and 4 columns −

The primary issue with your code is your rows aren't being added to a final, result array. Secondarily, you'll need to reverse the odd rows to match your expected output.

Here's a succinct ES6 version which uses the ... spread operator on Array constructors to create rows and columns, which are mapped to the two-dimensional result array. Parity is tested on row index j using the binary & 1 to reverse rows where the least significant bit is set:

const snakeLadder = (h, i=h*h) => 
  [...Array(h)].map((r, j) => {
    const row = [...Array(h)].map(c => i--)
    return j & 1 ? row : row.reverse();
  })
;

console.log(snakeLadder(5));

In the activities array, the first dimension represents the activity and the second one shows the number of hours spent per day for each. To show the activities array  Tables: It will decide the number of tables an array can accept. Two Dimensional Array is always a single table with rows and columns. In contrast, Multi Dimensional array in Java is more than 1 table with rows and columns. Row_Size: Please specify the number of Row elements an array can store. For example, Row_Size =5, then the array will have

Try this:

  for (var i = 0; i < height; i++) {
      var output = [];
          if((i+2)%2 ==0){
                for (var j = 1; j <=height; j++) {
                          output.push( (i * height ) + j );
                }
          }
          else if( (i%2) ==1){
                 for (var j = height; j>0; j--) {
                           output.push( (i * height) + j );
                }
          }
     }
            return output;
    }

If the numbering does not start at 0, the constant B may not be the address of any element. Multidimensional arrays[edit]. Multidimensional Arrays As the name indicates, multi dimensional arrays are arrays that contain more than one dimension, usually two or three but it can have up to 32 dimensions. A multi array works like a matrix with various levels, take in example a comparison between one, two, and three Dimensions.

The dimension of an array indicates the number of indices you need to select an element. For a two-dimensional array you need two indices to select an element​  Arrays can have more than one dimension. For example, the following declaration creates a two-dimensional array of four rows and two columns. The following declaration creates an array of three dimensions, 4, 2, and 3.

In this tutorial, you will learn to work with multidimensional arrays (two-​dimensional and three-dimensional arrays) in C programming with the help of examples. Similarly, a multi-dimensional array in Java usually has 3 or more defined indexes, but in reality, one particular row of elements have another multitude of elements defined on their names. Basically, you can have a 3×3 or a bigger matrix of elements to define a multi-dimensional array.

In this tutorial, we will learn about the Java multidimensional array using 2-​dimensional arrays and 3-dimensional arrays with the help of examples. A multidimensional array is an array containing one or more arrays. The dimension of an array indicates the number of indices you need to select an element. For a two-dimensional array you need two indices to select an element For a three-dimensional array you need three indices to select an element

Comments
  • I've try the code and it works wonder for height 5. However, I've found that if the height is 3, the output do not format correctly even though the array is ok. the output is like this : [ [ 7, 8, 9 ], [ 6, 5, 4 ], [ 1, 2, 3 ] ] where it should be 3 x 3 grid like height 5 output. any ideas?
  • @AdeKurniawan the output do not format correctly? could you explain more please?
  • i want it to be like chess board which is the output will be show like a grid. it's ok with height 5, but not with height 3. After the comma, the next array do not place on the next line but instead on the same line. here is the picture for more detail. One is what it should be and the other is the current result. pictr.com/image/01IphG pictr.com/image/01I1xc
  • output format does not make any difference for computer. what is this format for? human?
  • the result is not the same as in the question
  • The output is different.
  • Good catch, fixed.
  • this is the output after I tried your code: [ 21, 22, 23, 24, 25 ] [ 7, 8, 9 ] [ 4, 3 ]
  • Fixed issues.Output shows correctly for me. Is it what you expected now?