From 14bc6bd07bf4395c4e487c9b3052fd1fcee54c3a Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 9 Nov 2019 10:23:54 +0100 Subject: Ex 1, Ex13 Used C++11 style for define array length and/or return pointer --- I_anno/Programmazione_1/ex01.cc | 7 ++++--- I_anno/Programmazione_1/ex13.cc | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'I_anno/Programmazione_1') 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 +bool func(int (&a)[KK][NN][NN], int w) { vector 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 +#include #include #include -#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 +unique_ptr func(int (&A)[K][N], int (&B)[N][K]) { + auto arr = unique_ptr(new double{K}); for(int i = 0; i < K; ++i) { vector 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; -- cgit v1.2.3-18-g5258