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.
Mosaic Matrix - image
ALGOL 68
<lang algol68>BEGIN # draw a "mosaic matrix" - one with a 1 in the top-left and then #
# alternating with another character vertically and horiontally # # draws a mosaic matrix with height and width = n using "1" and "." # PROC draw mosaic = ( INT n )VOID: BEGIN CHAR set = "1"; CHAR reset = "."; FOR i TO n DO CHAR c := IF ODD i THEN set ELSE reset FI; FOR j TO n DO print( ( " ", c ) ); 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>
- 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
C
<lang 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;
}</lang>
- 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++
<lang cpp>#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_unusedint size) { return (x + y) % 2 == 0; };
PrintMatrix(mosaic, 8); PrintMatrix(mosaic, 9);
}
</lang>
- 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
F#
<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) </lang>
- 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]]
FreeBASIC
Text based
<lang freebasic>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</lang>
- 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
<lang freebasic>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</lang>
- Output:
https://www.dropbox.com/s/nbxrttz0j99usos/mosaic_matrix.bmp?dl=0
Go
<lang 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)
}</lang>
- 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
J
Implementation: <lang J>mosq=:Template:0=2</lang>
Examples:
<lang J> 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</lang>
Julia
<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)
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
</lang>
Perl
<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;</lang>
- 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'.
Python
<lang pythong>
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()
</lang>
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
Raku
This isn't a matrix, especially if it is supposed to be graphical; it's a very small (or extremely low resolution) bitmap. <lang perl6>sub checker ($n) { (^$n).map: { 1 xx $n Z× (flat ($_ %% 2, $_ % 2) xx *) } }
.put for checker 7; put ; .put for checker 8;</lang>
- 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
<lang rebol>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</lang>
- Output:
Similar to graphical Wren entry
Ring
<lang 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
</lang> Output image: Mosaic Matrix
Wren
Text based
<lang ecmascript>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)</lang>
- 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
<lang ecmascript>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)</lang>
- Output:
https://commons.wikimedia.org/wiki/File:Mosaic_matrix.png
XPL0
<lang 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);
]</lang>
- 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