Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 28, 2023 07:21
Show Gist options
  • Save juanfal/39f82dd47d7b5c9d9eff98c2dff8b11e to your computer and use it in GitHub Desktop.
Save juanfal/39f82dd47d7b5c9d9eff98c2dff8b11e to your computer and use it in GitHub Desktop.
building an identity matrix
// t13e03.identityMat.cpp
// juanfc 2023-11-23
//
#include <iostream>
#include <array>
using namespace std;
const int N = 3;
typedef array<array<int, N>,N> TSqMat;
void print(TSqMat m);
TSqMat identMat();
int main()
{
print(identMat());
return 0;
}
TSqMat identMat()
{
TSqMat m;
for (int row = 0; row < N; ++row)
for (int col = 0; col < N; ++col)
m[row][col] = (row==col)? 1: 0;
return m;
}
void print(TSqMat m)
{
for (int row = 0; row < N; ++row) {
for (int col = 0; col < N; ++col) {
cout << m[row][col] << " ";
}
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment