# Multidimensional array

This is a code-golf, so keep your code small!

The objective is to create a function that receives 3 arguments:

• Rows(int): The max number of an item in each array;

• Cols(int): The max number of dimensions (sub-arrays);

• Val(*): The starting value in each item;

This function should return a multidimensional array with the proper size and with the items containing the value specified.

It should be able to return any number of dimensions.

### Example:

var multiArray = createMultiArray(2, 4,'empty');

multiArray[0][0][0][0] === 'empty' //true
multiArray[0][0][0][1] === 'empty' //true
multiArray[0][0][0][2] === 'empty' //true
multiArray[0][0][1][0] === 'empty' //true
...
multiArray[2][2][2][2] === 'empty' //true

• I'd recommend at the very least a different challenge type than a popularity-contest. I'm having a tough time thinking what would make a recursive array initialization exercise popular. code-golf would be better. Apr 4, 2014 at 14:23
• This feels homework-y, and even if I'm wrong it's not a particularly interesting challenge. Apr 4, 2014 at 14:23
• No reason to freak out. The point they are making is that this is quite a simple question which doesn't leave a lot of flexibility in the solutions (especially if you are prescribing recursive approachs), and hence it not very well suited for popularity contests. Also even if you leave it as a popularity contest, there is no incentive to keep the code small. So you may want to think about changing the winning criterion or lifting the recursion constraint to allow for creative approaches. Apr 4, 2014 at 14:35
• @rafaelcastrocouto Please don't take it personally. Just look at these comments as recommendations for asking a better question, they are not personal attacks. I see this is your first question attempt -- that makes you braver than I, and kudos for that, but there's no shame in taking the suggestions you have been giving, acting on them, and coming up with a better question. Apr 4, 2014 at 14:39
• "Informative" is subjective. If you want useful, clearly written ways to do things, a general QA site might be more appropriate than a puzzle site. Apr 4, 2014 at 14:45

## GolfScript

This is pretty trivial, so there's no need to over-complicate it:

{[1$({2$@(@A}{\;}if]*}:A;


Online demo

• @ProgramFOX, until they add syntax highlighting for GolfScript, I think it's best to leave it unhighlit. I don't know any other language with the same tokens. Apr 4, 2014 at 14:53
• @ProgramFOX You can even see in this short example that the @ is coloured differently in two places as is the A. Apr 4, 2014 at 14:57
• Ok, I'll keep it in mind. Apr 4, 2014 at 14:57
• Where is the byte count? Jul 31, 2017 at 22:43
• @Zacharý, when I wrote the answer it wasn't golf. Aug 1, 2017 at 7:22

### GolfScript, 17 characters

A non-recursive approach in GolfScript.

{\{\[.;]*}+@*}:A;


Online demo

## JavaScript, 132 characters

function m(e,t,n){var r=0,i=[];var s=function(i){r++;for(var o=0;o<=e;o++){if(r==t)i[o]=n;else i[o]=s([])}r--;return i};return s(i)}

UnGolfed:
function multiArray(maxRows, maxCols, val){
var c = 0, farray = [];
var recursive = function(array){
c++;
for(var r = 0; r <= maxRows; r++){
if(c == maxCols) array[r] = val;
else array[r] = recursive([]);
}
c--;
return array;
};
return recursive(farray);
}


## Mathematica, 32 bytes

Within reach of GolfScript for once! Scrap that, I didn't see Howard's answer.

f=ConstantArray[#3,#+0Range@#2]&


## APL, 19 bytes

{(¯1↑⍵)⍴⍨⊃(//)⌽2↑⍵}


Sample:

    {(¯1↑⍵)⍴⍨⊃(//)⌽2↑⍵} 2 4 'empty'
┌─────┬─────┐
│empty│empty│
├─────┼─────┤
│empty│empty│
└─────┴─────┘
┌─────┬─────┐
│empty│empty│
├─────┼─────┤
│empty│empty│
└─────┴─────┘

┌─────┬─────┐
│empty│empty│
├─────┼─────┤
│empty│empty│
└─────┴─────┘
┌─────┬─────┐
│empty│empty│
├─────┼─────┤
│empty│empty│
└─────┴─────┘

• Unneeded commute, you can change it to{(⊃(//)⌽2↑⍵)⍴¯1↑⍵}, and maybe even {(⊃(//)⌽2↑⍵)⍴⊃⌽⍵} Jul 31, 2017 at 22:42
• The commute is unneeded (and its absence would save us one character), but the second suggestion is wrong: "minus one take" returns a one element vector, whereas "first reverse" opens the box. Try it online. Aug 2, 2017 at 9:21
• Yeah, I don't do well with stuff like that in APL (which is probably bad). Aug 2, 2017 at 23:54

# Python 3, 36 bytes

lambda a,b,x:eval("["*b+"x"+"]*a"*b)


Try it online!

# J, 8 bytes

$/@]$<@[


Try it online!