Ο γυμναστής μιας ομάδας μπάσκετ καταχωρεί σε ένα πίνακα το όνομα και το ύψος (cm) των 10 αθλητών της ομάδας. Να γράψετε πρόγραμμα που καταχωρεί τα στοιχεία σε ένα πίνακα και στη συνέχεια:
- Εντοπίζει τον πιο ψηλό αθλητή (όνομα-ύψος)
- Εντοπίζει τον πιο χαμηλό αθλητή (όνομα-ύψος)
- Υπολογίζει το μέσο όρο του ύψους της ομάδας
- Πόσοι αθλητές έχουν ύψος πάνω από 180cm; (ονόματα)
- Ταξινομεί και εμφανίζει στην οθόνη τα όνομα και το ύψος των αθλητών σε αύξουσα, με βάση το ύψος τους.
- Το ερώτημα 4 επίσης να αποθηκεύεται σε ένα αρχείο με το όνομα «team.txt».
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct athlete{
string aname;
int height;
}team[10],temp;
int main(){
int i;
for(i=0;i<10;i++){
cout<<"Name: ";
cin>>team[i].aname;
cout<<"Height: ";
cin>>team[i].height;
}
int maxi=0;
int mini=0;
int sum=0;
int counterOver180=0;
for(i=1;i<10;i++){
if(team[maxi].height<team[i].height){
maxi=i;
}
if(team[mini].height>team[i].height){
mini=i;
}
sum+=team[i].height;
if(team[i].height>180){
counterOver180++;
}
}
cout<<"Tallest athlete is "<<team[maxi].aname<<", "<<team[maxi].height<<"cm tall."<<endl;
cout<<"Shortest athlete is "<<team[mini].aname<<", "<<team[mini].height<<"cm tall."<<endl;
cout<<"Average height of the team is "<<(double)sum/10<<"cm"<<endl;
cout<<counterOver180<<" athletes have height over 180cm"<<endl;
for(i=0;i<9;i++){
for(int j=i+1;j<10;j++){
if(team[i].height>team[j].height){
temp=team[i];
team[i]=team[j];
team[j]=temp;
}
}
}
ofstream fout("team.txt");
for(i=0;i<10;i++){
cout<<team[i].aname<<"\t"<<team[i].height<<endl;
fout<<team[i].aname<<"\t"<<team[i].height<<endl;
}
fout.close();
return 0;
}