Question

In: Computer Science

C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using...

C++ coding question

From the text file given to you- “worldpop.txt”, perform the following tasks using Boolean function. PS-*Write separate codes for each task* Task 1. Display the names of the countries with: 1. Population >=1000,000,000 2. Population <= 1000,000 Task 2. Display the names of the first 10 countries Task 3. Display the names of the last 10 countries

contents of worldpop.txt:

Afghanistan     32738376

Akrotiri        15700

Albania         3619778

Algeria         33769669

Andorra         72413

Angola          12531357

Anguilla        14108

Argentina       40677348

Armenia         2968586

Aruba           101541

Australia       20600856

Austria         8205533

Azerbaijan      8177717

Bahamas         307451

Bahrain         718306

Bangladesh      153546901

Barbados        281968

Belarus         9685768

Belgium         10403951

Belize          301270

Benin           8294941

Bermuda         66536

Bhutan          682321

Bolivia         9247816

Bosnia          4590310

Botswana        1842323

Brazil          191908598

British         24004

Brunei          381371

Bulgaria        7262675

Burkina         15264735

Burma           47758181

Burundi         8691005

Cambodia        14241640

Cameroon        18467692

Canada          33212696

Chad            10111337

Chile           16454143

China           1330044605

Christmas       1402

Cocos           596

Colombia        45013674

Comoros         731775

Congo           66514506

Congo           3903318

Croatia         4491543

Cuba            11423952

Cyprus          792604

Denmark         5484723

Dhekelia        15700

Djibouti        506221

Dominica        72514

Ecuador         13927650

Egypt           81713517

Eritrea         5028475

Estonia         1307605

Ethiopia        78254090

Fiji            931741

Finland         5244749

France          64057790

Gabon           1485832

Gambia          1735464

Georgia         4630841

Germany         82369548

Ghana           23382848

Gibraltar       28002

Greece          10722816

Greenland       56326

Grenada         90343

Guam            175877

Guatemala       13002206

Guernsey        65726

Guinea          10211437

Guyana          770794

Haiti           8924553

Honduras        7639327

HongKong        7018636

Hungary         9930915

Iceland         304367

India           1147995898

Indonesia       237512355

Iran            65875223

Iraq            28221181

Ireland         4156119

Israel          7112359

Italy           58145321

Jamaica         2804332

Japan           127288419

Jersey          91533

Jordan          6198677

Kazakhstan      15340533

Kenya           37953838

Kiribati        110356

KoreaNorth      23479089

KoreaSouth      49232844

Kosovo          2126708

Kuwait          2596799

Kyrgyzstan      5356869

Laos            6677534

Latvia          2245423

Lebanon         3971941

Lesotho         2128180

Liberia         3334587

Libya           6173579

Lithuania       3565205

Luxembourg      486006

Macau           460823

Macedonia       2061315

Madagascar      20042551

Malawi          13931831

Malaysia        25274133

Maldives        379174

Mali            12324029

Malta           403532

Marshall        63174

Mauritania      3364940

Mauritius       1274189

Mayotte         216306

Mexico          109955400

Moldova         4324450

Monaco          32796

Mongolia        2996081

Montenegro      678177

Montserrat      9638

Morocco         34343219

Mozambique      21284701

Namibia         2088669

Nauru           13770

Nepal           29519114

Netherlands     16645313

NewZealand      4173460

Nicaragua       5785846

Niger           13272679

Nigeria         138283240

Niue            1444

Norfolk         2128

Norway          4644457

Oman            3311640

Pakistan        167762040

Palau           21093

Panama          3292693

Paraguay        6831306

Peru            29180899

Philippines     92681453

Poland          38500696

Portugal        10676910

Qatar           928635

Romania         22246862

Russia          140702094

Rwanda          10186063

Samoa           217083

SanMarino       29973

SaudiArabia     28161417

Senegal         12853259

Serbia          10159046

Seychelles      82247

Singapore       4608167

Slovakia        5455407

Slovenia        2007711

Somalia         9558666

SouthAfrica     43786115

Spain           40491051

SriLanka        21128773

Sudan           40218455

Suriname        475996

Svalbard        2165

Swaziland       1128814

Sweden          9045389

Switzerland     7581520

Syria           19747586

Taiwan          22920946

Tajikistan      7211884

Tanzania        40213162

Thailand        65493298

TimorLeste      1108777

Togo            5858673

Tokelau         1433

Tonga           119009

Tunisia         10383577

Turkey          71892807

Turkmenistan    5179571

Tuvalu          12177

Uganda          31367972

Ukraine         45994287

UAE             4621399

UK              60943912

USA             303824646

Uruguay         3477778

Uzbekistan      28268440

Vanuatu         215446

Venezuela       26414815

Vietnam         86116559

WestBank        2611904

Yemen           23013376

Zambia          11669534

Zimbabwe        12382920

Solutions

Expert Solution

Here are the answers for your questions in C++ Programming Language.

Kindly upvote if you find the answer helpful.

NOTE : As you alreadt have the worlpop.txt file with you and its content is big I don't attach the file in the answer.

###############################################################################

TASK - 1:

CODE :

#include<iostream>
#include<string>
#include<fstream>

using namespace std;
//Required structure to hold country data
struct Country{
   string name;
   unsigned long population;
}country;

