A model of world population in billions of people is given by the equation
Population = 4.08e^0.019t
where t is the time in years (t=0 represents January 1985 and t=1 represents January 1986).
Write a C++ program that uses the above formula to display a yearly population table for the years 2000 through 2015.
----------
the question reminded me about the leap year question but in this case i have to use loop which is a big problem for me
//Example program to calculate the formula (pe^rt
//Population = 4.08e^0.019t -- p = 4.08, r = 0.019, t = unknown
//where t is the number of years since 1985
//then output the population for years [start, end]
//where start = 2000 and end = 2015
#include <iostream> //std::cout, std::endl
#include <cmath>
int main()
{
unsignedconst start = 2000u;
unsignedconst end = 2015u;
doubleconst p = 4.08;
doubleconst r = 0.019;
unsignedconst t = 1985;
//you could make a variable for e also, but I'll just use exp()
for(unsigned year = start; year <= end; ++year)
{
std::cout << "Year " << year << " has a population of " << p * std::exp(r * (year - t)) << '\n';
}
std::cout << std::endl;
return 0;
}
Year 2000 has a population of 5.42543
Year 2001 has a population of 5.5295
Year 2002 has a population of 5.63556
Year 2003 has a population of 5.74366
Year 2004 has a population of 5.85383
Year 2005 has a population of 5.96612
Year 2006 has a population of 6.08056
Year 2007 has a population of 6.1972
Year 2008 has a population of 6.31607
Year 2009 has a population of 6.43722
Year 2010 has a population of 6.5607
Year 2011 has a population of 6.68654
Year 2012 has a population of 6.8148
Year 2013 has a population of 6.94552
Year 2014 has a population of 7.07875
Year 2015 has a population of 7.21453
Your biggest problem is:
1 2 3 4 5 6 7 8 9 10 11
cout<<"Enter the year: ";
cin>>year;
if (year>=2000 && year<=2015){
cout<<"yearly population is: "<<population<<endl;
i++;
}
else
cout<<"Invalid year! Enter Again "<<endl;
should be cout << "yearly poplulation for " << 1985 + t << " is " << popluation << endl;
By the way I am guessing the 4.08 is in billions?
Also if you wanted them to type in the year you wouldn't need the for loop, you would simply use the equation and output that. Such as
1 2 3 4
cout << "Enter the year: ";
cin >> year;
cout << "yearly population for year " << year << " is " << 4.08 * exp(0.019 * (year - 1985)) << endl;
Actually the reason is that my teacher said that we mostly gonna use int and double thats why, and btw this is the first time i see a const inside the main(), we usually apply it before starting :)