Advertisement
informaticage

esempio

May 29th, 2021 (edited)
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct persona {
  7.   string nome;
  8.   string cognome;
  9.   int voti[25];
  10.   int numero_voti;
  11.   int media;
  12.   int eta;
  13. };
  14.  
  15. double media_voti_classe(struct persona studenti[], int lunghezza);
  16. struct persona piu_giovane(struct persona studenti[], int lunghezza);
  17. const int numero_studenti = 3;
  18. int main() {
  19.   struct persona studenti[numero_studenti];
  20.   for (int i = 0; i < numero_studenti; i++) {
  21.     cout << "Nome: ";
  22.     cin >> studenti[i].nome;
  23.     cout << "Cognome: ";
  24.     cin >> studenti[i].cognome;
  25.     cout << "Eta: ";
  26.     cin >> studenti[i].eta;
  27.  
  28.     // Chiedo quanti voti inserire
  29.     cout << "Numero voti: ";
  30.     cin >> studenti[i].numero_voti;
  31.     // Leggo tutti i voti
  32.     for (int j = 0; j < studenti[i].numero_voti; j++) {
  33.       cout << "Inserire voto: ";
  34.       cin >> studenti[i].voti[j];
  35.     }
  36.  
  37.     // calcolo media del singolo studente
  38.     int somma_voti = 0;
  39.     for (int j = 0; j < studenti[i].numero_voti; j++) {
  40.       somma_voti = somma_voti + studenti[i].voti[j];
  41.     }
  42.     studenti[i].media = somma_voti / studenti[i].numero_voti;
  43.   }
  44.  
  45.   cout << "Media voti: " << media_voti_classe(studenti, numero_studenti)
  46.        << endl;
  47.   struct persona studente_giovane = piu_giovane(studenti, numero_studenti);
  48.   cout << "Piu' giovane: " << studente_giovane.nome << " "
  49.        << studente_giovane.cognome << endl;
  50.  
  51.   for (int i = 0; i < numero_studenti; i++) {
  52.     cout << studenti[i].nome << ", ";
  53.   }
  54.   return 0;
  55. }
  56.  
  57. double media_voti_classe(struct persona studenti[], int lunghezza) {
  58.   double somma_voti = 0;
  59.   for (int i = 0; i < lunghezza; i++) {
  60.     somma_voti = somma_voti + studenti[i].media;
  61.   }
  62.  
  63.   return somma_voti / lunghezza;
  64. }
  65.  
  66. struct persona piu_giovane(struct persona studenti[], int lunghezza) {
  67.   int posizione_giovane = 0;
  68.   for (int i = 0; i < lunghezza; i++) {
  69.     if (studenti[i].eta < studenti[posizione_giovane].eta) {
  70.       posizione_giovane = i;
  71.     }
  72.   }
  73.  
  74.   return studenti[posizione_giovane];
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement