below is my program and what the program needed to be. when i submitted it, my t
ID: 3618111 • Letter: B
Question
below is my program and what the program needed to be. when i submitted it, my teacher said that i got the idea ofusing four arrays, however, not the sub-array part. The data filedoes not contain a number at the top indicate how many records arestored. The program needs to find out that. can someone help me outwith it. heres what we need: 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. Now you’ve learned arrays and you may modifythe program so it retains all the data. You may make an array ofstring to store all the country names, and three one-dimensionalarrays, one for each metal of gold, silver andbronze. You don’t know in advance how manycountries’ data are kept in the data file. To make theprogram useful to accommodate most probable data files, it must beable to handle as many as 180 countries. Your program willcreate the arrays large enough to handle data file of up to 180countries, read in data from data file until the end of file to thearrays, and then work on the data that are in the arrays. You arerequired to use a function to read in the data to the arrays, andanother function to process on the data. The program will behavethe same way as before, only now all the data are stored. #include <iostream>#include <fstream>#include <string>#include <iomanip>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");return 0;}void input(string countryName[],int gold[],int silver[],int bronze[],int& totCountries){int i=0;ifstream ifile("medals.txt");if(ifile.fail()){cout<< "Can't find file medals.txt/n";system("pause");exit (0);}ifile >> totCountries;cout << "Total Countries participating: "<<totCountries;cout << endl << "Country Gold Silver Bronze Total %Gold" << endl;ifile >> countryName[i];while(ifile){ifile >> gold[i] >> silver[i] >> bronze[i];i++;ifile >> countryName[i];}}void process(string countryName[],int gold[],int silver[],int bronze[],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;i<totCountries;i++){totalMedals[i] = gold[i] + silver[i] + bronze[i]; //calculate total medals wonif(totalMedals[i]==0)percentGold[i]=0;elsepercentGold[i] = (double(gold[i])/totalMedals[i]) * 100;cout << endl << countryName[i];if(countryName[i].length() < 8){cout << " ";}cout << " "; //}cout << gold[i] << " " << silver[i] << " " << bronze[i] << " " << totalMedals[i];cout << fixed << showpoint << setprecision(1) << " " << percentGold[i] << "%";if( maxGold < gold[i] ){maxGold = gold[i];maxGoldCountry = countryName[i];}if( maxMedals < totalMedals[i] ){maxMedals = totalMedals[i];maxMedalCountry = countryName[i];}if( minMedals > totalMedals[i] ){minMedals = totalMedals[i];minMedalCountry = countryName[i];}}cout << endl;cout << " Most Gold Medals: " << maxGoldCountry << " with " << maxGold;cout << " Most Medals won: " << maxMedalCountry << " with " << maxMedals;cout << " Least Medals won: " << minMedalCountry << " with " << minMedals;cout << " ";} heres the initial text file: Australia 9 9 22China 16 22 12Cuba 3 6 8France 15 7 15Germany 20 18 27Hungary 7 4 10Italy 13 9 12Poland 7 5 5Russia 26 21 16South.Korea 7 15 5Spain 5 11 6Ukraine 9 2 12United.States 23 32 26below is my program and what the program needed to be. when i submitted it, my teacher said that i got the idea ofusing four arrays, however, not the sub-array part. The data filedoes not contain a number at the top indicate how many records arestored. The program needs to find out that. can someone help me outwith it. heres what we need: 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. Now you’ve learned arrays and you may modifythe program so it retains all the data. You may make an array ofstring to store all the country names, and three one-dimensionalarrays, one for each metal of gold, silver andbronze. You don’t know in advance how manycountries’ data are kept in the data file. To make theprogram useful to accommodate most probable data files, it must beable to handle as many as 180 countries. Your program willcreate the arrays large enough to handle data file of up to 180countries, read in data from data file until the end of file to thearrays, and then work on the data that are in the arrays. You arerequired to use a function to read in the data to the arrays, andanother function to process on the data. The program will behavethe same way as before, only now all the data are stored.
#include <iostream>#include <fstream>#include <string>#include <iomanip>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");return 0;}void input(string countryName[],int gold[],int silver[],int bronze[],int& totCountries){int i=0;ifstream ifile("medals.txt");if(ifile.fail()){cout<< "Can't find file medals.txt/n";system("pause");exit (0);}ifile >> totCountries;cout << "Total Countries participating: "<<totCountries;cout << endl << "Country Gold Silver Bronze Total %Gold" << endl;ifile >> countryName[i];while(ifile){ifile >> gold[i] >> silver[i] >> bronze[i];i++;ifile >> countryName[i];}}void process(string countryName[],int gold[],int silver[],int bronze[],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;i<totCountries;i++){totalMedals[i] = gold[i] + silver[i] + bronze[i]; //calculate total medals wonif(totalMedals[i]==0)percentGold[i]=0;elsepercentGold[i] = (double(gold[i])/totalMedals[i]) * 100;cout << endl << countryName[i];if(countryName[i].length() < 8){cout << " ";}cout << " "; //}cout << gold[i] << " " << silver[i] << " " << bronze[i] << " " << totalMedals[i];cout << fixed << showpoint << setprecision(1) << " " << percentGold[i] << "%";if( maxGold < gold[i] ){maxGold = gold[i];maxGoldCountry = countryName[i];}if( maxMedals < totalMedals[i] ){maxMedals = totalMedals[i];maxMedalCountry = countryName[i];}if( minMedals > totalMedals[i] ){minMedals = totalMedals[i];minMedalCountry = countryName[i];}}cout << endl;cout << " Most Gold Medals: " << maxGoldCountry << " with " << maxGold;cout << " Most Medals won: " << maxMedalCountry << " with " << maxMedals;cout << " Least Medals won: " << minMedalCountry << " with " << minMedals;cout << " ";} heres the initial text file: Australia 9 9 22China 16 22 12Cuba 3 6 8France 15 7 15Germany 20 18 27Hungary 7 4 10Italy 13 9 12Poland 7 5 5Russia 26 21 16South.Korea 7 15 5Spain 5 11 6Ukraine 9 2 12United.States 23 32 26
Explanation / Answer
please rate - thanks don't understand what you mean subarrays, but this takes care ofnot getting a count #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=0; 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"); return 0; } void input(string countryName[],int gold[],int silver[],intbronze[],int& totCountries) { int i=0; ifstream ifile("medals.txt"); if(ifile.fail()) { cout> bronze[i]; i++; ifile >> 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.