Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct persona {
- string nome;
- string cognome;
- int voti[25];
- int numero_voti;
- int media;
- int eta;
- };
- double media_voti_classe(struct persona studenti[], int lunghezza);
- struct persona piu_giovane(struct persona studenti[], int lunghezza);
- const int numero_studenti = 3;
- int main() {
- struct persona studenti[numero_studenti];
- for (int i = 0; i < numero_studenti; i++) {
- cout << "Nome: ";
- cin >> studenti[i].nome;
- cout << "Cognome: ";
- cin >> studenti[i].cognome;
- cout << "Eta: ";
- cin >> studenti[i].eta;
- // Chiedo quanti voti inserire
- cout << "Numero voti: ";
- cin >> studenti[i].numero_voti;
- // Leggo tutti i voti
- for (int j = 0; j < studenti[i].numero_voti; j++) {
- cout << "Inserire voto: ";
- cin >> studenti[i].voti[j];
- }
- // calcolo media del singolo studente
- int somma_voti = 0;
- for (int j = 0; j < studenti[i].numero_voti; j++) {
- somma_voti = somma_voti + studenti[i].voti[j];
- }
- studenti[i].media = somma_voti / studenti[i].numero_voti;
- }
- cout << "Media voti: " << media_voti_classe(studenti, numero_studenti)
- << endl;
- struct persona studente_giovane = piu_giovane(studenti, numero_studenti);
- cout << "Piu' giovane: " << studente_giovane.nome << " "
- << studente_giovane.cognome << endl;
- for (int i = 0; i < numero_studenti; i++) {
- cout << studenti[i].nome << ", ";
- }
- return 0;
- }
- double media_voti_classe(struct persona studenti[], int lunghezza) {
- double somma_voti = 0;
- for (int i = 0; i < lunghezza; i++) {
- somma_voti = somma_voti + studenti[i].media;
- }
- return somma_voti / lunghezza;
- }
- struct persona piu_giovane(struct persona studenti[], int lunghezza) {
- int posizione_giovane = 0;
- for (int i = 0; i < lunghezza; i++) {
- if (studenti[i].eta < studenti[posizione_giovane].eta) {
- posizione_giovane = i;
- }
- }
- return studenti[posizione_giovane];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement