Matrix with two diagonals

From Rosetta Code
Revision as of 11:47, 17 February 2022 by Thundergnat (talk | contribs) (Thundergnat moved page Special identity matrix with two diagonals to Matrix with two diagonals: It's not special and it's not an identity)
Matrix with two diagonals is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Draw a square matrix which has 1's on both diagonals but 0's elsewhere.

C

Translation of: Wren

<lang c>#include <stdio.h>

void specialMatrix(unsigned int n) {

   int i, j;
   for (i = 0; i < n; ++i) {
       for (j = 0; j < n; ++j) {
           if (i == j || i + j == n - 1) {
               printf("%d ", 1);
           } else {
               printf("%d ", 0);
           }
       }
       printf("\n");
   }

}

int main() {

   specialMatrix(10); // even n
   printf("\n");
   specialMatrix(11); // odd n
   return 0;

}</lang>

Output:
1 0 0 0 0 0 0 0 0 1 
0 1 0 0 0 0 0 0 1 0 
0 0 1 0 0 0 0 1 0 0 
0 0 0 1 0 0 1 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 1 0 0 1 0 0 0 
0 0 1 0 0 0 0 1 0 0 
0 1 0 0 0 0 0 0 1 0 
1 0 0 0 0 0 0 0 0 1 

1 0 0 0 0 0 0 0 0 0 1 
0 1 0 0 0 0 0 0 0 1 0 
0 0 1 0 0 0 0 0 1 0 0 
0 0 0 1 0 0 0 1 0 0 0 
0 0 0 0 1 0 1 0 0 0 0 
0 0 0 0 0 1 0 0 0 0 0 
0 0 0 0 1 0 1 0 0 0 0 
0 0 0 1 0 0 0 1 0 0 0 
0 0 1 0 0 0 0 0 1 0 0 
0 1 0 0 0 0 0 0 0 1 0 
1 0 0 0 0 0 0 0 0 0 1 

Go

Translation of: Wren

<lang go>package main

import "fmt"

func specialMatrix(n uint) {

   for i := uint(0); i < n; i++ {
       for j := uint(0); j < n; j++ {
           if i == j || i+j == n-1 {
               fmt.Printf("%d ", 1)
           } else {
               fmt.Printf("%d ", 0)
           }
       }
       fmt.Println()
   }

}

func main() {

   specialMatrix(8) // even n
   fmt.Println()
   specialMatrix(9) // odd n

}</lang>

Output:
1 0 0 0 0 0 0 1 
0 1 0 0 0 0 1 0 
0 0 1 0 0 1 0 0 
0 0 0 1 1 0 0 0 
0 0 0 1 1 0 0 0 
0 0 1 0 0 1 0 0 
0 1 0 0 0 0 1 0 
1 0 0 0 0 0 0 1 

1 0 0 0 0 0 0 0 1 
0 1 0 0 0 0 0 1 0 
0 0 1 0 0 0 1 0 0 
0 0 0 1 0 1 0 0 0 
0 0 0 0 1 0 0 0 0 
0 0 0 1 0 1 0 0 0 
0 0 1 0 0 0 1 0 0 
0 1 0 0 0 0 0 1 0 
1 0 0 0 0 0 0 0 1 

Ring

<lang ring>

  1. Project : Identity Matrix
  2. Date  : 2022/16/02
  3. Author  : Gal Zsolt (~ CalmoSoft ~)
  4. 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 = Col or Row + Col = 9
               Button[Row][Col].setStyleSheet(C_ButtonOrangeStyle)
               Button[Row][Col].settext("1")
            else
               Button[Row][Col].setStyleSheet(C_ButtonBlueStyle)
               Button[Row][Col].settext("0")
            ok

next

    next
    score = 0

</lang> Outpu image:
Special identity matrix with two diagonals

Wren

A terminal based solution as I don't like asking people to view external images. <lang ecmascript>var specialMatrix = Fn.new { |n|

   for (i in 0...n) {
       for (j in 0...n) {
           System.write((i == j || i + j == n - 1) ? "1 " : "0 ")
       }
       System.print()
   }

}

specialMatrix.call(6) // even n System.print() specialMatrix.call(7) // odd n</lang>

Output:
1 0 0 0 0 1 
0 1 0 0 1 0 
0 0 1 1 0 0 
0 0 1 1 0 0 
0 1 0 0 1 0 
1 0 0 0 0 1 

1 0 0 0 0 0 1 
0 1 0 0 0 1 0 
0 0 1 0 1 0 0 
0 0 0 1 0 0 0 
0 0 1 0 1 0 0 
0 1 0 0 0 1 0 
1 0 0 0 0 0 1