below is a previous code that we did. we now must turnit into arrays as describe
ID: 3618070 • Letter: B
Question
below is a previous code that we did. we now must turnit into arrays as described below. any help in this processwill be appreciated Re-write example olympics.cpp using dynamic arrays. Theexample uses one set of variables: countryName, gold, silver, andbronze, and repeatedly use them for each country in a loop. Eachtime a new set of data is read, the data for previous country iswiped out. Your program will create the arrays large enoughto handle data file of up to 180 countries, read in data from datafile until the end of file to the arrays, and then work on the datathat are in the arrays. You are required to use a function to readin the data to the arrays, and another function to process on thedata. The program will behave the same way as before, only now allthe data are stored. #include <iostream>#include <fstream> #include <string> #include <iomanip>
using namespace std;
int main()
{
int totCountries;
string countryName;
string maxGoldCountry;
string maxMedalCountry;
string minMedalCountry;
int gold;
int silver;
int bronze;
int totalMedals = 0;
int maxGold = 0;
int maxMedals = 0;
int minMedals = 9000; double percentGold;
ifstream ifile("medals.dat"); if(ifile.fail()) {
cout<< "Can't find file medals.dat";
return0; }
ifile >> totCountries;
cout << "Total Countries participating: "
cout << endl <<"Country Gold Silver Bronze Total %Gold" <<endl;
ifile >> countryName >> gold >> silver>> bronze;
while(!ifile.eof())
{
totalMedals = gold + silver + bronze; //calculate total medalswon
percentGold = (double(gold)/totalMedals) * 100;
cout << endl << countryName;
if(countryName.length() < 8) {
cout << " "; }
else
{
cout << " "; //}
cout << gold << " " << silver << " "<< bronze << " " << totalMedals;
cout << fixed << showpoint << setprecision(1)<< " " << percentGold << "%";
if( maxGold < gold )
{
maxGold = gold; maxGoldCountry =countryName;
}
if( maxMedals < totalMedals )
{
maxMedals = totalMedals; maxMedalCountry =countryName;
}
if( minMedals > totalMedals )
{
minMedals = totalMedals;
minMedalCountry =countryName;
}
ifile >> countryName >> gold >> silver >>bronze;
cout << endl;
cout << " Most Gold Medals: " << maxGoldCountry<< " with " << maxGold;
cout << " Most Medals won: " << maxMedalCountry<< " with " << maxMedals;
cout << " Least Medals won: " << minMedalCountry<< " with " << minMedals;
cout << " "; //two linefeeds fordisplay before the "Press any..." message
system("pause");
return0; //required because main() returns int
} below is a previous code that we did. we now must turnit into arrays as described below. any help in this processwill be appreciated Re-write example olympics.cpp using dynamic arrays. Theexample uses one set of variables: countryName, gold, silver, andbronze, and repeatedly use them for each country in a loop. Eachtime a new set of data is read, the data for previous country iswiped out. Your program will create the arrays large enoughto handle data file of up to 180 countries, read in data from datafile until the end of file to the arrays, and then work on the datathat are in the arrays. You are required to use a function to readin the data to the arrays, and another function to process on thedata. The program will behave the same way as before, only now allthe data are stored. #include <iostream>
#include <fstream> #include <string> #include <iomanip>
using namespace std;
int main()
{
int totCountries;
string countryName;
string maxGoldCountry;
string maxMedalCountry;
string minMedalCountry;
int gold;
int silver;
int bronze;
int totalMedals = 0;
int maxGold = 0;
int maxMedals = 0;
int minMedals = 9000; double percentGold;
ifstream ifile("medals.dat"); if(ifile.fail()) {
cout<< "Can't find file medals.dat";
return0; }
ifile >> totCountries;
cout << "Total Countries participating: "
cout << endl <<"Country Gold Silver Bronze Total %Gold" <<endl;
ifile >> countryName >> gold >> silver>> bronze;
while(!ifile.eof())
{
totalMedals = gold + silver + bronze; //calculate total medalswon
percentGold = (double(gold)/totalMedals) * 100;
cout << endl << countryName;
if(countryName.length() < 8) {
cout << " "; }
else
{
cout << " "; //}
cout << gold << " " << silver << " "<< bronze << " " << totalMedals;
cout << fixed << showpoint << setprecision(1)<< " " << percentGold << "%";
if( maxGold < gold )
{
maxGold = gold; maxGoldCountry =countryName;
}
if( maxMedals < totalMedals )
{
maxMedals = totalMedals; maxMedalCountry =countryName;
}
if( minMedals > totalMedals )
{
minMedals = totalMedals;
minMedalCountry =countryName;
}
ifile >> countryName >> gold >> silver >>bronze;
cout << endl;
cout << " Most Gold Medals: " << maxGoldCountry<< " with " << maxGold;
cout << " Most Medals won: " << maxMedalCountry<< " with " << maxMedals;
cout << " Least Medals won: " << minMedalCountry<< " with " << minMedals;
cout << " "; //two linefeeds fordisplay before the "Press any..." message
system("pause");
return0; //required because main() returns int
}
Explanation / Answer
please rate - thanks #include #include #include #include using namespace std; void input(string[],int[],int[],int[],int&); void process(string[],int[],int[],int[],int); int main() { int totCountries; string countryName[180]; int gold[180]; int silver[180]; int bronze[180]; input(countryName,gold,silver,bronze, totCountries); process( countryName, gold, silver, bronze,totCountries); system("pause"); return0; //required because main() returns int } void input(string countryName[],int gold[],int silver[],intbronze[],int& totCountries) {int i=0; ifstream ifile("medals.dat"); if(ifile.fail()) { cout> totCountries; cout > countryName[i]; } } void process(string countryName[],int gold[],int silver[],intbronze[],int totCountries) {int i; int totalMedals[180] = {0}; int maxGold = 0; int maxMedals = 0; int minMedals = 9000; double percentGold[180]; string maxGoldCountry; string maxMedalCountry; string minMedalCountry; for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.