Вы находитесь на странице: 1из 5

DATA STRUCTURE AND ALGORITHM

ASSIGNMENT 1

o ADJIE GHUSA MAHENDRA | 001201800130


o AKHMAD SALMAN A. | 001201800048
o IBNU ATHAILAH | 001201800119

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

struct player {string name; string major ; int age; float height;};

int main(){
int i, x;
float avg=0;
float sum=0;
player num[100];
char choice;
cout<<"input data from file[Y/N]"<<endl;
cin>>choice;
if(choice=='n'||choice=='N'){
system("cls");
cout<<"How many players?"<<endl;
cin>>x;
cout<<"please type in this format name (space) major (space) age (space) height."<<endl<<endl;
for(i=1;i<=x;i++){
cout<<i<<"| ";
cin>>num[i].name;
cin>>num[i].major;
cin>>num[i].age;
cin>>num[i].height;
sum+=num[i].height;
}
system("cls");
cout<<endl;
avg=sum/x;
cout<<"average height: "<<avg<<endl;

cout<<"|"<<"NO"<<setw(1)<<"|"<<"NAME"<<setw(7)<<"|"<<setw(1)<<"MAJOR"<<setw(1)<<"|"<
<"AGE"<<setw(3)<<"|"<<setw(1)<<"HEIGHT"<<setw(1)<<"|"<<endl;
for(i=1;i<=x;i++){
if(num[i].height>=avg){
cout<<"|"<<i<<"
|"<<left<<setw(10)<<num[i].name<<"|"<<left<<setw(5)<<num[i].major<<"|"<<left<<setw(5)<<num
[i].age<<"|"<<left<<setw(6)<<num[i].height<<"|"<<endl;
}
}
}
else{
system("cls");
short loop=1;
string filename;
string line;
cout<<"enter the file name: "<<endl;
cin>>filename;
filename=filename+".txt";

ifstream myfile(filename.c_str());
if (myfile.is_open())
{
while (! myfile.eof() )
{
myfile>>num[loop].name;
myfile>>num[loop].major;
myfile>>num[loop].age;
myfile>>num[loop].height;
sum+=num[loop].height;
loop++;
}
system("cls");
cout<<endl;
avg=sum/(loop-1);
cout<<"average height: "<<avg<<endl;

cout<<"|"<<"NO"<<setw(1)<<"|"<<"NAME"<<setw(7)<<"|"<<setw(1)<<"MAJOR"<<setw(1)<<"|"<
<"AGE"<<setw(3)<<"|"<<setw(1)<<"HEIGHT"<<setw(1)<<"|"<<endl;
for(i=1;i<=(loop-1);i++){
if(num[i].height>=avg){
cout<<"|"<<i<<"
|"<<left<<setw(10)<<num[i].name<<"|"<<left<<setw(5)<<num[i].major<<"|"<<left<<setw(5)<<num
[i].age<<"|"<<left<<setw(6)<<num[i].height<<"|"<<endl;
}
}
myfile.close();
}
else cout << "Unable to open file";
}
return 0;
}

“file.txt” content:
Jono IT 21 180
Jono IT 21 180
Jono IT 21 180
Jono IT 21 180

ALL OUTPUT:

Using .txt file:

Showing (>= AVG) data only:

Вам также может понравиться