diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-06-27 20:22:30 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-06-27 20:22:30 +0100 |
| commit | 287d70fb03bbb2ba51e636cf051775400e94bcb4 (patch) | |
| tree | 85a39b2a331bbb5b49d17f63ae76796f3999e7ce /challenge-011/paulo-custodio/cpp/ch-2.cpp | |
| parent | 9beb73e6913f7e462c4aca8719048a7be98e4ad2 (diff) | |
| parent | 1b3142a75a8db8a886596770514e2d9fa1cd9187 (diff) | |
| download | perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.tar.gz perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.tar.bz2 perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-011/paulo-custodio/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-011/paulo-custodio/cpp/ch-2.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-011/paulo-custodio/cpp/ch-2.cpp b/challenge-011/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..831b467e4f --- /dev/null +++ b/challenge-011/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,49 @@ +/* +Challenge 011 + +Challenge #2 +Write a script to create an Indentity Matrix for the given size. For example, +if the size is 4, then create Identity Matrix 4x4. For more information about +Indentity Matrix, please read the wiki page. +*/ + +#include <iostream> +#include <vector> +using namespace std; + +vector<vector<int>> identity_matrix(int n) { + vector<vector<int>> id; + for (int row = 0; row < n; row++) { + id.emplace_back(); + for (int col = 0; col < n; col++) + id[row].push_back(col == row ? 1 : 0); + } + return id; +} + +void print_matrix(ostream& os, vector<vector<int>>& m) { + os << "["; + const char* row_sep = ""; + for (size_t row = 0; row < m.size(); row++) { + os << row_sep << "["; + row_sep = ",\n "; + const char* col_sep = ""; + for (size_t col = 0; col < m[row].size(); col++) { + os << col_sep << m[row][col]; + col_sep = ", "; + } + os << "]"; + } + os << "]\n"; +} + +int main(int argc, char* argv[]) { + int n = 0; + if (argc == 2) + n = atoi(argv[1]); + if (n < 1) + n = 4; + + vector<vector<int>> id = identity_matrix(n); + print_matrix(cout, id); +} |
