Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a calculation error in my program?

Tags:

c++

function

Apparently there is a calculation error somewhere in my program, but I simply can’t find it.

The only information I have as to why there is a calculation error is this following feedback given by MyProgrammingLab (A site that automatically tests code to see if it's incorrect or not). I don't know what values were entered for the annual death rate and annual birth rate to cause it. Could it be that I'm right but MyProgrammingLab is wrong? Honestly, all my own tests seem fine.

Expected Output:

Year 1: 200 176
Year 2: 176 154
Year 3: 154 135
Year 4: 135 118
Year 5: 118 103
Year 6: 103 90
Year 7: 90 79

Actual Output:

Year 1: 200 199
Year 2: 199 198
Year 3: 198 197
Year 4: 197 196
Year 5: 196 195
Year 6: 195 194
Year 7: 194 193

I built the program according to the following assignment:

In a population, the birth rate is the percentage increase of the population due to births, and the death rate is the percentage decrease of the population due to deaths. Write a program that asks for the following:

  • The starting size of a population (minimum 2) (Prompt Enter starting size:)
  • The annual birth rate (Prompt Enter annual birth rate:)
  • The annual death rate (Prompt Enter annual death rate:)
  • The number of years to display (minimum 1) (Prompt Enter years to display:)

The program should then display the starting population and the projected population at the end of each year. It should use a function that calculates and returns the projected new size of the population after a year. The formula is

  N = P(1 + B)(1 - D) 

where N is the new population size, P is the previous population size, B is the birth rate, and D is the death rate. Annual birth rate and death rate are the typical number of births and deaths in a year per 1000 people, expressed as a decimal. So, for example, if there are normally about 32 births and 26 deaths per 1000 people in a given population, the birth rate would be .032 and the death rate would be .026.

My code:

#include <iostream>
using namespace std;

int projectedNewSize(float population, float annualBirthRate, float annualDeathRate) {
    float annualBirthRate2 = annualBirthRate / 1000;
    float annualDeathRate2 = annualDeathRate / 1000;

    int newpopulation = population * (1 + annualBirthRate2) * (1 - annualDeathRate2);
    return newpopulation;
}

int main() {

    int populationStartingSize = 0;
    float annualBirthRate = 0;
    float annualDeathRate = 0;
    int numberOfYearsToDisplay = 0;

    do  {
        cout << "Enter starting population size: ";
        cin >> populationStartingSize;
        cout << "Enter annual birth rate: ";
        cin >> annualBirthRate;
        cout << "Enter annual death rate: ";
        cin >> annualDeathRate;
        cout << "Enter years to display: ";
        cin >> numberOfYearsToDisplay;
    } while (!(populationStartingSize >= 2) || !(numberOfYearsToDisplay >= 1));

    int population;

    for (int i = 1; i <= numberOfYearsToDisplay; i++) {
        cout << "Year " << i << ": " << populationStartingSize << " ";
        population = projectedNewSize(populationStartingSize, annualBirthRate, annualDeathRate);
        cout << population << endl;
        populationStartingSize = population;
    }

    system("pause");
    return 0;
}
like image 569
Learner Avatar asked Nov 10 '14 10:11

Learner


3 Answers

So, The answer is

There is no need to divide the annualBirthRate and the annualDeathRate by 1000. Since annualBirthRate is calculated as annual births per 1000 of a population, It need not be divided by 1000 again.

Thus removing these lines

float annualBirthRate2 = annualBirthRate / 1000;
float annualDeathRate2 = annualDeathRate / 1000;

and changing

int newpopulation = population * (1 + annualBirthRate2) * (1 - annualDeathRate2);

to

int newpopulation = population * (1 + annualBirthRate) * (1 - annualDeathRate);

So, the final code would look like this:

#include <iostream>
using namespace std;

int projectedNewSize(float population, float annualBirthRate, float annualDeathRate) {

    int newpopulation = population * (1 + annualBirthRate) * (1 - annualDeathRate);
    return newpopulation;
}

int main() {

    int populationStartingSize = 0;
    float annualBirthRate = 0;
    float annualDeathRate = 0;
    int numberOfYearsToDisplay = 0;

    do  {
        cout << "Enter starting population size: ";
        cin >> populationStartingSize;
        cout << "Enter annual birth rate: ";
        cin >> annualBirthRate;
        cout << "Enter annual death rate: ";
        cin >> annualDeathRate;
        cout << "Enter years to display: ";
        cin >> numberOfYearsToDisplay;
    } while (!(populationStartingSize >= 2) || !(numberOfYearsToDisplay >= 1));

    int population;

    for (int i = 1; i <= numberOfYearsToDisplay; i++) {
        cout << "Year " << i << ": " << populationStartingSize << " ";
        population = projectedNewSize(populationStartingSize, annualBirthRate, annualDeathRate);
        cout << population << endl;
        populationStartingSize = population;
    }

    system("pause");
    return 0;
}

You guys discussed in the comments section and left the question unanswered..

like image 90
Rey Rajesh Avatar answered Oct 16 '22 14:10

Rey Rajesh


int projectedNewSize(float population, float annualBirthRate, float annualDeathRate) {

    int newpopulation = roundf(population * (1.0 + annualBirthRate) * (1.0 - annualDeathRate);
    return newpopulation;
}

In the calculatiton you dont have to consider the factor of 1000 if it is already done in the input values. But if you want a 'most accurate' table you have to round the values in a proper mind. The return of the calculation is a float. If you assign it to an int - it always will be truncated. A little difference wich will be carried over from one loop to the next, ending up in some reasonable differences to the expected values at the end. The most proper way would be to change the type of 'newcalculation' to float and round only when display the value.

like image 2
Tunichtgut Avatar answered Oct 16 '22 13:10

Tunichtgut


N = P + BP - DP

Program asked for birthrate and death rate, assume user entered 5 for 5%. Basic math needs to be calculated here 5/100 = 0.05

The formula is now

N = P + (BP/100) - (DP/100)

Replace

    float annualBirthRate2 = annualBirthRate / 1000;
    float annualDeathRate2 = annualDeathRate / 1000;

    int newpopulation = population * (1 + annualBirthRate2) * (1 - annualDeathRate2);

With

    int newpopulation = population + (population * birthRate/100) - (population * deathRate/100)
like image 1
Mark Truong Avatar answered Oct 16 '22 13:10

Mark Truong