summaryrefslogtreecommitdiff
path: root/I_anno
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2019-11-09 10:23:54 +0100
committerSanto Cariotti <dcariotti24@gmail.com>2019-11-09 10:23:54 +0100
commit14bc6bd07bf4395c4e487c9b3052fd1fcee54c3a (patch)
treeaad866e80d2052aa6fa85c736f9e196984f69cf4 /I_anno
parent9614d577a3f3ca8f659074a56744ed33b7df67c6 (diff)
Ex 1, Ex13
Used C++11 style for define array length and/or return pointer
Diffstat (limited to 'I_anno')
-rw-r--r--I_anno/Programmazione_1/ex01.cc7
-rw-r--r--I_anno/Programmazione_1/ex13.cc18
2 files changed, 14 insertions, 11 deletions
diff --git a/I_anno/Programmazione_1/ex01.cc b/I_anno/Programmazione_1/ex01.cc
index ef91698..9183dec 100644
--- a/I_anno/Programmazione_1/ex01.cc
+++ b/I_anno/Programmazione_1/ex01.cc
@@ -6,11 +6,12 @@
using namespace std;
-bool func(int a[K][N][N], int w) {
+template<int KK, int NN>
+bool func(int (&a)[KK][NN][NN], int w) {
vector<int> list;
- for(int i = 0; i < K; ++i) {
+ for(int i = 0; i < KK; ++i) {
list.clear();
- for(int j = 0; j < N; ++j)
+ for(int j = 0; j < NN; ++j)
list.push_back(a[i][j][j]);
auto mm = minmax_element(begin(list), end(list));
diff --git a/I_anno/Programmazione_1/ex13.cc b/I_anno/Programmazione_1/ex13.cc
index e044504..bc0a6b5 100644
--- a/I_anno/Programmazione_1/ex13.cc
+++ b/I_anno/Programmazione_1/ex13.cc
@@ -1,12 +1,14 @@
#include <iostream>
+#include <memory>
#include <algorithm>
#include <vector>
-#define K 2
-#define N 3
+#define K2 2
+#define N2 3
using namespace std;
-double* func(int A[K][N], int B[N][K]) {
- double* arr = new double[K];
+template<int N, int K>
+unique_ptr<double[]> func(int (&A)[K][N], int (&B)[N][K]) {
+ auto arr = unique_ptr<double[]>(new double{K});
for(int i = 0; i < K; ++i) {
vector<int> t;
int sum = 0;
@@ -24,18 +26,18 @@ double* func(int A[K][N], int B[N][K]) {
}
int main() {
- int A[K][N] = {
+ int A[K2][N2] = {
{3, 7, 10},
{5, 12, 32},
};
- int B[N][K] = {
+ int B[N2][K2] = {
{12, 10},
{15, 17},
{8, 0},
};
- double* x = func(A, B);
+ auto x = func(A, B);
- for(int i = 0; i < K; ++i)
+ for(int i = 0; i < K2; ++i)
cout << x[i] << ' ';
cout << endl;