Write a C++ program that reads data from a data file(season2017.txt) and stores
ID: 3908495 • Letter: W
Question
Write a C++ program that reads data from a data file(season2017.txt) and stores the data in three parallel arrays of size 5. Once the data is loaded in the arrays, call a function, named listShow(), that displays the array elements in a tabular format, where each column is 15 characters wide. Call a function, named listSort(), that displays the list, in a tabular format (each column 15 characters wide), in ascending order (least to greatest) based on the number of games won.
Specifics:
-The array names are rank, teamName, gamesWon -listShow accepts three arrays and and an integer (string r[ ], string tn[ ], int gw[ ], int s) r represents rank, tn represenet teamName, gw represents gamesWon and s represents number of elements. -listSort accepts three arrays and and an integer (string r[ ], string tn[ ], int gw[ ], int s) r represents rank, tn represenet teamName, gw represents gamesWon and s represents number of elements. Use any sort algorithm.
INPUT: mu
Explanation / Answer
/*The program seems fine except the column width is set as 30 and that needs modifivation.Rest of all seems fine.The modification has
been done in the following code: */
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void listShow(string[], string[], int[], int);
void listSort(string[], string[], int[], int);
int main(){
const int listSize = 5;
int gamesWon[listSize];
string teamName[listSize];
string rank[listSize];
ifstream fin;
fin.open("C:\classdata\season2017.txt");
if(fin){
//Get rank
for(int i = 0; i < listSize; i++){
getline(fin, rank[i]);
}
//Get team name
for(int i = 0; i < listSize; i++){
getline(fin, teamName[i]);
}
//Get games won
for(int i = 0; i < listSize; i++){
fin >> gamesWon[i];
fin.ignore(1, ' ');
}
//fin.ignore(100, ' ');
}
else{
cout << "Error: file not found" << endl;
}
cout << " Original List =============" << endl;
listShow(rank, teamName, gamesWon, listSize);
cout << mu
<< endl;
cout << " Sorted List (by games won) ===========================" << endl;
listSort(rank, teamName, gamesWon, listSize);
return 0;
}
void listShow(string rank[] , string teamsName[], int gamesWon[], int listSize)
{
for(int i=0;i<listSize;i++)
cout<<setw(15)<<left<<rank[i]<<setw(15)<<teamsName[i]<<setw(15)<<gamesWon[i]<<endl;
}
void listSort(string rank[] , string teamsName[], int gamesWon[], int listSize)
{
int di, dj, temp1;
string temp2, temp3;
for (di = 0; di < listSize-1; di++)
{
for (dj = 0; dj < listSize-di-1; dj++)
{
if (gamesWon[dj] > gamesWon[dj+1])
{
temp1=gamesWon[dj];
gamesWon[dj]=gamesWon[dj+1];
gamesWon[dj+1]=temp1;
temp2=rank[dj];
rank[dj]=rank[dj+1];
rank[dj+1]=temp2;
temp2=teamsName[dj];
teamsName[dj]=teamsName[dj+1];
teamsName[dj+1]=temp2;
}
}
}
for(int i=0;i<listSize;i++)
cout<<setw(15)<<left<<rank[i]<<setw(15)<<teamsName[i]<<setw(15)<<gamesWon[i]<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.