The following is my HW problem for my c++ class. I have to create a function , and just the function for it, but obviously to test its effectiveness I'm writing the whole program.
Q:Using those rates and the current US population, write an algorithm to calculate the U.S. population in exactly one year (365 days). Your function should return the result of your calculations. If you end up with a non-integer projected population, then round down to the nearest whole person.
Three rates of change are provided:
a. There is a birth every 8 seconds
b. There is a death every 12 seconds
c. There is a new immigrant arriving in the US every 27 seconds
Specifications:
Your function MUST be named population
Your function should accept only one input argument: the initial population, as an integer
Your function should return the population value in a year.
Your function should not print/display/output anything to the screen
When I input 0 the correct answer is 2482000 acroding to my online submission website, but I am getting: 2483650
I am struggling as to why. Thanks for the help!
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <ostream>
#include <math.h>
using namespace std;
int population(int pop){
cin >> pop;
int birthRate = 8;
int deathRate = 12;
int imigrationRate = 27;
int x =0;
while(x <= 31556952)
{
if ((x % birthRate) == 0) {
pop = pop + 1;
if ((x % deathRate) == 0){
pop = pop - 1;
} else{}
if ((x % imigrationRate) == 0) {
pop = pop + 1;
}else{}
x++;
} else if ((x % deathRate) == 0) {
pop = pop - 1;
if ((x % imigrationRate) == 0) {
pop = pop + 1;
}else{}
x++;
} else if ((x % imigrationRate) == 0) {
pop = pop + 1;
x++;
} else {
x++;
}
}
}
int main(){
cout << "Enter a population value" << endl;
int z;
int y;
z = population(y);
cout << z << endl;
}
|