below is the previous code and what we need to do now. iam just now barely under
ID: 3618209 • Letter: B
Question
below is the previous code and what we need to do now. iam just now barely understanding arrays and my teacher just keepsassigning the programs. some help would be great. Re-write in-class Olympics_Sub_Array using array of structvariables.Define a struct as follows: struct SOlympicsData { string countryName; int gold. silver, bronze; }; The program should declare and define two functions: int getData (SOlympicsData[], int); // it gets an array of SOlympicsData with maximum capacitycomponents // opens the data file, read into the array, // and return how many countries were read void processData (SOlympicsData[], int); / it performs statistical calculation on part of the arraythat has data in it // and displays the result
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void input(string[],int[],int[],int[],int&);
voidprocess(string,int,int,int,int&,int&,int&,string&,string&,string&);
int main()
{int i;
int totCountries=0;
string countryName[180];
int gold[180];
int silver[180];
int bronze[180];
int maxGold = 0;
int maxMedals = 0;
int minMedals = 9000;
string maxGoldCountry;
string maxMedalCountry;
string minMedalCountry;
input(countryName,gold,silver,bronze, totCountries);
for(i=0;i<totCountries;i++)
process( countryName[i], gold[i], silver[i],bronze[i],maxGold,maxMedals,
minMedals,maxGoldCountry,maxMedalCountry,minMedalCountry);
cout << endl;
cout << " Most Gold Medals: " << maxGoldCountry<< " with " << maxGold;
cout << " Most Medals won: " << maxMedalCountry<< " with " << maxMedals;
cout << " Least Medals won: " << minMedalCountry<< " with " << minMedals;
cout << " ";
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<< "Can't find file medals.txt ";
system("pause");
exit (0);
}
cout << endl <<"Country Gold Silver Bronze Total %Gold" << endl;
ifile >> countryName[i];
while(ifile)
{totCountries++;
ifile >> gold[i] >> silver[i] >> bronze[i];
i++;
ifile >> countryName[i];
}
}
void process(string countryName,int gold,int silver,int bronze,
int& maxGold,int& maxMedals,int& minMedals,string&maxGoldCountry,
string& maxMedalCountry, string& minMedalCountry)
{
int totalMedals;
double percentGold;
totalMedals = gold+ silver + bronze; //calculate total medalswon
if(totalMedals==0)
percentGold=0;
else
percentGold = (double(gold)/totalMedals) * 100;
cout << endl << countryName;
if(countryName.length() < 8)
{
cout << " ";
}
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;
}
}
Heres the text file input: Australia 9 9 22
China 16 22 12
Cuba 3 6 8
France 15 7 15
Germany 20 18 27
Hungary 7 4 10
Italy 13 9 12
Poland 7 5 5
Russia 26 21 16
South.Korea 7 15 5
Spain 5 11 6
Ukraine 9 2 12
United.States 23 32 26 below is the previous code and what we need to do now. iam just now barely understanding arrays and my teacher just keepsassigning the programs. some help would be great. Re-write in-class Olympics_Sub_Array using array of structvariables.
Define a struct as follows: struct SOlympicsData { string countryName; int gold. silver, bronze; }; The program should declare and define two functions: int getData (SOlympicsData[], int); // it gets an array of SOlympicsData with maximum capacitycomponents // opens the data file, read into the array, // and return how many countries were read void processData (SOlympicsData[], int); / it performs statistical calculation on part of the arraythat has data in it // and displays the result
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void input(string[],int[],int[],int[],int&);
voidprocess(string,int,int,int,int&,int&,int&,string&,string&,string&);
int main()
{int i;
int totCountries=0;
string countryName[180];
int gold[180];
int silver[180];
int bronze[180];
int maxGold = 0;
int maxMedals = 0;
int minMedals = 9000;
string maxGoldCountry;
string maxMedalCountry;
string minMedalCountry;
input(countryName,gold,silver,bronze, totCountries);
for(i=0;i<totCountries;i++)
process( countryName[i], gold[i], silver[i],bronze[i],maxGold,maxMedals,
minMedals,maxGoldCountry,maxMedalCountry,minMedalCountry);
cout << endl;
cout << " Most Gold Medals: " << maxGoldCountry<< " with " << maxGold;
cout << " Most Medals won: " << maxMedalCountry<< " with " << maxMedals;
cout << " Least Medals won: " << minMedalCountry<< " with " << minMedals;
cout << " ";
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<< "Can't find file medals.txt ";
system("pause");
exit (0);
}
cout << endl <<"Country Gold Silver Bronze Total %Gold" << endl;
ifile >> countryName[i];
while(ifile)
{totCountries++;
ifile >> gold[i] >> silver[i] >> bronze[i];
i++;
ifile >> countryName[i];
}
}
void process(string countryName,int gold,int silver,int bronze,
int& maxGold,int& maxMedals,int& minMedals,string&maxGoldCountry,
string& maxMedalCountry, string& minMedalCountry)
{
int totalMedals;
double percentGold;
totalMedals = gold+ silver + bronze; //calculate total medalswon
if(totalMedals==0)
percentGold=0;
else
percentGold = (double(gold)/totalMedals) * 100;
cout << endl << countryName;
if(countryName.length() < 8)
{
cout << " ";
}
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;
}
}
Heres the text file input: Australia 9 9 22
China 16 22 12
Cuba 3 6 8
France 15 7 15
Germany 20 18 27
Hungary 7 4 10
Italy 13 9 12
Poland 7 5 5
Russia 26 21 16
South.Korea 7 15 5
Spain 5 11 6
Ukraine 9 2 12
United.States 23 32 26
Explanation / Answer
please rate - thanks #include #include #include #include using namespace std; struct SOlympicsData { string countryName; int gold, silver, bronze; }data[180]; //void input(string[],int[],int[],int[],int&); //voidprocess(string,int,int,int,int&,int&,int&,string&,string&,string&); int getData (SOlympicsData[], int); // it gets an array of SOlympicsData with maximum capacitycomponents // opens the data file, read into the array, // and return how many countries were read void processData (SOlympicsData[], int); // it performs statistical calculation on part of the array thathas data in it // and displays the result int main() {int i; int totCountries=0; totCountries=getData (data,180); processData( data,totCountries); system("pause"); return 0; } int getData (SOlympicsData data[], int n) {int i=0; ifstream ifile("medals.txt"); if(ifile.fail()) { coutdata[i].bronze; i++; ifile >> data[i].countryName; } return i; } void processData (SOlympicsData data[], int n) { int maxGold = 0; int maxMedals = 0; int minMedals = 9000; string maxGoldCountry; string maxMedalCountry; stringminMedalCountry; int totalMedals; double percentGold; int i; for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.