Objectives: • Use void and value-returning functions to divide the program into
ID: 3670112 • Letter: O
Question
Objectives: • Use void and value-returning functions to divide the program into manageable pieces. • Use reference parameters. • Use input and output files. • Format output.
Write a program that estimates the population growth for several countries over a specified number of years. The population growth depends on the birth rate and death rate. It is, however, projected that the birth rate and the death rate increase or decrease by a small percentage year after year. For example, a birth rate of 5% might increase by 1% the following year, which increases the birth rate to 5.05%. The data for all countries are stored in a text file. Your results should be printed to another text file. Your program should start by asking the user for input and output file names. If any of these files fail to open your program should display an error message and quit.
Calculate the projected population after n years. Print the country name, original population, projected population (rounded to the nearest 1), and the number of years. All output should be stored in the output text file. See sample output file below.
projected population = <population > +<growth rate >/100 * population
growth = birth rate - death rate
You must use a function to do each of the following (you may use more functions if you like): • Read the input and output file names (use reference parameters) • Calculate the projected population growth for a single country. • Output the results to the file as shown below.
Input text file: Each line in the input file contains statistical data for a single country separated by a white space. The data is organized as follows:
<country name> <current pop> <birth rate> <annual change birth rate> <death rate> <annual change death rate> <years>
USA 320000000 4.0 .5 1.5 -.1 10
China 1250000000 2.0 .5 1.5 -.1 5
Brazil 200000000 3.0 .5 2.0 .1 20
Russia 143000000 4.0 .4 1.8 -.1 10
Mexico 122000000 4.5 .7 2.1 -.1 15
Argentina 40000000 3.5 .2 3.0 -.1 20
Belarus 2000000 -.6 .1 1.5 -.1 50
The program should continue calculating projected population until the whole input file is processed.
Sample output: Output the result in the output text file using the following format:
Country Current Pop Projected Pop Years
-------------------------------------------------------
USA 320000000 413557290 10
China 1250000000 1283037325 5
Brazil 200000000 250284809 20
Russia 143000000 179175589 10
Mexico 122000000 180396152 15
Argentina 40000000 45044443 20
Belarus 9500000 3323277 50
Hints: • Start by reading the data from the input file and print the same data to the output file. This is to verify that you are processing the input file correctly. Use a sentinel controlled loop that quits when the end of file is reached.
get the country name
while not eof get the rest of the data items
//do the calculations later output the data //results later
get the next country name
• Print your results to the screen first for quicker verification of correctness. Once you’re satisfied with the results, print to the file.
Explanation / Answer
/**C++ program that read a input population.txt file and print the population
datat to console. Then open a output file projPopulation.txt file with projected population
over years of time*/
//include header files
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
//function prototype
long int projPopulation(long int currPop, float rate, int years);
int main()
{
//input and output text file names
string inputFile="population.txt";
string outputFile="projPopulation.txt";
string cname;
long int currPop;
float birthrate;
float annualchangeBirhtrate;
float deathrate;
float annualchangeDeathrate;
int years;
//Open input file
ifstream fin;
fin.open(inputFile);
//Check if input file exists
if(!fin)
{
cout<<"File doesnot exist. Terminate the program. "<<endl;
system("pause");
exit(0);
}
//print heading
cout<<setw(10)<<"Name"
<<setw(10)<<"currPop"
<<setw(10)<<"birthrate"
<<setw(10)<<"Ann Birhtrate"
<<setw(10)<<"Deathrate"
<<setw(10)<<"Ann Deathrate"
<<setw(5)<<"years"<<endl;
//read and display the input file to the console
while(fin>>cname>>currPop>>birthrate>>annualchangeBirhtrate>>deathrate>>annualchangeDeathrate>>years)
{
cout<<setw(10)<<cname
<<setw(10)<<currPop
<<setw(10)<<birthrate
<<setw(10)<<annualchangeBirhtrate
<<setw(10)<<deathrate
<<setw(10)<<annualchangeDeathrate
<<setw(10)<<years<<endl;
}
cout<<endl<<endl;
fin.close();
//Open output file
ofstream fout;
fout.open(outputFile);
//Check if file exists
if(!fout)
{
cout<<"File doesnot exist. Terminate the program. "<<endl;
system("pause");
exit(0);
}
//open a input file
fin.open(inputFile);
cout<<left<<setw(15)<<"Country"<<setw(15)<<"Current Pop"<<setw(15)<<"projected Pop"<<setw(15)<<"years"<<endl;
//read data from the input file object fin
while(fin>>cname>>currPop>>birthrate>>annualchangeBirhtrate>>deathrate>>annualchangeDeathrate>>years)
{
//call the method projPopulation that returns the projection of population
long int projection=projPopulation(currPop,(birthrate-deathrate),years);
cout<<left<<setw(15)<<cname<<left<<setw(15)<<currPop<<left<<setw(15)<<projection<<left<<setw(15)<<years<<endl;
fout<<left<<setw(10)<<cname<<left<<setw(10)<<currPop<<left<<setw(10)<<projection<<left<<setw(10)<<years<<endl;
}
//close the input and output file objects
fin.close();
fout.close();
system("pause");
return 0;
}
/**The method projPopulation that takes current population , rate and years as input arguments
and returns the current population after n years. */
long int projPopulation(long int currPop, float rate, int years)
{
long int total=0;
for(int year=1;year<=years;year++)
{
long int projection=currPop+(rate/100.0)*currPop;
currPop=projection;
}
return currPop;
}
----------------------------------------------------------------------------------
Input file : USA 320000000 4.0 .5 1.5 -.1 10
China 1250000000 2.0 .5 1.5 -.1 5
Brazil 200000000 3.0 .5 2.0 .1 20
Russia 143000000 4.0 .4 1.8 -.1 10
Mexico 122000000 4.5 .7 2.1 -.1 15
Argentina 40000000 3.5 .2 3.0 -.1 20
Belarus 2000000 -.6 .1 1.5 -.1 50
-----------------------------------------------------------------------------------
output text file : projPopulation.txt
USA 320000000 409627052 10
China 125000000012815640655
Brazil 200000000 244038000 20
Russia 143000000 177764480 10
Mexico 122000000 174124215 15
Argentina 40000000 44195817 20
Belarus 2000000 692079 50
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.