aboutsummaryrefslogtreecommitdiff
path: root/challenge-011
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-06-27 14:47:40 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-06-27 14:47:40 +0100
commit51109a5e84bc635dabe896eeafbca10909db095c (patch)
treef9f62651aec348c1539ab906770c254cf061f273 /challenge-011
parent553115b6b775de8fa1f94b359857e41922d86925 (diff)
downloadperlweeklychallenge-club-51109a5e84bc635dabe896eeafbca10909db095c.tar.gz
perlweeklychallenge-club-51109a5e84bc635dabe896eeafbca10909db095c.tar.bz2
perlweeklychallenge-club-51109a5e84bc635dabe896eeafbca10909db095c.zip
Add C and C++ solutions to challenge 011
Diffstat (limited to 'challenge-011')
-rw-r--r--challenge-011/paulo-custodio/c/ch-2.c62
-rw-r--r--challenge-011/paulo-custodio/cpp/ch-2.cpp49
-rw-r--r--challenge-011/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-011/paulo-custodio/t/test-2.yaml26
4 files changed, 125 insertions, 14 deletions
diff --git a/challenge-011/paulo-custodio/c/ch-2.c b/challenge-011/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..ae1f1f7453
--- /dev/null
+++ b/challenge-011/paulo-custodio/c/ch-2.c
@@ -0,0 +1,62 @@
+/*
+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 <stdio.h>
+#include <stdlib.h>
+
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+int** identity_matrix(int n) {
+ int** id = check_mem(malloc(n * sizeof(int*)));
+ for (int i = 0; i < n; i++) {
+ id[i] = check_mem(calloc(n, sizeof(int)));
+ id[i][i] = 1;
+ }
+ return id;
+}
+
+void print_matrix(int** m, int n) {
+ printf("[");
+ const char* row_sep = "";
+ for (int row = 0; row < n; row++) {
+ printf("%s[", row_sep);
+ row_sep = ",\n ";
+ const char* col_sep = "";
+ for (int col = 0; col < n; col++) {
+ printf("%s%d", col_sep, m[row][col]);
+ col_sep = ", ";
+ }
+ printf("]");
+ }
+ printf("]\n");
+}
+
+void free_matrix(int** m, int n) {
+ for (int i = 0; i < n; i++)
+ free(m[i]);
+ free(m);
+}
+
+int main(int argc, char* argv[]) {
+ int n = 0;
+ if (argc == 2)
+ n = atoi(argv[1]);
+ if (n < 1)
+ n = 4;
+
+ int** id = identity_matrix(n);
+ print_matrix(id, n);
+ free_matrix(id, n);
+}
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..d2847da6ec
--- /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.push_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);
+}
diff --git a/challenge-011/paulo-custodio/perl/ch-2.pl b/challenge-011/paulo-custodio/perl/ch-2.pl
index 7154fdab05..7898121151 100644
--- a/challenge-011/paulo-custodio/perl/ch-2.pl
+++ b/challenge-011/paulo-custodio/perl/ch-2.pl
@@ -20,4 +20,4 @@ for my $r (0..$size-1) {
}
# print matrix
-say "[", join("\n ", map {"[".join(", ", @{$i[$_]})."]"} 0..$size-1), "]";
+say "[", join(",\n ", map {"[".join(", ", @{$i[$_]})."]"} 0..$size-1), "]";
diff --git a/challenge-011/paulo-custodio/t/test-2.yaml b/challenge-011/paulo-custodio/t/test-2.yaml
index 3d3d7b7bb3..f783d9a927 100644
--- a/challenge-011/paulo-custodio/t/test-2.yaml
+++ b/challenge-011/paulo-custodio/t/test-2.yaml
@@ -9,29 +9,29 @@
args: 2
input:
output: |
- |[[1, 0]
+ |[[1, 0],
| [0, 1]]
- setup:
cleanup:
args: 4
input:
output: |
- |[[1, 0, 0, 0]
- | [0, 1, 0, 0]
- | [0, 0, 1, 0]
+ |[[1, 0, 0, 0],
+ | [0, 1, 0, 0],
+ | [0, 0, 1, 0],
| [0, 0, 0, 1]]
- setup:
cleanup:
args: 10
input:
output: |
- |[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
- | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
- | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
- | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
- | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
- | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
- | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
- | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
+ |[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
+ | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
+ | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
+ | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
+ | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
+ | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
+ | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
+ | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
+ | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
| [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]