#include

#include

#include

using namespace std;

const int MAX=100;

struct t {

int hh, mm, ss;

int TotSecondi;

};

struct partec {

int pettorale;

string nome;

t tempo;

};

int ChiediDimensione();

Indice

  1. Caricamento della tabella
  2. Funzione di scambio

Caricamento della tabella

void CaricaTabella(partec T[], int d);

Funzione di scambio

void Scambia(partec& a, partec& b);
void Ordina(partec T[], int d);
void StampaTabella(partec T[], int d);

int main()

{

int dim;

partec tab[MAX];

dim=ChiediDimensione();

CaricaTabella(tab,dim);

Ordina(tab,dim);

StampaTabella(tab,dim);

system("pause");

return 0;

}

int ChiediDimensione()

{

int d;

do{

cerr

cin>>d;

} while (dMAX);

return d;

}

void CaricaTabella(partec T[], int d)

{

for (int i=0; i

cerr

cin>>T.pettorale;

cerr

cin>>T.nome;

cerr

cin>>T.tempo.hh;

cerr

cin>>T.tempo.mm;

cerr

cin>>T.tempo.ss;

T.tempo.TotSecondi = T.tempo.hh*360 + T.tempo.mm*60 + T.tempo.ss;

}

}

void Scambia(partec& a, partec& b)

{

partec comodo;

comodo=a;

a=b;

b=comodo;

}

void Ordina(partec T[], int d)

{

for (int i=0; i

for (int j=i+1; j

if (T.tempo.TotSecondi>T[j].tempo.TotSecondi)

Scambia(T, T[j]);

}

}

}

void StampaTabella(partec T[], int d)

{

cout

for (int i=0; i

cout .pettorale

.nome

.tempo.hh

.tempo.mm

.tempo.ss

}

Domande da interrogazione

  1. What is the purpose of the `CaricaTabella` function in the code?
  2. The `CaricaTabella` function is designed to populate an array of `partec` structures with participant data, including their bib number, name, and race time in hours, minutes, and seconds. It also calculates the total time in seconds for each participant.

  3. How does the `Ordina` function organize the participants' data?
  4. The `Ordina` function sorts the participants in ascending order based on their total race time in seconds. It uses a nested loop to compare each participant's time and swaps them if necessary to ensure the list is ordered from the fastest to the slowest.

  5. What role does the `Scambia` function play in the sorting process?
  6. The `Scambia` function is used to exchange the positions of two `partec` structures within the array. It is called by the `Ordina` function whenever two participants need to be swapped to maintain the correct order based on their race times.

Domande e risposte

Hai bisogno di aiuto?
Chiedi alla community