Four sides of square: Difference between revisions

m
(Added "See also")
 
(12 intermediate revisions by 6 users not shown)
Line 1:
{{Draft task|Matrices}}
 
;Task:
Line 6:
<br>
[http://keptarhely.eu/view.php?file=20220218v00x6hugz.jpeg Four sides of square - image]
 
 
===See also===
* [[Matrix with two diagonals]]
* [[Mosaic matrix]]
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
V size = 9
Line 37 ⟶ 35:
1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">
;;; draw a matrix with 1s on the edges and 0s elsewhere
 
;;; draws a matrix with height and width = n with 1s on the edges
PROC drawSquare( INT n )
CARD i, j
FOR i = 1 TO n DO
FOR j = 1 TO n DO
Put(' )
IF i = 1 OR i = n OR j = 1 OR j = n THEN Put('1) ELSE Put('0) FI
OD
PutE()
OD
RETURN
 
PROC Main()
drawSquare( 6 )
PutE()
drawSquare( 7 )
RETURN
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
Line 127 ⟶ 165:
1 1 1 1 1 1 1
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % draw a matrix with 1s on the edges and 0s elsewhere %
% draws a matrix with height and width = n with 1s on the edges %
procedure drawSquare ( integer value n ) ;
for i := 1 until n do begin
for j := 1 until n do begin
writeon( s_w := 0, if i = 1 or i = n or j = 1 or j = n then " 1" else " 0" )
end for_j ;
write()
end drawSquare ;
% test the draw square procedure %
drawSquare( 6 );
write();
drawSquare( 7 )
end.
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|AppleScript}}==
Defined in terms of a generic matrix function:
Line 359 ⟶ 433:
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
Line 419 ⟶ 492:
1 1 1 1 1 1 1
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Square ← ∨⌜˜1⌽↑⟜1‿1
 
Square 5</syntaxhighlight>
{{out}}
<pre>┌─
╵ 1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
┘</pre>
 
=={{header|C}}==
Line 443 ⟶ 529:
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Line 573 ⟶ 658:
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc holmat n . .
for i to n
for j to n
if i = 1 or i = n or j = 1 or j = n
write "1 "
else
write "0 "
.
.
print ""
.
.
holmat 10
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|F_Sharp|F#}}==
Line 595 ⟶ 709:
[1; 1; 1; 1; 1; 1]]
</pre>
 
 
=={{header|FreeBASIC}}==
Line 646 ⟶ 759:
{{out}}
https://www.dropbox.com/s/9g5ahfzw1muuzgm/hollowMatrix.bmp?dl=0
 
 
=={{header|Go}}==
Line 779 ⟶ 891:
 
=={{header|J}}==
 
Implementation:
<syntaxhighlight lang="j">fsosq=: {{+./~(+.|.)y{.1}}</syntaxhighlight>
Line 873 ⟶ 984:
squareonesapp(8)
</syntaxhighlight>
 
=={{header|K}}==
<syntaxhighlight lang="k">square: {x|/:x}{x||x}@~-':!:
 
square 5</syntaxhighlight>
{{out}}
<pre>(1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1)</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Manipulate[ArrayPad[ConstantArray[0, {1, 1} n - 1], 1, 1] // Grid, {n, 2, 20, 1}]</syntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a square matrix with square pattern in their entries */
square(n):=genmatrix(lambda([x,y],if x=1 or y=1 or x=n or y=n then 1 else 0),n,n)$
 
/* Example */
square(6);
</syntaxhighlight>
{{out}}
<pre>
matrix(
[1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1]
)
</pre>
 
=={{header|Nim}}==
There are several ways to draw the square. Here is one of them:
<syntaxhighlight lang="Nim">import std/[sequtils, strutils]
 
proc drawSquare(n: Positive) =
let s1 = repeat(1, n).join(" ")
let s2 = (1 & repeat(0, n - 2) & 1).join(" ")
echo s1
for i in 2..<n: echo s2
echo s1
 
drawSquare(7)
</syntaxhighlight>
 
{{out}}
<pre>1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|Perl}}==
Line 893 ⟶ 1,059:
=={{header|Phix}}==
See [[Matrix_with_two_diagonals#GUI.2Fonline|Matrix_with_two_diagonals#Phix]] and press 'O'.
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* DRAW SOME SQUARES WITH 1S ON THE EDGES and 0S ELSEWHERE */
 
/* 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$SQUARE: PROCEDURE( N );
DECLARE N BYTE;
DECLARE ( I, J ) BYTE;
DO I = 1 TO N;
DO J = 1 TO N;
CALL PR$CHAR( ' ' );
IF I = 1 OR I = N OR J = 1 OR J = N THEN CALL PR$CHAR( '1' );
ELSE CALL PR$CHAR( '0' );
END;
CALL PR$NL;
END;
END DRAW$SQUARE ;
 
CALL DRAW$SQUARE( 6 );
CALL PR$NL;
CALL DRAW$SQUARE( 7 );
 
EOF
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|Processing}}==
Line 1,025 ⟶ 1,240:
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ 0 over 2 - of
1 tuck join join nested
Line 1,207 ⟶ 1,421:
1 1 1 1
</pre>
 
 
=={{header|Ruby}}==
Line 1,245 ⟶ 1,458:
=={{header|Wren}}==
===Text based===
<syntaxhighlight lang="ecmascriptwren">var hollowMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
Line 1,272 ⟶ 1,485:
{{libheader|Go-fonts}}
This is designed to look as close as possible to the Red entry's image so that we don't have to fill up Wikimedia Commons with similar looking images.
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color, Font
class Main {
1,978

edits