This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
recupera esercizi lezione array statici
- Loading branch information
1 parent
bc0e83c
commit 274b374
Showing
7 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |