Mosaic matrix
- 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.
Other cells can either be left blank or filled with some other character.
If you can please use GUI
Mosaic Matrix - image
See also
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()
- Output:
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
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
- Output:
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
Ada
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Command_Line;
procedure Mosaic_Matrix is
type Matrix_Type is array (Positive range <>, Positive range <>) of Character;
function Mosaic (Length : Natural) return Matrix_Type is
begin
return M : Matrix_Type (1 .. Length, 1 .. Length) do
for Row in M'Range (1) loop
for Col in M'Range (2) loop
M (Row, Col) := (if (Row + Col) mod 2 = 0
then '1' else '.');
end loop;
end loop;
end return;
end Mosaic;
procedure Put (M : Matrix_Type) is
begin
for Row in M'Range (1) loop
for Col in M'Range (2) loop
Put (' ');
Put (M (Row, Col));
end loop;
New_Line;
end loop;
end Put;
begin
Put (Mosaic (Length => Natural'Value (Ada.Command_Line.Argument (1))));
exception
when others =>
Put_Line ("Usage: ./mosaic_matrix <side-length>");
end Mosaic_Matrix;
- Output:
$ ./mosaic_matrix 4 1 . 1 . . 1 . 1 1 . 1 . . 1 . 1 $ ./mosaic_matrix 2 1 . . 1 $ ./mosaic_matrix 1 1 $ ./mosaic_matrix 0 $ ./mosaic_matrix Usage: ./mosaic_matrix <side-length>
ALGOL 68
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:
FOR i TO n DO
BOOL one := ODD i;
FOR j TO n DO
print( ( " ", IF one THEN "1" ELSE "." FI ) );
one := NOT one
OD;
print( ( newline ) )
OD # draw mosaic # ;
# test the draw mosaic procedure #
draw mosaic( 10 );
print( ( newline ) );
draw mosaic( 11 )
END
- Output:
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 . 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 . 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
ALGOL W
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.
- Output:
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
Arturo
drawSquare: function [side][
loop 1..side 'x ->
print map 1..side 'y [
(equal? x%2 y%2)? -> 1 -> 0
]
]
drawSquare 6
print ""
drawSquare 9
- Output:
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
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
}
- Output:
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
AWK
# syntax: GAWK -f MOSAIC_MATRIX.AWK
BEGIN {
for (n=6; n<=7; n++) {
for (i=1; i<=n; i++) {
for (j=1; j<=n; j++) {
tmp = ((i+j) % 2 == 0) ? 1 : 0
printf("%2d",tmp)
}
printf("\n")
}
print("")
}
exit(0)
}
- Output:
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
BQN
Mosaic ← =⌜˜2|↕
Mosaic 6
- Output:
┌─ ╵ 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 ┘
C
#include <stdio.h>
void mosaicMatrix(unsigned int n) {
int i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if ((i + j) % 2 == 0) {
printf("%s ", "1");
} else {
printf("%s ", "0");
}
}
printf("\n");
}
}
int main() {
mosaicMatrix(10);
printf("\n");
mosaicMatrix(11);
return 0;
}
- Output:
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 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
C++
#include <concepts>
#include <iostream>
// Print each element of a matrix according to a predicate. It
// will print a '1' if the predicate function is true, otherwise '0'.
void PrintMatrix(std::predicate<int, int, int> auto f, int size)
{
for(int y = 0; y < size; y++)
{
for(int x = 0; x < size; x++)
{
std::cout << " " << f(x, y, size);
}
std::cout << "\n";
}
std::cout << "\n";
}
int main()
{
// a lambda to create a mosaic
auto mosaic = [](int x, int y, [[maybe_unused]]int size)
{
return (x + y) % 2 == 0;
};
PrintMatrix(mosaic, 8);
PrintMatrix(mosaic, 9);
}
- Output:
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
Delphi
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:
{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;
- Output:
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]
EasyLang
proc mosaic side . .
start = 1
for i to side
c = start
for j to side
write c & " "
c = 1 - c
.
print ""
start = 1 - start
.
.
mosaic 6
- Output:
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
F#
// 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)
- Output:
[[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]]
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 ]
- Output:
FreeBASIC
Text based
Sub mosaicMatrix(n As Integer)
For i As Integer = 1 To n
For j As Integer = 1 To n
Print Iif((i + j) Mod 2 = 0, "1 ", ". ");
Next j
Print
Next i
End Sub
mosaicMatrix(9)
Sleep
- Output:
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
Graphical
Dim As Integer n = 9, size = 60 * n + 70
Screenres size, size, 24
Cls
Windowtitle "Mosaic matrix"
Dim As Integer beige = Rgb(245, 245, 220), brown = Rgb(171, 82, 54)
For x As Integer = 0 To n
For y As Integer = 0 To n
Dim As Integer cx = x*60 + 10
Dim As Integer cy = y*60 + 10
If (x + y) Mod 2 = 0 Then
Line (cx,cy) - (cx+50, cy+50), brown, BF
Draw String (cx + 20, cy + 20), "1", beige
Else
Line (cx,cy) - (cx+50, cy+50), beige, BF
End If
Next y
Next x
Bsave "mosaic_matrix.bmp",0
Sleep
- Output:
https://www.dropbox.com/s/nbxrttz0j99usos/mosaic_matrix.bmp?dl=0
FutureBasic
Text based
Courtesy Jay
void local fn MosaicMatrix( n as unsigned int )
NSInteger i, j
for i = 0 to n - 1
for j = 1 to n // So that grid will start with 1
print " ";(i+j) and 1;
next
print
next
end fn
fn MosaicMatrix( 11 )
HandleEvents
- Output:
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
Graphical
Short version courtesy Jay
void local fn MosaicMatrix( n as unsigned int )
window 1, @"Mosaic Matrix Graphic", (0, 0, n*21 + 40, n*21 + 40)
NSInteger x , y
text @"Arial bold", 16
for y = 20 to n * 21 step 21
for x = 21 to n * 21 step 21
rect fill (x, y, 20, 20), fn colorYellow
print %(x + 5, y + 2 )((x + y) and 1)
next
next
end fn
fn MosaicMatrix( 15 )
HandleEvents
Longer graphical version
_window = 1
begin globals
short gValue
end globals
void local fn MosaicMatrixGraphic( gridRectSize as int, gridSize as int )
CFStringRef valueStr
int i = 1, wndStyle
wndStyle = NSWindowStyleMaskTitled
wndStyle += NSWindowStyleMaskClosable
wndStyle += NSWindowStyleMaskMiniaturizable
CGRect r = fn CGRectMake( 0, 0, 480, 500 )
window _window, @"Mosaic Matrix Graphic", r, wndStyle
gValue = 1
r = fn CGRectMake( gridRectSize, 420, gridRectSize, gridRectSize )
for i = 1 to gridSize * gridSize
if gValue = 1 then valueStr = @"1" : gValue = 0 else valueStr = @"0" : gValue = 1
textlabel i, valueStr, r, _window
TextFieldSetBordered( i, YES )
ControlSetAlignment( i, NSTextAlignmentCenter )
ControlSetFontWithName( i, @"Times", (double)gridRectSize - 10 )
TextFieldSetBackgroundColor( i, fn ColorWithRGB( 0.996, 1.000, 0.111, 1.0 ) )
TextFieldSetTextColor( i, fn ColorBlack )
TextFieldSetDrawsBackground( i, YES )
r = fn CGRectOffset( r, gridRectSize, 0 )
if ( i mod gridSize == 0 )
r = fn CGRectMake( gridRectSize, r.origin.y - gridRectSize, gridRectSize, gridRectSize )
if gValue = 1 then gValue = 0 else gValue = 1 : continue
end if
next
end fn
void local fn DoDialog( ev as long, tag as long, wnd as long )
select ( ev )
case _windowWillClose : end
end select
end fn
on dialog fn DoDialog
fn MosaicMatrixGraphic( 40, 10 )
HandleEvents
Go
package main
import "fmt"
func mosaicMatrix(n uint) {
for i := uint(0); i < n; i++ {
for j := uint(0); j < n; j++ {
if (i+j)%2 == 0 {
fmt.Printf("%s ", "1")
} else {
fmt.Printf("%s ", ".")
}
}
fmt.Println()
}
}
func main() {
mosaicMatrix(7)
fmt.Println()
mosaicMatrix(8)
}
- Output:
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 . 1 . 1 . . 1 . 1 . 1 . 1 1 . 1 . 1 . 1 . . 1 . 1 . 1 . 1
Haskell
import Data.Matrix (Matrix, matrix)
mosaic :: Int -> Matrix Int
mosaic n =
matrix n n ((`rem` 2) . succ . uncurry (+))
main :: IO ()
main = mapM_ (print . mosaic) [7, 8]
- Output:
┌ ┐ │ 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 │ └ ┘
J
Implementation:
mosq=: [: =/~ 2 | i.
Examples:
mosq 4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
mosq 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
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();
})();
- Output:
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
jq
Works with gojq, the Go implementation of jq
def mosaicMatrix:
[range(0;.) | . % 2] as $one
| [range(0;.) | (. + 1) % 2] as $two
| [range(0;.) | if .%2 == 1 then $one else $two end];
Example:
def display:
map(join(" ")) | join("\n");
9|mosaicMatrix|display
- Output:
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
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)
julia> mosaicmat(1)
1×1 Matrix{Int64}:
1
julia> mosaicmat(2)
2×2 Matrix{Int64}:
1 0
0 1
julia> mosaicmat(3)
3×3 Matrix{Int64}:
1 0 1
0 1 0
1 0 1
julia> mosaicmat(4)
4×4 Matrix{Int64}:
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
julia> mosaicmat(5)
5×5 Matrix{Int64}:
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
K
K6
mosaic: {x=/:x}2!!:
mosaic 6
- Output:
(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)
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);
- Output:
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] )
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)
- Output:
6x6 matrix: 101010 010101 101010 010101 101010 010101 7x7 matrix: 1010101 0101010 1010101 0101010 1010101 0101010 1010101
Nu
def mosaic [n] {
0..<$n | each {|i| seq 1 $n | bits xor $i | bits and 1 | str join ' ' } | str join "\n"
}
..5 | each { { k: $in v: (mosaic $in) } } | transpose -r
- Output:
╭───┬───┬───┬─────┬───────┬─────────┬───────────╮ │ # │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ ├───┼───┼───┼─────┼───────┼─────────┼───────────┤ │ 0 │ │ 1 │ 1 0 │ 1 0 1 │ 1 0 1 0 │ 1 0 1 0 1 │ │ │ │ │ 0 1 │ 0 1 0 │ 0 1 0 1 │ 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 │ ╰───┴───┴───┴─────┴───────┴─────────┴───────────╯
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.
- Output:
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
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;
- Output:
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
Phix
See Matrix_with_two_diagonals#Phix and press 'M'.
Picat
Imperative
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.
- Output:
1 . 1 . 1 . . 1 . 1 . 1 1 . 1 . 1 . . 1 . 1 . 1 1 . 1 . 1 . . 1 . 1 . 1
List comprehension
main =>
N=11,
[[((1+I^J)/\1).to_string:J in 1..N].join:I in 1..N].join("\n").println.
- Output:
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
PL/M
... under CP/M (or an emulator)
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
- Output:
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
Processing
//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);
}
}
}
Python
Procedural
size = 9
for Row in range(size):
for Col in range(size):
if (Row % 2 == 1 and Col % 2 == 1) or (Row % 2 == 0 and Col % 2 == 0):
print("1", end=" ")
else:
print("0", end=" ")
print()
Output:
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
Functional
No need for nested conditionals here.
'''Mosaic grid'''
# mosaic :: Int -> [[Int]]
def mosaic(n):
'''Grid of alternating ones and zeroes,
starting with one at top left.
'''
return matrix(
n, n,
lambda y, x: (1 + y + x) % 2
)
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Test'''
for n in [7, 8]:
print(
showMatrix(
mosaic(n)
) + '\n'
)
# ----------------------- GENERIC ------------------------
# matrix :: Int -> Int -> ((Int, Int) -> a) -> [[a]]
def matrix(nRows, nCols, f):
'''A matrix of a given number of columns and rows,
in which each value is a given function over the
tuple of its (one-based) row and column indices.
'''
return [
[f(y, x) for x in range(1, 1 + nCols)]
for y in range(1, 1 + nRows)
]
# showMatrix :: [[a]] -> String
def showMatrix(rows):
'''String representation of a matrix'''
return '\n'.join([
' '.join([str(x) for x in y]) for y in rows
])
# MAIN ---
if __name__ == '__main__':
main()
- Output:
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
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 ]
- Output:
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
R
mosaic<-function(n){
x<-ifelse(n%%2==0,n+1,n)
M<-matrix(c(rep(c(1,0),times=x**2%/%2),1),nrow=x)
M[1:n,1:n]
}
print(mosaic(7))
print(mosaic(8))
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 0 1 0 1 0 1 [2,] 0 1 0 1 0 1 0 [3,] 1 0 1 0 1 0 1 [4,] 0 1 0 1 0 1 0 [5,] 1 0 1 0 1 0 1 [6,] 0 1 0 1 0 1 0 [7,] 1 0 1 0 1 0 1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 0 1 0 1 0 1 0 [2,] 0 1 0 1 0 1 0 1 [3,] 1 0 1 0 1 0 1 0 [4,] 0 1 0 1 0 1 0 1 [5,] 1 0 1 0 1 0 1 0 [6,] 0 1 0 1 0 1 0 1 [7,] 1 0 1 0 1 0 1 0 [8,] 0 1 0 1 0 1 0 1
Raku
This isn't a matrix, especially if it is supposed to be graphical; it's a very small (or extremely low resolution) bitmap.
sub checker ($n) { (^$n).map: { 1 xx $n Z× (flat ($_ %% 2, $_ % 2) xx *) } }
.put for checker 7;
put '';
.put for checker 8;
- Output:
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
Red
Red[]
mosaic: function [size][
matrix: copy [
title "Mosaic matrix"
style cell: base 50x50 font-size 20
style one: cell brown font-color beige "1"
style zero: cell beige
]
repeat i size [
repeat j size [
append matrix either even? i + j ['one] ['zero]
]
append matrix 'return
]
view matrix
]
mosaic 8
- Output:
Similar to graphical Wren entry
Ring
# Project : Mosaic matrix
# Date : 2022/18/02
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
load "stdlib.ring"
load "guilib.ring"
size = 8
C_Spacing = 1
C_ButtonBlueStyle = 'border-radius:6px;color:black; background-color: blue'
C_ButtonOrangeStyle = 'border-radius:6px;color:black; background-color: orange'
Button = newlist(size,size)
LayoutButtonRow = list(size)
app = new qApp
{
win = new qWidget() {
setWindowTitle('Identity Matrix')
move(500,100)
reSize(600,600)
winheight = win.height()
fontSize = 18 + (winheight / 100)
LayoutButtonMain = new QVBoxLayout()
LayoutButtonMain.setSpacing(C_Spacing)
LayoutButtonMain.setContentsmargins(0,0,0,0)
for Row = 1 to size
LayoutButtonRow[Row] = new QHBoxLayout() {
setSpacing(C_Spacing)
setContentsmargins(0,0,0,0)
}
for Col = 1 to size
Button[Row][Col] = new QPushButton(win) {
setSizePolicy(1,1)
}
LayoutButtonRow[Row].AddWidget(Button[Row][Col])
next
LayoutButtonMain.AddLayout(LayoutButtonRow[Row])
next
LayoutDataRow1 = new QHBoxLayout() { setSpacing(C_Spacing) setContentsMargins(0,0,0,0) }
LayoutButtonMain.AddLayout(LayoutDataRow1)
setLayout(LayoutButtonMain)
show()
}
pBegin()
exec()
}
func pBegin()
for Row = 1 to size
for Col = 1 to size
if (Row%2 = 1 and Col%2 = 1) or (Row%2 = 0 and Col%2 = 0)
Button[Row][Col].setStyleSheet(C_ButtonOrangeStyle)
Button[Row][Col].settext("1")
ok
next
next
Output image:
Mosaic Matrix
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
≫ ≫ 'MOZTX' STO
4 MOZTX
- Output:
1: [[ 1 0 1 0 ] [ 0 1 0 1 ] [ 1 0 1 0 ] [ 0 1 0 1 ]]
Filling the stack
« → n
« 1 n FOR j
j 2 MOD
2 n START DUP NOT NEXT
NEXT
n DUP 2 →LIST →ARRY
» » 'MOZTX' 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 ≫ 'MOZTX' STO
Sidef
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 }
- Output:
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
Wren
Text based
var mosaicMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
System.write(((i + j) % 2 == 0) ? "1 " : ". ")
}
System.print()
}
}
mosaicMatrix.call(9)
- Output:
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
Graphical
import "dome" for Window
import "graphics" for Canvas, Color, Font, ImageData
class Main {
construct new(n) {
var size = 60 * n + 10
Window.resize(size, size)
Canvas.resize(size, size)
Window.title = "Mosaic matrix"
// see Go-fonts page
Font.load("Go-Regular20", "Go-Regular.ttf", 20)
Canvas.font = "Go-Regular20"
var beige = Color.new(245, 245, 220)
Canvas.cls(Color.lightgray)
for (x in 0...n) {
for (y in 0...n) {
var cx = x*60 + 10
var cy = y*60 + 10
if ((x + y) % 2 == 0) {
Canvas.rectfill(cx, cy, 50, 50, Color.brown)
Canvas.print("1", cx + 20, cy + 15, beige)
} else {
Canvas.rectfill(cx, cy, 50, 50, beige)
}
}
}
// save to .png file for publication
var outputImage = ImageData.create("", size, size)
for (x in 0...size) {
for (y in 0...size) outputImage.pset(x, y, Canvas.pget(x, y))
}
outputImage.saveToFile("mosaic_matrix.png")
}
init() {}
update() {}
draw(alpha) {}
}
var Game = Main.new(9)
- Output:
https://commons.wikimedia.org/wiki/File:Mosaic_matrix.png
XPL0
proc DrawMat(S);
int S, I, J;
[for I:= 0 to S-1 do
[for J:= 0 to S-1 do
Text(0, if (J xor I) and 1 then "0 " else "1 ");
CrLf(0);
];
];
[DrawMat(6); CrLf(0);
DrawMat(7); CrLf(0);
]
- Output:
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
- Draft Programming Tasks
- Matrices
- 11l
- Action!
- Ada
- ALGOL 68
- ALGOL W
- Arturo
- AutoHotkey
- AWK
- BQN
- C
- C++
- Delphi
- SysUtils,StdCtrls
- EasyLang
- F Sharp
- Factor
- Pages with broken file links
- FreeBASIC
- FutureBasic
- Go
- Haskell
- J
- JavaScript
- Jq
- Julia
- K
- Maxima
- Nim
- Nu
- Pascal
- Perl
- Phix
- Picat
- PL/M
- Processing
- Python
- Quackery
- R
- Raku
- Red
- Ring
- RPL
- Sidef
- Wren
- DOME
- Go-fonts
- XPL0