int main(){
   //Required variables
  
   //Input file object
   ifstream inFile;
   //Open the file 'worldpop.txt'
   inFile.open("worldpop.txt");
   int numberOfCountries = 0,i;
  
   //If file cannot be opened
   if(!inFile){
       cout << "File not found..." << endl;
       return 1;
   }else{
       //Count number of countries in the file
       while(inFile >> country.name >> country.population){
           numberOfCountries++;
       }
       inFile.close();
       inFile.open("worldpop.txt");
      
       //Create structure with number of countries
       struct Country countries[numberOfCountries];
       i = 0;
       //Reads and store each country data to the structure
       while(inFile >> countries[i].name >> countries[i].population){
           i++;
       }
       inFile.close();
      
       /* TASK - 1 : Display the names of the countries with: */
       //Part - 1 : Population >=1000,000,000 */
      
       cout << "Names of the countries with population >= 1000,000,000 : " << endl;
       cout << "=======================================================" << endl;
       for(int j = 0;j<numberOfCountries;j++){
           if(countries[j].population >= 1000000000){
               cout << countries[j].name << endl;
           }
       }
      
       //Part - 1 : Population <= 1000,000 */
      
       cout << "Names of the countries with population <= 1000,000 : " << endl;
       cout << "=======================================================" << endl;
       for(int j = 0;j<numberOfCountries;j++){
           if(countries[j].population <= 1000000){
               cout << countries[j].name << endl;
           }
       }
   }      
   return 0;
}

#################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

#########################################################################

OUTPUT :

#######################################################################################

TASK - 2

CODE :

#include<iostream>
#include<string>
#include<fstream>

using namespace std;
//Required structure to hold country data
struct Country{
   string name;
   unsigned long population;
}country;

int main(){
   //Required variables
  
   //Input file object
   ifstream inFile;
   //Open the file 'worldpop.txt'
   inFile.open("worldpop.txt");
   int numberOfCountries = 0,i;
  
   //If file cannot be opened
   if(!inFile){
       cout << "File not found..." << endl;
       return 1;
   }else{
       //Count number of countries in the file
       while(inFile >> country.name >> country.population){
           numberOfCountries++;
       }
       inFile.close();
       inFile.open("worldpop.txt");
      
       //Create structure with number of countries
       struct Country countries[numberOfCountries];
       i = 0;
       //Reads and store each country data to the structure
       while(inFile >> countries[i].name >> countries[i].population){
           i++;
       }
       inFile.close();          
       /* TASK - 2 : Display the names of the first 10 countries */      
      
       cout << "Names of the first 10 countries: " << endl;
       cout << "=======================================================" << endl;
       for(int j = 0;j<10;j++){          
           cout << countries[j].name << endl;          
       }
              
   }      
   return 0;
}

#################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

####################################################################

OUTPUT :

#####################################################################

TASK - 3

CODE :

#include<iostream>
#include<string>
#include<fstream>

using namespace std;
//Required structure to hold country data
struct Country{
   string name;
   unsigned long population;
}country;

int main(){
   //Required variables
  
   //Input file object
   ifstream inFile;
   //Open the file 'worldpop.txt'
   inFile.open("worldpop.txt");
   int numberOfCountries = 0,i;
  
   //If file cannot be opened
   if(!inFile){
       cout << "File not found..." << endl;
       return 1;
   }else{
       //Count number of countries in the file
       while(inFile >> country.name >> country.population){
           numberOfCountries++;
       }
       inFile.close();
       inFile.open("worldpop.txt");
      
       //Create structure with number of countries
       struct Country countries[numberOfCountries];
       i = 0;
       //Reads and store each country data to the structure
       while(inFile >> countries[i].name >> countries[i].population){
           i++;
       }
       inFile.close();  
      
       /* TASK - 3 : Display the names of the last 10 countries */      
      
       cout << "Names of the last 10 countries: " << endl;
       cout << "=======================================================" << endl;
       int minus10 = numberOfCountries - 10;      
       for(int j = minus10;j<numberOfCountries;j++){          
           cout << countries[j].name << endl;          
       }
              
   }      
   return 0;
}

################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

######################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Develop a C++ program that looks for a given value in a text file full of...
Develop a C++ program that looks for a given value in a text file full of integer values Prompt the user for a value to search for in the file No input validation is required Open the accompanying text file named numbers.txt Search the contents of the text file You may not "hard-code" the quantity of values found in the file... Use a loop of some sort until the end of the text file is reached Maintain how many many...
using java, parse a text file to answer the following question: -list sentences with the maximum...
using java, parse a text file to answer the following question: -list sentences with the maximum number of occurences of the word “the” in the whole file and also list the corresponding frequency. (cannot use hash maps) example output: the:3:The day had came to leave before the storm. What hit the back bumper of the car before the window cracked? The classroom doors where shut closed before the students open the project.
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write...
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write a Schedule.cpp file that implements the missing pieces from Schedule.h. // ************************************ // ************* main.cpp ************ // ************************************ #include "Schedule.h" void main() { Schedule s1(10); s1.addEntry(1,20,"Feed Cat"); s1.addEntry(2,40,"Feed Dog"); s1.addEntry(2,50,"Walk Dog"); s1.addEntry(4,0, "Fix Dinner"); s1.addEntry(5,15,"Eat Dinner"); s1.printSchedule(); Schedule s2(s1); // Note this uses the copy constructor cout << endl << "Output from s2 " << endl; s2.printSchedule(); } // ********************************************** // ************* Schedule.h ******************...
Perform a sentiment analysis of a big text file in python Extract each word from the...
Perform a sentiment analysis of a big text file in python Extract each word from the file, transform the words to lower case, and remove special characters from the words using code similar to the following line:w=w.replace(':','').replace('?','').replace(',','').replace('.','').replace('"','').replace('!','').replace('(','').replace(')','').replace('\'','').replace('\\','').replace('/','') Utilize the lists of positive words, found in positive.txt to perform a sentiment analysis on the file (count how many positive words there are in a file) positive.txt crisp crisper cure cure-all cushy cute cuteness danke danken daring ... file.txt ...has a new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT