Build an identity matrix of a size known at runtime.

An identity matrix is a square matrix, of size *n* × *n*, where the diagonal elements are all 1s, and the other elements are all 0s.

**Library**: STL

template<class T> class matrix { public: matrix( unsigned int nSize ) : m_oData(nSize * nSize, 0), m_nSize(nSize) {} inline T& operator()(unsigned int x, unsigned int y) { return m_oData[x+m_nSize*y]; } void identity() { int nCount = 0; int nStride = m_nSize + 1; std::generate( m_oData.begin(), m_oData.end(), [&]() { return !(nCount++%nStride); } ); } inline unsigned int size() { return m_nSize; } private: std::vector<T> m_oData; unsigned int m_nSize; }; int main() { int nSize; std::cout << "Enter matrix size (N): "; std::cin >> nSize; matrix<int> oMatrix( nSize ); oMatrix.identity(); for ( unsigned int y = 0; y < oMatrix.size(); y++ ) { for ( unsigned int x = 0; x < oMatrix.size(); x++ ) { std::cout << oMatrix(x,y) << " "; } std::cout << std::endl; } return 0; }

**Library:** boost

#include <boost/numeric/ublas/matrix.hpp> int main() { using namespace boost::numeric::ublas; int nSize; std::cout << "Enter matrix size (N): "; std::cin >> nSize; identity_matrix<int> oMatrix( nSize ); for ( unsigned int y = 0; y < oMatrix.size2(); y++ ) { for ( unsigned int x = 0; x < oMatrix.size1(); x++ ) { std::cout << oMatrix(x,y) << " "; } std::cout << std::endl; } return 0; }

- Output:

Enter matrix size (N): 5 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1

Content is available under GNU Free Documentation License 1.2.