A
A
Alexander2020-11-20 16:49:16
C++ / C#
Alexander, 2020-11-20 16:49:16

How to display the sum of loop iterations?

I am going through the cs50 course, the task sounds like this:

Task
Complete the implementation of population.c, such that it calculates the number of years required for the population to grow from the start size to the end size.

Your program should first prompt the user for a starting population size.
If the user enters a number less than 9 (the minimum allowed population size), the user should be re-prompted to enter a starting population size until they enter a number that is greater than or equal to 9.

Say we have a population of n llamas. Each year, n / 3 new llamas are born, and n / 4 llamas pass away.

For example, if we were to start with n = 1200 llamas, then in the first year, 1200 / 3 = 400 new llamas would be born and 1200 / 4 = 300 llamas would pass away. At the end of that year, we would have 1200 + 400 - 300 = 1300 llamas.

To try another example, if we were to start with n = 1000 llamas, at the end of the year, we would have 1000 / 3 = 333.33 new llamas. We can’t have a decimal portion of a llama, though, so we’ll truncate the decimal to get 333 new llamas born. 1000 / 4 = 250 llamas will pass away, so we’ll end up with a total of 1000 + 333 - 250 = 1083 llamas at the end of the year.


My solution: since the cycle continues until the sum calculated by the formula x=n+(n/3)-(n/4) where n is the number of population at the start, less than the number of population at the end, then to -number of iterations = number of years required to reach the number of final population.
The code:

#include <cs50.h>
#include <stdio.h>

int start_int();
int end_int();
int calculate();

int main(void) {
    int i = start_int();
    int j = end_int(i);
    int h = calculate(j, i);
}

// prompt the user for a starting population size
int start_int(void) {
    int y;
    do {
        y = get_int("set starting population size: ");
    } while (y < 9);
    return y;
}

// prompt the user for an ending population size
int end_int(start_int) {
    int x;
    do {
        x = get_int("set ending population size: ");
    } while (x < start_int);
    return x;
}

int calculate(start_int, end_int) {
    int k;
    int n = 0; //счетчик
    do {
        k = start_int + (start_int / 3) - (start_int / 4);
    } while (k < end_int);
    {
        n++;           //инкремент счетчика
        start_int = k; //подмена полученной суммы на "start_int"
        return start_int; //возврат замененного значения
    }
    printf("%i", n); //не выводит сумму итераций
}


where printf("%i", n); does not display sum of iterations

result
~/ $ ./population
set starting population size: 1200
set ending population size: 1300


Tell me please, what could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Demin, 2020-11-20
@gliese581d

A small recommendation - specify the types of arguments in functions and the arguments themselves in prototypes:

int start_int();
int end_int(int start_int);
int calculate(int start_int, int end_int);

Solution - you need to rewrite the calculate function as follows:
  1. n++ needs to be moved to while so that the variable n is incremented every iteration.
  2. Set the initial value of k to start_int, since k is the current population size.
  3. Instead of start_int to update k, use the value of k itself, according to the rules for updating the population size
  4. Remove printf, since the calculate function must find the number of iterations, it is better to move its output to main.
  5. Remove parentheses around return, there is no point in wrapping return in a block.
  6. Return n instead of start_int.

int calculate(int start_int, int end_int) {
    int k = start_int; //текущий размер популяции
    int n = 0; //счетчик

    do {
        k = k + (k / 3) - (k / 4);
        n++;           //инкремент счетчика
    } while (k < end_int);

    return n; //возврат количества итераций
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question