Hi,
something like this?
generateArray[k_?OddQ] := Table[ArrayPad[ConstantArray[1, m], {(k - m)/2, (k - m)/2}, 0], {m, 1, k, 2}]
generateArray[_] := "Invalid input!"
This generates:
generateArray[5]
(* {{0, 0, 1, 0, 0}, {0, 1, 1, 1, 0}, {1, 1, 1, 1, 1}}*)
and
generateArray[11]
(*{{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
0}, {0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0}, {0, 0, 1, 1, 1, 1, 1, 1, 1,
0, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1}}*)
For non-odd inputs it says:
generateArray[6]
(*"Invalid input!"*)
You can also plot this:
ArrayPlot[generateArray[101]]
A better way of doing it might be to use CellularAutomaton. Your particular problem obviously corresponds to the automaton with the rule 254 (see Stephen Wolfram's book).
RulePlot[CellularAutomaton[254]]
g[k_?OddQ] := CellularAutomaton[254, {{1}, 0}, Evaluate[(k - 1)/2]]
g[_] := "Invalid input"
Should also do it.
g[5]
(*{{0, 0, 1, 0, 0}, {0, 1, 1, 1, 0}, {1, 1, 1, 1, 1}}*)
and
g[7]
(*{{0, 0, 0, 1, 0, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 1, 1, 1}}*)
and
g[4]
(*"Invalid Input"*)
Hope this helps.
Cheers,
Marco
PS: Sorry the previous answer only showed up after I submitted mine....