From 274b3747e4eb0b9ba8d491961ade83006cb9d8b5 Mon Sep 17 00:00:00 2001 From: mc-cat-tty <44820563+mc-cat-tty@users.noreply.github.com> Date: Thu, 18 Nov 2021 00:26:07 +0100 Subject: [PATCH] recupera esercizi lezione array statici --- esercitazioni/array_unici.cpp | 56 +++++++++++++++++++ esercitazioni/eventi.cpp | 39 +++++++++++++ esercitazioni/eventi_eq.cpp | 35 ++++++++++++ esercitazioni/funz_copia_vettori_ordinati.cpp | 42 ++++++++++++++ esercitazioni/maiuscolo.cpp | 19 +++++++ esercitazioni/stampa_occorrenze.cpp | 1 + esercitazioni/vettore_pari.cpp | 24 ++++++++ 7 files changed, 216 insertions(+) create mode 100644 esercitazioni/eventi.cpp create mode 100644 esercitazioni/eventi_eq.cpp create mode 100644 esercitazioni/funz_copia_vettori_ordinati.cpp create mode 100644 esercitazioni/maiuscolo.cpp create mode 100644 esercitazioni/vettore_pari.cpp diff --git a/esercitazioni/array_unici.cpp b/esercitazioni/array_unici.cpp index 0151a06..6ac433a 100644 --- a/esercitazioni/array_unici.cpp +++ b/esercitazioni/array_unici.cpp @@ -2,4 +2,60 @@ using namespace std; +/* +* Conta il numero di occorrenze di x presenti in v +* +* @param x: target number +* @param v[]: vettore nel quale cercare le occorrenze di x +* @param dim: dimensione del vettore v[] +* @return: numero di occorrenze di x in v[] +*/ +const int contavolte(const int x, const int v[], const int dim) { + int count = 0; + for (int i=0; i +#include +#include + +using namespace std; + +/* +* Questa funzione genera un evento di tipo 1 +* o tipo 2. +* @param prob: probabilità di generare un evento di tipo 1 +*/ +const int gen_evento(const int prob) { + return rand() / (RAND_MAX*((float)(100-prob)/100)); +} + +int main() { + int n; // numero di iterazioni + cout << "Numero di iterazioni: "; cin >> n; + + int p; // probabilità evento 1; 0<=p<=100 + cout << "Probabilità evento 1: "; cin >> p; + + // contatori dei due tipi di eventi + int type1, type2; type1 = type2 = 0; + + srand(time(0)); + for(int i=0; i +#include +#include + +using namespace std; + +/* +* Questa funzione genera un evento di tipo 1 +* o tipo 2 in modo equamente distribuito +*/ +const int gen_evento() { + return rand() / (RAND_MAX/2); +} + +int main() { + int n; // numero di iterazioni + cout << "Numero di iterazioni: "; cin >> n; + + // contatori dei due tipi di eventi + int type1, type2; type1 = type2 = 0; + + srand(time(0)); + for(int i=0; i + +using namespace std; + +// this function will write dim1+dim2 elements into v3 +void copy_ord(const int v1[], const int dim1, const int v2[], const int dim2, int v3[]) { + int i1, i2, i3; i1 = i2 = i3 = 0; + while (i1 < dim1 && i2 < dim2) { + if (v1[i1] < v2[i2]) + v3[i3++] = v1[i1++]; + else + v3[i3++] = v2[i2++]; + } + + if (i1 == dim1 && i2 == dim2) + return; + else if (i1 == dim1) + for (int i=i2; i + +using namespace std; + +bool lowercase(char &c) { + if (c<'A' || c>'Z') + return false; + + c = c - 'A' + 'a'; + return true; +} + +int main() { + char c; + cout << "Carattere: "; cin >> c; + + cout << lowercase(c) << " " << c << endl; + return 0; +} diff --git a/esercitazioni/stampa_occorrenze.cpp b/esercitazioni/stampa_occorrenze.cpp index f58f0a2..b9ad00a 100644 --- a/esercitazioni/stampa_occorrenze.cpp +++ b/esercitazioni/stampa_occorrenze.cpp @@ -2,6 +2,7 @@ using namespace std; +// 0 non è un input value valido, in quanto usato come "marcatore" void stampa_occorrenze (int v[], int dim) { int c; for (int i=0; i + +using namespace std; + +const int init(const int v_in[], int v_out[], const int n) { + int i_out = 0; + for (int i=0; i