Mosaic matrix: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Python}}: Added a functionally composed version.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(36 intermediate revisions by 17 users not shown)
Line 1:
{{Draft task|Matrices}}
;Task:
Draw a 'mosaic' matrix which, for the purposes of this task, is a square matrix which has 1's in alternate cells (both horizontally and vertically) starting with a 1 in the top-left hand cell.
Line 9:
[http://keptarhely.eu/view.php?file=20220218v00xf1rfh.jpeg Mosaic Matrix - image]
<br>
 
===See also===
* [[Four sides of square]]
* [[Matrix with two diagonals]]
 
 
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">
V size = 9
L(Row) 0 .< size
L(Col) 0 .< size
I (Row % 2 == 1 & Col % 2 == 1) | (Row % 2 == 0 & Col % 2 == 0)
print(‘1’, end' ‘ ’)
E
print(‘0’, end' ‘ ’)
print()
</syntaxhighlight>
 
{{out}}
<pre>
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">
;;; draw a "mosaic matrix" - one with a 1 in the top-left and then
;;; alternating with another character vertically and horizontally
 
;;; draws a mosaic matrix with height and width = n
PROC drawMosaic( INT n )
CARD i, j, one
FOR i = 1 TO n DO
one = i AND 1
FOR j = 1 TO n DO
Put(' )
IF one = 1 THEN Put('1) ELSE Put('.) FI
one = ( one + 1 ) AND 1
OD
PutE()
OD
RETURN
 
PROC Main()
drawMosaic( 6 )
PutE()
drawMosaic( 7 )
RETURN
</syntaxhighlight>
{{out}}
<pre>
1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1
 
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io; use Ada.Text_Io;
with Ada.Command_Line;
 
Line 48 ⟶ 122:
Put_Line ("Usage: ./mosaic_matrix <side-length>");
 
end Mosaic_Matrix;</langsyntaxhighlight>
 
{{out}}
Line 68 ⟶ 142:
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
<lang algol68>BEGIN # draw a "mosaic matrix" - one with a 1 in the top-left and then #
BEGIN # draw a "mosaic matrix" - one with a 1 in the top-left and then #
# alternating with another character vertically and horiontally #
# horiontally #
# draws a mosaic matrix with height and width = n using "1" and "." #
PROC draw mosaic = ( INT n )VOID:
BEGINFOR i TO n DO
CHAR set BOOL one := "1"ODD i;
CHAR resetFOR =j ".";TO n DO
FOR i TO n DO print( ( " ", IF one THEN "1" ELSE "." FI ) );
CHAR cone := IFNOT ODD i THEN set ELSE reset FI;one
FOR j TO n DOOD;
print( ( " ", cnewline ) );
OD # draw mosaic # ;
c := IF c = set THEN reset ELSE set FI
OD;
print( ( newline ) )
OD
END # draw mosaic # ;
# test the draw mosaic procedure #
draw mosaic( 10 );
print( ( newline ) );
draw mosaic( 11 )
END</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 114 ⟶ 187:
1 . 1 . 1 . 1 . 1 . 1
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % draw a "mosaic matrix" - one with a 1 in the top-left and then %
% alternating with another character vertically and horiontally %
% horiontally %
% draws a mosaic matrix with height and width = n using "1" and "." %
procedure drawMosaic ( integer value n ) ;
for i := 1 until n do begin
logical one;
one := odd( i );
for j := 1 until n do begin
writeon( s_w := 0, if one then " 1" else " ." );
one := not one
end for_j ;
write()
end drawMosaic ;
% test the draw mosaic procedure %
drawMosaic( 6 );
write();
drawMosaic( 7 )
end.
</syntaxhighlight>
{{out}}
<pre>
1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1
 
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
print map 1..side 'y [
(equal? x%2 y%2)? -> 1 -> 0
]
]
 
drawSquare 6
print ""
drawSquare 9</syntaxhighlight>
 
{{out}}
 
<pre>1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
 
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">for i, v in [8, 9]
result .= "Matrix Size: " v "*" v "`n" matrix2txt(mosaicMatrix(v)) "`n"
MsgBox % result
return
 
mosaicMatrix(size){
M := []
loop % size {
row := A_Index
loop % size
M[row, A_Index] := (Toggle:=!Toggle) ? 1 : 0
, toggle := (A_Index = size && size/2 = floor(size/2)) ? !toggle : toggle
}
return M
}
 
matrix2txt(M){
for row , obj in M {
for col, v in obj
result .= M[row, col] " "
result .= "`n"
}
return result
}</syntaxhighlight>
{{out}}
<pre>Matrix Size: 8*8
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
 
Matrix Size: 9*9
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 </pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MOSAIC_MATRIX.AWK
BEGIN {
Line 130 ⟶ 322:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 148 ⟶ 340:
1 0 1 0 1 0 1
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Mosaic ← =⌜˜2|↕
 
Mosaic 6</syntaxhighlight>
{{out}}
<pre>┌─
╵ 1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
┘</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
void mosaicMatrix(unsigned int n) {
Line 171 ⟶ 377:
mosaicMatrix(11);
return 0;
}</langsyntaxhighlight>
 
{{out}}
<pre>
Line 200 ⟶ 405:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <concepts>
#include <iostream>
 
Line 230 ⟶ 435:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre> 1 0 1 0 1 0 1 0
Line 250 ⟶ 455:
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Another example of modular code and saving time by reusing subroutines and structures. Most of the code from is take from this previously worked example:
 
[[https://rosettacode.org/wiki/Matrix_with_two_diagonals#Delphi| Matrix with two Diagonals]]
 
<syntaxhighlight lang="Delphi">
{Define a matrix}
 
type TMatrix = array of array of double;
 
procedure DisplayMatrix(Memo: TMemo; Mat: TMatrix);
{Display specified matrix}
var X,Y: integer;
var S: string;
begin
S:='';
for Y:=0 to High(Mat) do
begin
S:=S+'[';
for X:=0 to High(Mat[0]) do
if X=0 then S:=S+Format('%0.0f',[Mat[X,Y]])
else S:=S+Format('%2.0f',[Mat[X,Y]]);
S:=S+']'+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
 
procedure ClearMatrix(var Mat: TMatrix; Value: double);
{Set all elements of the matrix to specified value}
var X,Y: integer;
begin
for Y:=0 to High(Mat) do
for X:=0 to High(Mat[0]) do Mat[X,Y]:=0;
end;
 
procedure SetMosaicMatrix(var Mat: TMatrix);
{Set matrix to mosaic pattern}
var X,Y: integer;
begin
{Set every other cell to one or zero}
for Y:=0 to High(Mat) do
for X:=0 to High(Mat) do Mat[X,Y]:=(X+Y+1) mod 2;
end;
 
 
procedure BuildMosaicMatrix(var Mat: TMatrix; Size: integer);
{Build a matrix with diagonals of the specified size }
var X,Y: integer;
begin
SetLength(Mat,Size,Size);
ClearMatrix(Mat,0);
SetMosaicMatrix(Mat);
end;
 
procedure BuildShowMosaicMatrix(Memo: TMemo; var Mat: TMatrix; Size: integer);
{Build and show a matrix of specific size}
begin
Memo.Lines.Add(Format('Matrix = %2d X %2d',[Size,Size]));
BuildMosaicMatrix(Mat,Size);
DisplayMatrix(Memo,Mat);
end;
 
 
procedure DisplayMosaicMatrices(Memo: TMemo);
{Build and display matrices of various sizes}
var Mat: TMatrix;
var I: integer;
begin
for I:=3 to 10 do BuildShowMosaicMatrix(Memo,Mat,I);
end;
 
</syntaxhighlight>
{{out}}
<pre>
Matrix = 3 X 3
[1 0 1]
[0 1 0]
[1 0 1]
 
Matrix = 4 X 4
[1 0 1 0]
[0 1 0 1]
[1 0 1 0]
[0 1 0 1]
 
Matrix = 5 X 5
[1 0 1 0 1]
[0 1 0 1 0]
[1 0 1 0 1]
[0 1 0 1 0]
[1 0 1 0 1]
 
Matrix = 6 X 6
[1 0 1 0 1 0]
[0 1 0 1 0 1]
[1 0 1 0 1 0]
[0 1 0 1 0 1]
[1 0 1 0 1 0]
[0 1 0 1 0 1]
 
Matrix = 7 X 7
[1 0 1 0 1 0 1]
[0 1 0 1 0 1 0]
[1 0 1 0 1 0 1]
[0 1 0 1 0 1 0]
[1 0 1 0 1 0 1]
[0 1 0 1 0 1 0]
[1 0 1 0 1 0 1]
 
Matrix = 8 X 8
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
 
Matrix = 9 X 9
[1 0 1 0 1 0 1 0 1]
[0 1 0 1 0 1 0 1 0]
[1 0 1 0 1 0 1 0 1]
[0 1 0 1 0 1 0 1 0]
[1 0 1 0 1 0 1 0 1]
[0 1 0 1 0 1 0 1 0]
[1 0 1 0 1 0 1 0 1]
[0 1 0 1 0 1 0 1 0]
[1 0 1 0 1 0 1 0 1]
 
Matrix = 10 X 10
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Mosaic matrix. Nigel Galloway: February 18th., 2022
let m11 m=Array2D.init m m (fun n g->if (n+g)%2=0 then 1 else 0)
printfn "%A\n\n%A" (m11 5) (m11 6)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 274 ⟶ 625:
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99}}
<syntaxhighlight lang="factor">USING: accessors colors kernel math math.matrices ui
ui.gadgets.grid-lines ui.gadgets.grids ui.gadgets.labels
ui.pens.solid ;
IN: mosaic
 
: <square> ( m n -- gadget )
+ 2 mod 0 = [
" 1 " <label> COLOR: AntiqueWhite2 <solid> >>interior
[ 50 >>size ] change-font
] [ "" <label> ] if ;
 
: <checkerboard> ( n -- gadget )
dup [ <square> ] <matrix-by-indices> <grid>
COLOR: gray <grid-lines> >>boundary ;
 
MAIN: [ 8 <checkerboard> "Mosaic" open-window ]</syntaxhighlight>
{{out}}
[[File:Factor-mosaic.png|thumb|center]]
 
=={{header|FreeBASIC}}==
===Text based===
<langsyntaxhighlight lang="freebasic">Sub mosaicMatrix(n As Integer)
For i As Integer = 1 To n
For j As Integer = 1 To n
Line 287 ⟶ 658:
 
mosaicMatrix(9)
Sleep</langsyntaxhighlight>
{{out}}
<pre>1 . 1 . 1 . 1 . 1
Line 300 ⟶ 671:
 
===Graphical===
<langsyntaxhighlight lang="freebasic">Dim As Integer n = 9, size = 60 * n + 70
Screenres size, size, 24
Cls
Line 319 ⟶ 690:
Next x
Bsave "mosaic_matrix.bmp",0
Sleep</langsyntaxhighlight>
{{out}}
https://www.dropbox.com/s/nbxrttz0j99usos/mosaic_matrix.bmp?dl=0
Line 325 ⟶ 696:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 346 ⟶ 717:
fmt.Println()
mosaicMatrix(8)
}</langsyntaxhighlight>
 
{{out}}
Line 368 ⟶ 739:
</pre>
 
=={{header|JHaskell}}==
<syntaxhighlight lang="haskell">import Data.Matrix (Matrix, matrix)
 
mosaic :: Int -> Matrix Int
Implementation:
mosaic n =
<lang J>mosq=:{{0=2|+/~i.y}}</lang>
matrix n n ((`rem` 2) . succ . uncurry (+))
 
main :: IO ()
Examples:
main = mapM_ (print . mosaic) [7, 8]</syntaxhighlight>
{{Out}}
<pre>┌ ┐
│ 1 0 1 0 1 0 1 │
│ 0 1 0 1 0 1 0 │
│ 1 0 1 0 1 0 1 │
│ 0 1 0 1 0 1 0 │
│ 1 0 1 0 1 0 1 │
│ 0 1 0 1 0 1 0 │
│ 1 0 1 0 1 0 1 │
└ ┘
┌ ┐
│ 1 0 1 0 1 0 1 0 │
│ 0 1 0 1 0 1 0 1 │
│ 1 0 1 0 1 0 1 0 │
│ 0 1 0 1 0 1 0 1 │
│ 1 0 1 0 1 0 1 0 │
│ 0 1 0 1 0 1 0 1 │
│ 1 0 1 0 1 0 1 0 │
│ 0 1 0 1 0 1 0 1 │
└ ┘</pre>
 
=={{header|J}}==
<lang J> mosq 4
Implementation:
<syntaxhighlight lang="j">mosq=: [: =/~ 2 | i.</syntaxhighlight>
Examples:
<syntaxhighlight lang="j"> mosq 4
1 0 1 0
0 1 0 1
Line 385 ⟶ 783:
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
// ------------------ MOSAIC MATRIX ------------------
 
// mosaic :: Int -> [[Int]]
const mosaic = n => matrix(n)(n)(
row => col => (1 + row + col) % 2
);
 
 
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () =>
// Matrices of dimensions 7 and 8.
[7, 8].map(
compose(
showMatrix(String),
mosaic
)
).join("\n\n");
 
 
// --------------------- GENERIC ---------------------
 
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
// A function defined by the right-to-left
// composition of all the functions in fs.
fs.reduce(
(f, g) => x => f(g(x)),
x => x
);
 
 
// matrix Int -> Int -> (Int -> Int -> a) -> [[a]]
const matrix = nRows => nCols =>
// A matrix of a given number of columns and rows,
// in which each value is a given function of its
// (zero-based) column and row indices.
f => Array.from({
length: nRows
}, (_, iRow) => Array.from({
length: nCols
}, (__, iCol) => f(iRow)(iCol)));
 
 
// showMatrix :: (a -> String) -> [[a]] -> String
const showMatrix = fShow =>
rows => 0 < rows.length ? (() => {
const w = fShow(Math.max(...rows.flat())).length;
 
return rows.map(
cells => cells.map(
x => fShow(x).padStart(w, " ")
).join(" ")
).join("\n");
})() : "";
 
// main
return main();
})();</syntaxhighlight>
{{Out}}
<pre>1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
 
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">def mosaicMatrix:
[range(0;.) | . % 2] as $one
| [range(0;.) | (. + 1) % 2] as $two
| [range(0;.) | if .%2 == 1 then $one else $two end];</langsyntaxhighlight>
'''Example''':
<langsyntaxhighlight lang="jq">def display:
map(join(" ")) | join("\n");
 
9|mosaicMatrix|display</langsyntaxhighlight>
{{out}}
<pre>
Line 413 ⟶ 892:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">julia> mosaicmat(n) = (m = n + iseven(n); reshape([Int(isodd(i)) for i in 1:m^2], m, m)[1:n, 1:n])
mosaicmat (generic function with 1 method)
 
Line 445 ⟶ 924:
0 1 0 1 0
1 0 1 0 1
</syntaxhighlight>
</lang>
 
=={{header|K}}==
K6
<syntaxhighlight lang="k">mosaic: {x=/:x}2!!:
 
mosaic 6</syntaxhighlight>
{{out}}
<pre>(1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1)</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a mosaic matrix */
mosaic(n):=genmatrix(lambda([i,j],charfun(evenp(abs(i-j)))),n,n)$
 
/* Alternative fumction */
altern_mosaic(n):=genmatrix(lambda([i,j],if evenp(abs(i-j)) then 1 else 0),n,n)$
 
/* Examples */
mosaic(3);
mosaic(4);
</syntaxhighlight>
{{out}}
<pre>
matrix(
[1, 0, 1],
[0, 1, 0],
[1, 0, 1]
)
matrix(
[1, 0, 1, 0],
[0, 1, 0, 1],
[1, 0, 1, 0],
[0, 1, 0, 1]
)
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">proc drawMosaicMatrix(side: Positive) =
var start = 1
for i in 0..<side:
var c = start
for j in 0..<side:
stdout.write c
c = 1 - c
echo()
start = 1 - start
 
echo "6x6 matrix:\n"
drawMosaicMatrix(6)
 
echo "\n7x7 matrix:\n"
drawMosaicMatrix(7)
</syntaxhighlight>
 
{{out}}
<pre>6x6 matrix:
 
101010
010101
101010
010101
101010
010101
 
7x7 matrix:
 
1010101
0101010
1010101
0101010
1010101
0101010
1010101
</pre>
 
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">program mosaicMatrix(output);
 
const
filledCell = '1';
emptyCell = '⋅';
 
procedure printMosaicMatrix(dimension: integer);
var
line: integer;
begin
{ NB: In Pascal, `for`-loop-limits are evaluated exactly once. }
for line := 1 to dimension do
begin
for dimension := 1 to dimension do
begin
{ `ord(odd(line))` is either zero or one. }
if odd(dimension + ord(odd(line))) then
begin
{ `write(emptyCell)` is shorthand for `write(output, emptyCell)`. }
write(emptyCell)
end
else
begin
write(filledCell)
end
end;
writeLn
end
end;
 
begin
printMosaicMatrix(9)
end.</syntaxhighlight>
{{out}}
1⋅1⋅1⋅1⋅1
⋅1⋅1⋅1⋅1⋅
1⋅1⋅1⋅1⋅1
⋅1⋅1⋅1⋅1⋅
1⋅1⋅1⋅1⋅1
⋅1⋅1⋅1⋅1⋅
1⋅1⋅1⋅1⋅1
⋅1⋅1⋅1⋅1⋅
1⋅1⋅1⋅1⋅1
 
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
 
my $n = 5;
say join ' ', @$_ for map { $_%2 ? [map { $_%2 ? 1 : 0 } 1..$n] : [map { $_%2 ? 0 : 1 } 1..$n] } 1..$n;</langsyntaxhighlight>
{{out}}
<pre>1 0 1 0 1
Line 463 ⟶ 1,067:
=={{header|Phix}}==
See [[Matrix_with_two_diagonals#GUI.2Fonline|Matrix_with_two_diagonals#Phix]] and press 'M'.
 
=={{header|Picat}}==
===Imperative===
<syntaxhighlight lang="picat">go =>
N = 6,
A = new_array(N,N),
bind_vars(A,"."),
foreach(I in 1..N)
foreach(J in 1..N, I mod 2 == J mod 2)
A[I,J] := "1"
end,
println(A[I].to_list.join(" "))
end.</syntaxhighlight>
 
{{out}}
<pre>1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1</pre>
 
===List comprehension===
<syntaxhighlight lang="picat">main =>
N=11,
[[((1+I^J)/\1).to_string:J in 1..N].join:I in 1..N].join("\n").println.</syntaxhighlight>
 
{{out}}
<pre>1 0 1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1</pre>
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* DRAW SOME MOSAIC MATRICES WITH ALTERNATING 1S and .S */
 
/* CP/M SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
 
/* TASK */
 
DRAW$MOSAIC: PROCEDURE( N );
DECLARE N BYTE;
DECLARE ( I, J, ONE ) BYTE;
DO I = 1 TO N;
ONE = I; /* EVEN NUMBERS ARE FALSE, ODD ARE TRUE IN PL/M */
DO J = 1 TO N;
CALL PR$CHAR( ' ' );
IF ONE THEN CALL PR$CHAR( '1' ); ELSE CALL PR$CHAR( '0' );
ONE = NOT ONE;
END;
CALL PR$NL;
END;
END DRAW$MOSAIC ;
 
CALL DRAW$MOSAIC( 6 );
CALL PR$NL;
CALL DRAW$MOSAIC( 7 );
 
EOF
</syntaxhighlight>
{{out}}
<pre>
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
 
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
</pre>
 
=={{header|Processing}}==
<syntaxhighlight lang="java">
//Aamrun, 27th June 2022
 
size(1000,1000);
 
textSize(50);
 
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
noFill();
square(i*100,j*100,100);
fill(#000000);
if((i+j)%2==0){
text("1",i*100+50,j*100+50);
}
else{
text("0",i*100+50,j*100+50);
}
}
}
</syntaxhighlight>
 
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">size = 9
for Row in range(size):
for Col in range(size):
Line 473 ⟶ 1,189:
else:
print("0", end=" ")
print()</langsyntaxhighlight>
Output:
<br>
Line 496 ⟶ 1,212:
===Functional===
No need for nested conditionals here.
<langsyntaxhighlight lang="python">'''Mosaic grid'''
 
 
# mosaic :: Int -> [[Int]]
def mosaic(n):
'''Grid of alternating ones and zeroes,
Line 541 ⟶ 1,258:
' '.join([str(x) for x in y]) for y in rows
])
 
 
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>1 0 1 0 1 0 1
Line 562 ⟶ 1,280:
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1</pre>
 
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ 1 & not ] is even ( n --> b )
 
[ dip [ temp put [] ]
temp share times
[ temp share split
dip
[ nested join ] ]
drop temp release ] is matrixify ( n [ --> [ )
 
[ dup 2dup * 1+ 1 >>
' [ 1 0 ] swap of
matrixify
swap even if
[ [] swap witheach
[ i even if
[ behead join ]
nested join ] ] ] is mosaic ( n --> [ )
 
8 mosaic
witheach
[ witheach [ echo sp ] cr ]
cr
9 mosaic
witheach
[ witheach [ echo sp ] cr ]</syntaxhighlight>
 
{{out}}
 
<pre>1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
 
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
</pre>
 
=={{header|Raku}}==
This isn't a matrix, especially if it is supposed to be graphical; it's a very small (or extremely low resolution) bitmap.
<syntaxhighlight lang="raku" perl6line>sub checker ($n) { (^$n).map: { 1 xx $n Z× (flat ($_ %% 2, $_ % 2) xx *) } }
 
.put for checker 7;
put '';
.put for checker 8;</langsyntaxhighlight>
{{out}}
<pre>1 0 1 0 1 0 1
Line 589 ⟶ 1,356:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red[]
 
mosaic: function [size][
Line 607 ⟶ 1,374:
]
 
mosaic 8</langsyntaxhighlight>
{{out}}
Similar to graphical Wren entry
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Mosaic matrix
# Date : 2022/18/02
Line 675 ⟶ 1,442:
next
next
</syntaxhighlight>
</lang>
Output image:
<br>
[http://keptarhely.eu/view.php?file=20220218v00xf1rfh.jpeg Mosaic Matrix]
 
=={{header|RPL}}==
'''Filling a matrix'''
≪ → n
≪ n DUP 2 →LIST 0 CON
1 n '''FOR''' r
1 n '''FOR''' c
r c 2 →LIST r c + 2 MOD NOT PUT
'''NEXT NEXT'''
≫ ≫ '<span style="color:blue">'''MOZTX'''</span>' STO
 
4 <span style="color:blue">'''MOZTX'''</span>
{{out}}
<pre>
1: [[ 1 0 1 0 ]
[ 0 1 0 1 ]
[ 1 0 1 0 ]
[ 0 1 0 1 ]]
</pre>
'''Filling the stack'''
« → n
« 1 n '''FOR''' j
j 2 MOD
2 n '''START''' DUP NOT '''NEXT'''
'''NEXT'''
n DUP 2 →LIST →ARRY
» » '<span style="color:blue">'''MOZTX'''</span>' STO
'''Direct approach'''
 
Latest RPL versions have a specific instruction to generate a matrix ruled by a formula.
≪ DUP 'NOT(I+J) MOD 2' LCXM →NUM ≫ '<span style="color:blue">'''MOZTX'''</span>' STO
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func mosaic_matrix(n) {
n.of {|k|
var(a,b) = (k.is_even ? (1,0) : (0,1))
n.of {|j| j.is_even ? a : b }
}
}
 
mosaic_matrix(5).each { .join(' ').say }; say ''
mosaic_matrix(6).each { .join(' ').say }</syntaxhighlight>
{{out}}
<pre>
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
 
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
</pre>
 
=={{header|Wren}}==
===Text based===
<langsyntaxhighlight ecmascriptlang="wren">var mosaicMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
Line 691 ⟶ 1,515:
}
 
mosaicMatrix.call(9)</langsyntaxhighlight>
 
{{out}}
Line 708 ⟶ 1,532:
{{libheader|DOME}}
{{libheader|Go-fonts}}
<langsyntaxhighlight ecmascriptlang="wren">import "dome" for Window
import "graphics" for Canvas, Color, Font, ImageData
 
Line 749 ⟶ 1,573:
}
 
var Game = Main.new(9)</langsyntaxhighlight>
 
{{out}}
Line 755 ⟶ 1,579:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc DrawMat(S);
int S, I, J;
[for I:= 0 to S-1 do
Line 765 ⟶ 1,589:
[DrawMat(6); CrLf(0);
DrawMat(7); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
9,476

edits