1

I have looked this over and am wondering where my math issue is. I believe that it should be calculating correctly, but the floats do not round up, .75 to 1 to add to the count for births/deaths. I am a novice to c. Here is the code I have so far:

float births(long popul);
float deaths(long pop);
long yearAdjustment(long pop);
int threshold(long population, long end);

int main(void){

    long begin = 0;
    long end = 0;
    int year = 0;
    float input = 0.0;

    do{
        // TODO: Prompt for start size
       input = get_float("Beginning population: ");
       begin = (long) roundf(input);
    } while (begin < 9);

    do{
        // TODO: Prompt for end size
        input = get_float("Ending population: ");
        end = (long) roundf(input);
    } while (end < begin || end <= 0);

    if(begin == end)
    {
        year = 0;
    } else
    {
        year = threshold(begin, end);
    }
    // TODO: Print number of years
    printf("Years: %i\n", year);
}

    
float births(long pop){
    float tmp = pop / 3;
    return tmp;
}

float deaths(long pop){
     float tmp = pop / 4;
     return tmp;
}

long yearAdjustment(long pop){
    long tmp = pop + ((long) roundf(births(pop) - deaths(pop)));
    return tmp;
}

int threshold(long population, long end){
    int years = 0;
    long tmp = 0;

    // TODO: Calculate number of years until we reach threshold
    while (tmp < end){
        tmp += yearAdjustment(population);
        years++;
    }
    return years;
}

I'm using longs because the numbers may start in the thousands. The floats are for a little precision, more rounding, in the divisions in births/deaths. Essentially it should increment by roughly 1/10/100... respectively of single/tens/hundreds... input. 1.25 on an input of 9. That is where the decimal is important. Technically every 4 years I gain 1 extra. Say 18 as an end should be 8 years.

Thank you.

Lee
  • 11
  • 3
  • 5
    `float tmp = pop / 4;` is done by integer division, so the decimal part is discarded, do probably want to do `float tmp = pop / 4.0f;`. – mch Mar 30 '21 at 06:53
  • Thank you for pointing that out. I do recall that was said in one of the lectures, but slipped my mind. I was enjoying laying out structure I missed the details. – Lee Mar 30 '21 at 07:02
  • `long tmp = population;` and then `tmp += yearAdjustment(tmp);`. Part of the CS50 criteria is to check that `start` is not less than `9`. If this is the CS50 project, there is no need for floating-point calculations. – David C. Rankin Mar 30 '21 at 07:21
  • It is the cs50 problem,. I'm auditing the class and really don't have anyone to speak with on this. One of the checks is that it handles decimal numbers. From the checks it seems to be looking for the adjustment from the combination of decimal results. Using 9 to 18 would result in 9 steps not 8, which is the correct answer according to the check50 output, though i get 2. This is why i realize my issue is math related, not my code. Is there a better algorithm to solve this? Is my implementation of the algorithm wrong? – Lee Mar 30 '21 at 14:54
  • I think i realized my issue. I should initialize tmp with the population and remove the population added in year adjustment. Essentially I'm increasing the original population by adding the original population each time, instead of the adjustment for the year, which is the only thing i should be adding to the original population. – Lee Mar 31 '21 at 14:43

2 Answers2

0

The main problem is that you are using 'long', that is the same as 'long int', so it will not give you any precision in your divisions. You can use 'long double' instead, this way it will also give you the decimals.

Rlyra
  • 86
  • 5
  • Thank you. You're right. Carrying the decimal value is important so it will adjust by the fractional over multiple iterations. – Lee Mar 31 '21 at 14:53
0

Initialize tmp with the population and remove the population added in year adjustment. Population is being added each iteration, creating growth beyond the year adjustment. Similar to balancing a checking account, you wouldn't add the original balance to each transaction.

Lee
  • 11
  • 3