Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
recupera esercizi lezione array statici
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-cat-tty committed Nov 17, 2021
1 parent bc0e83c commit 274b374
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 0 deletions.
56 changes: 56 additions & 0 deletions esercitazioni/array_unici.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<dim; i++) {
if (v[i] == x)
count++;
}
return count;
}

/*
* Inserisce nell'array out gli elementi unici presenti nell'array in
*
* @param in[]: vettore di input
* @param out[]: parametro di output. Vettore nel quale verranno inseriti
* gli elementi unici di in[]
* @param dim: dimensione di in[]
* @return la dimensione dell'array out[]
*/
const int creaunici(const int in[], int out[], const int dim) {
int i_out = 0;
for (int i=0; i<dim; i++)
if (contavolte(in[i], in, dim) == 1)
out[i_out++] = in[i];
return i_out;
}

void stampa_array(const int v[], const int dim) {
for (int i=0; i<dim; i++) {
cout << v[i] << " ";
}
cout << endl;
}

int main() {
const int DIM = 11;
int v1[DIM] = {2, 4, 3, 2, 7, 1, 3, 5, 1, 8, 9};
int v2[DIM]; // 4 7 5 8 9


cout << "Array originale: ";
stampa_array(v1, DIM);

int dim2 = creaunici(v1, v2, DIM);

cout << "Array unici: ";
stampa_array(v2, dim2);

return 0;
}
39 changes: 39 additions & 0 deletions esercitazioni/eventi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <time.h>
#include <stdlib.h>

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<n; i++) {
if (gen_evento(p))
type1++;
else
type2++;
}

cout
<< "type1%: " << (float)type1*100.0/n << endl
<< "type2%: " << (float)type2*100.0/n << endl;

return 0;
}
35 changes: 35 additions & 0 deletions esercitazioni/eventi_eq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <time.h>
#include <stdlib.h>

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<n; i++) {
if (gen_evento())
type1++;
else
type2++;
}

cout
<< "type1%: " << (float)type1*100.0/n << endl
<< "type2%: " << (float)type2*100.0/n << endl;

return 0;
}
42 changes: 42 additions & 0 deletions esercitazioni/funz_copia_vettori_ordinati.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>

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<dim2; i++)
v3[i3++] = v2[i];
else if (i2 == dim2)
for (int i=i1; i<dim1; i++)
v3[i3++] = v1[i];
}

int main() {
const int
DIM1 = 5,
DIM2 = 4,
DIM3 = DIM1 + DIM2;
int v1[DIM1] = {2, 3, 6, 8, 10},
v2[DIM2] = {1, 5, 10, 15},
v3[DIM3];

copy_ord(v1, DIM1, v2, DIM2, v3);

for (int i=0; i<DIM3; i++) {
cout << v3[i] << " ";
}
cout << endl;

return 0;
}
19 changes: 19 additions & 0 deletions esercitazioni/maiuscolo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>

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;
}
1 change: 1 addition & 0 deletions esercitazioni/stampa_occorrenze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<dim; i++) {
Expand Down
24 changes: 24 additions & 0 deletions esercitazioni/vettore_pari.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>

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<n; i++)
if (v_in[i]%2 == 0)
v_out[i_out++] = v_in[i];
return i_out;
}

int main() {
const int DIM1 = 5;
int v1[DIM1] = {3, 2, 7, 8, 10},
v2[DIM1];
int dim2;

dim2 = init(v1, v2, DIM1);

for (int i=0; i<dim2; i++)
cout << v2[i] << " ";
cout << endl;
}

0 comments on commit 274b374

Please sign in to comment.