diff --git a/struct/dato.cpp b/struct/dato.cpp index 6c7ec83..b719b2e 100644 --- a/struct/dato.cpp +++ b/struct/dato.cpp @@ -3,9 +3,8 @@ AUTORE: Manuel Vichi (WIP) Dato */ #include -#include -#include #include +#include #define SIZEARRAY 10 using namespace std; typedef struct { @@ -14,15 +13,48 @@ typedef struct { char simbolo; } Dato; -Dato riempiArray(Dato arr[], size_t size) { +void riempiArray(Dato arr[], size_t size) { + srand(time(NULL)); for (size_t i = 0; i < size; i++) { arr[i].numero = rand() % 100; - arr[i].valore = rand() % 1.5f; + arr[i].valore = (float)rand(); arr[i].simbolo = 'A' + rand() % 65; } } +int trovaMassimo(const Dato arr[], size_t size) { + float valoremax = 0; + int index; + for (size_t i = 0; i < size; i++) { + if (arr[i].valore > valoremax) { + valoremax = arr[i].valore; + index = i; + } + } + return index; +} + +int contaCar(const Dato arr[], size_t size, char c) { + int occorrenze = 0; + for (size_t i = 0; i < size; i++) { + if (arr[i].simbolo == c) { + occorrenze++; + } + } + return occorrenze; +} + +void stampaArray(const Dato arr[], size_t size) { + for (size_t i = 0; i < size; i++) { + cout << "Numero: " << arr[i].numero << ", Valore: " << arr[i].valore << ", Carattere: " << arr[i].simbolo << endl; + } +} + int main(void) { Dato array[SIZEARRAY]; + riempiArray(array,SIZEARRAY); + stampaArray(array,SIZEARRAY); + cout << "Occorrenze del carattere A: " << contaCar(array,SIZEARRAY,'A') << endl; + cout << "Valore massimo: " << trovaMassimo(array,SIZEARRAY); return 0; -} +} \ No newline at end of file