Population function

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;
}
Last edited on
x is uninitialized.

-Albatross
You should probably enter the pop in the place you prompted for it (main). No point otherwise in having it as an argument.

Since you are given the (rather unlikely) fixed absolute rates, just return
pop + S * ( 1.0 / 8 - 1.0 / 12 + 1.0 / 27 )
It's quicker. S is the number of seconds in a year.
What is the return value of population?

(Also else{} is unnecessary.)
PS: If you edit your OP, please make it known what you edited, or just make a new post, otherwise it's really hard to keep track of what is actually happening.
Last edited on
Also, y (in main) is also uninitialized. It arguably doesn't matter because you read into pop in your population function, but that's against the spirit of the prompt: to write a function that handles a single specific task. Right now, it handles two: input, and computation.

-Albatross
Also also, both of the errors that the three of us mentioned would show up as warnings if you enabled warnings on your compiler.

On GCC, you can simply pass the -Wall flag.
On Visual Studio, go into your project properties and go to Build --> Warnings and Errors section.
I suspect your online submission site is using exactly 365 days in a year, rather than the more precise, but considerably less memorable, value that you have hard-coded.

Difficult to check since you don't tell us your input value, but I guess the answer has probably changed since you first submitted the question!
Last edited on
Registered users can post here. Sign in or register to post.