can you help me on this question

can you help me on this question

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

So what have you done so far?
nothing because im stuck, anyway here is the code I didn't understand how to solve the question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>
using namespace std;
int main(){

	int i; 
	double population;

	for(int t=0; t<=15.0; i++){

		population=4.08*exp(0.019*t);



	}




	return 0;
}
find value of t at the year 2000, if t at 1986 is 1
Then change the start of the loop to that value
i edited the code but it keeps outputting the same value everytime

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
using namespace std;
int main(){
 
	double population,year, i=0;

	for(int t=15; t<=30.0; i++){

		population=4.08*exp(0.019*t);
		
		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;
	}	
	 

	return 0;
}
First of all, why are year and i doubles?

Second, you can make 2 variables 1) starting year, and 2) ending year. Then iterate from starting year until ending year -- [start, end]

Here's how I would do it personally.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//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()
{
    unsigned const start = 2000u;
    unsigned const end = 2015u;
    
    double const p = 4.08;
    double const r = 0.019;
    unsigned const 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;
Last edited on
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 :)

Yes 4.08 billions
Topic archived. No new replies allowed.