Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ HELP Write a program that prints the 3 largest countries (by 2015 population

ID: 3762472 • Letter: C

Question

C++ HELP

Write a program that prints the 3 largest countries (by 2015 population). Also modify the data structure to store the indices of these 3 countries so that the searching can be avoided in the future. One algorithm to do this is a 3 pass algorithm; a better algorithm might be to keep track of the 'top 3' and adjust these as you iterate through the data structure.

What the csv. file is:

I already have this much. I'm not sure how to continue.

#include

#include

#include

using namespace std;

const int MAXCOUNTRIES = 230;

void myWorld(Country country);

struct Country {

string name;

double pop1950;

double pop1970;

double pop1990;

double pop2010;

double pop2015;

};

int main(){

vector large2015;

Country country;

myWorld(country);

while (data >> country.pop2015){

large2015.push_back(country.pop2015);

}

   for (int i = 0; i < large2015.size(); i++)

cout << large2015[i] << endl;   

return 0;

}

void myWorld(Country country){

ifstream data;

ofstream testfile;

  

data.open("population_corrected.csv");

testfile.open("population_test.csv");

  

while(data >> country.pop1950 >> country.pop1970 >> country.pop1990 >> country.pop2010 >> country.pop2015){

getline(data, country.name);

testfile << country.pop1950 << country.pop1970 << country.pop1990 << country.pop2010 << country.pop2015 << country.name<

}

data.close();

testfile.close();

}

2308.923 3467.113 5613.141 9461.117 11178.921 Burundi 156.334 228.443 415.144 698.695 788.474 Comoros 62.001 159.667 588.356 830.802 887.861 Djibouti 1142.15 1805.48 3139.083 4689.664 5227.791 Eritrea 18128.034 28414.999 48057.094 87561.814 99390.75 Ethiopia 6076.757 11252.466 23446.229 40328.313 46050.302 Kenya 4083.554 6576.301 11545.782 21079.532 24235.39 Madagascar 2953.871 4603.739 9408.998 14769.824 17215.232 Malawi 493.254 826.447 1055.865 1247.9511273.212 Mauritius 15.141 36.965 94.78 208.723 240.015 Mayotte 6313.29 9262.078 13371.971 24321.457 27977.863 Mozambique 2186.187 3754.546 7259.74 10293.669 11609.666 Rwanda 36.322 52.364 70.627 93.081 96.471 Seychelles 2264.081 3445.42 6321.615 9581.714 10787.104 Somalia 2582.929 3647.097 5762.19 10056.475 12339.812 South Sudan 5158.193 9446.024 17384.369 33149.417 39032.383 Uganda 7649.766 13605.504 25458.208 45648.525 53470.42 United Republic of Ta 2316.95 4185.378 8143.142 13917.439 16211.767 Zambia 2746.854 5206.311 10484.771 13973.897 15602.751 Zimbabwe 4354.882 6300.969 11127.87 21219.954 25021.974 Angola

Explanation / Answer

Below is the required code. I have commented some old code & given new code for it. Let me know if you have any queries.

#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
const int MAXCOUNTRIES = 230;
void myWorld(Country country);
struct Country {
   string name;
   double pop1950;
   double pop1970;
   double pop1990;
   double pop2010;
   double pop2015;
};

int main(){
   int i=0, cnt=0;
   double max1, max2, max3;
   string max1Name, max2Name, max3Name;
   /* vector large2015; */
   Country country[MAXCOUNTRIES];
   cnt = myWorld(country);

   max1 = country[0].pop2015;
   max2 = 0.0;
   max3 = 0.0;
   max1Name = country[0].name;
   max2Name = null;
   max3Name = null;

   for(i=1; i<cnt; i++)
   {
   if(country[i].pop2015 > max1)
   {
       max3 = max2; max3Name = max2name;
       max2 = max1; max2Name = max1name;
       max1 = country[i].pop2015; max1Name = country[i].name;
   }
   }

   cout << " First Max Country : " << max1Name << " , Population : " << max1;
   cout << " Second Max Country : " << max2Name << " , Population : " << max2;
   cout << " Third Max Country : " << max3Name << " , Population : " << max3;

/*
   while (data >> country.pop2015){
   large2015.push_back(country.pop2015);
   }

   for (int i = 0; i < large2015.size(); i++)
   cout << large2015[i] << endl;   
*/

   return 0;
}

int myWorld(Country country[]){
   int i=0;
   ifstream data;
   ofstream testfile;
  
   data.open("population_corrected.csv");
   testfile.open("population_test.csv");
  
   while(data >> country[i].pop1950 >> country[i].pop1970 >> country[i].pop1990 >> country[i].pop2010 >> country[i].pop2015){
   getline(data, country[i].name);
   /*  
   testfile << country.pop1950 << country.pop1970 << country.pop1990 << country.pop2010 <<    country.pop2015 << country.name<
   */
   i++;
   }
   data.close();
   testfile.close();
   return i; // i tells the number of records in the structure
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote