Baseball. Write a program to use the file to produce a text file containing the
ID: 3867075 • Letter: B
Question
Baseball. Write a program to use the file to produce a text file containing the information listed above. In the new file, the baseball teams should be in descending order by the percentage of games won.
Complete your program design (flow chart, pseudocode, or other)
Save the finished project as a .pyfile
My program will not run properly keeps saying invalid syntax
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
struct data{
string name;
int wins;
int losses;
double per;
};
void sort( data list[], int count){
data temp;
for(int i = 0; i<count; i++){
for (int j = i+1; j<count; j++){
if (list[i].per < list[j].per){
temp.name = list[j].name;
temp.wins = list[j].wins;
temp.losses = list[j].losses;
temp.per = list[j].per;
list[i].name = list[j].name;
list[i].wins = list[j].wins;
list[i].losses = list[j].losses;
list[i].per = list[j].per;
list[j].name = temp.name;
list[j].wins = temp.wins;
list[j].losses = temp.losses;
list[j].per = temp.per;
}
}
}
}
int main(){
ifstream fin;
ofstream fout;
data list[20];
int count;
string name;
int wins;
int losses;
double per;
data temp;
string line;
count = 0;
fin.open("file1.txt");
if (fin) {
while (getline(fin,line)){
if (line.find("Team") != string::npos)
continue;
istringstream iss(line);
iss >> name >> wins >> losses >> per;
list[count].name = name;
list[count].wins = wins;
list[count].losses = losses;
list[count].per = per;
count++;
}
fin.close();
for (int i = 0; i<count; i++){
cout << setprecision(3) << setw(10) << list[i].name << " " << list[i].wins << " " << list[i].losses<< " " << list[i].per<<endl;
}
sort(list,count);
fout.open("file1out.txt");
for (int i = 0; i<count; i++){
fout << setprecision(3) << setw(10) << list[i].name << " " << list[i].wins << " " << list[i].losses<< " " << list[i].per<<endl;
}
fout.close();
}
else {
cout << "Error opening file" << endl;
}
return 0;
}
Team Won Lost Pct
Baltimore 94 44 0.593
Newyork 84 78 0.519
Toronto 83 79 0.512
TampaBay 77 85 0.475
Boston 71 91 0.438
This program will not run. There is an error reading invalid syntax. Something is missing.
6 Alti more Lo 0.593 2 ne oronto 0.415 am 0.438Explanation / Answer
team=list();
won=list();
lost=list();
pct=list();
count=0;
with open("file.txt") as file:#file.txt is the input file to be read
data = file.readlines()
for line in data :
cleanedLine = line.strip().split();
line=cleanedLine;
if count!=0:
team.append(line[0]);
won.append(line[1]);
lost.append(line[2]);
pct.append(line[3]);
count=1;
def fun( pct, count):# soting the list based on percentage
for i in range(0,count):
for j in range(i+1,count):
if (pct[i] < pct[j]):
tempname = team[i];
tempwins = won[i];
templosses = lost[i];
tempper = pct[i];
team[i]= team[j];
won[i] = won[j];
lost[i] = lost[j];
pct[i] = pct[j];
team[j] = tempname;
won[j] = tempwins;
lost[j] = templosses;
pct[j] = tempper;
fun(pct,len(pct));
thefile = open('test.txt', 'w');#test.txt is the output file
for i in range(0,len(pct)):
thefile.write(team[i]+" "+won[i]+" "+lost[i]+" "+pct[i]);
thefile.write(" ");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.